Reputation: 182
This problem is solved. My mistake was that within my second block of code I was using SaveProgress instead of ExtractProgress and I was using Zip.ExtractAll before setting up the Event Handler. Thanks to Bradley Moorfield for helping me.
So I'm using the DotNetZip library to compress/extract zip files throughout my code. I was able to accomplish showing the percentage of compression with this code:
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(filePath);
var mb = GetDirectorySize(filePath) / 1048576;
long timesRunNeeded = mb / 100;
if (mb % 100 > 0) { timesRunNeeded++; }
if (mb <= 100) { timesRunNeeded = 1; }
int timesRun = 1;
int pastP = 0;
zip.SaveProgress += (o, args) =>
{
var percentage = (int)(1.0d / args.TotalBytesToTransfer * args.BytesTransferred * 100.0d);
if (pastP != percentage)
{
clearLine();
setColor(ConsoleColor.Gray); Console.Write(" Percentage: "); setColor(ConsoleColor.Green);
Console.Write(percentage + " [" + timesRun + "/" + timesRunNeeded + "]");
if ((percentage == 100) && (pastP >= 0) && (pastP <= 99))
{
timesRun++;
}
}
pastP = percentage;
};
zip.Save(filePath + ".zip");
}
The reason it is like this is beacuse it compresses 100 mb at a time, so for example, if a folder was 2153 mb, it'd go to 100% 22 times. This is all working perfectly, and I like the way I have it, however I'm having some trouble displaying the percentage done when extracting from a zip to a directory. Here's my code so far:
using (ZipFile zip = ZipFile.Read(filePath))
{
Directory.CreateDirectory(filePath.Replace(".zip", ""));
zip.ExtractAll(filePath.Replace(".zip", ""), ExtractExistingFileAction.OverwriteSilently);
zip.SaveProgress += (o, args) =>
{ //Fix this, not showing percentage of extraction.
var percentage = (int)(1.0d / args.TotalBytesToTransfer * args.BytesTransferred * 100.0d);
clearLine();
Console.Write(percentage);
};
}
For whatever reason, this code doesn't print any percentage at all, so I'm guessing it's got something to do with the way I'm actually calculating the percentage. Should it be different when compressing vs extracting? Thanks in advance.
Upvotes: 2
Views: 3560
Reputation: 877
You need to add the event handler before you start extracting. Also, the SaveProgress
event fires during saving, there is a different handler, ExtractProgress
that fires during extraction.
See the reference for example usage (web.archive.org)
private static bool justHadByteUpdate = false;
public static void ExtractProgress(object sender, ExtractProgressEventArgs e)
{
if(e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten)
{
if (justHadByteUpdate)
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(" {0}/{1} ({2:N0}%)", e.BytesTransferred, e.TotalBytesToTransfer,
e.BytesTransferred / (0.01 * e.TotalBytesToTransfer ));
justHadByteUpdate = true;
}
else if(e.EventType == ZipProgressEventType.Extracting_BeforeExtractEntry)
{
if (justHadByteUpdate)
Console.WriteLine();
Console.WriteLine("Extracting: {0}", e.CurrentEntry.FileName);
justHadByteUpdate= false;
}
}
public static ExtractZip(string zipToExtract, string directory)
{
string TargetDirectory= "extract";
using (var zip = ZipFile.Read(zipToExtract)) {
zip.ExtractProgress += ExtractProgress;
foreach (var e in zip1)
{
e.Extract(TargetDirectory, true);
}
}
}
Upvotes: 4