DotNetZip ExtractProgress Error?

ExtractProgressEventArgs.EntriesTotal and ExtractProgressEventArgs.EntriesExtracted always zero. Is this a known bug? See my code below:

 public static void UnZip(string zipFile, string destination) { using(ZipFile zip = ZipFile.Read(zipFile)) { zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(zip_ExtractProgress); foreach (ZipEntry entry in zip) { entry.Extract(destination, ExtractExistingFileAction.OverwriteSilently); } Console.WriteLine("DONE"); } } static void zip_ExtractProgress(object sender, ExtractProgressEventArgs e) { if(e.EventType == ZipProgressEventType.Extracting_AfterExtractEntry) Console.WriteLine(String.Format("{0} : {1} / {2} = {3}%", e.CurrentEntry.FileName, e.EntriesTotal, e.EntriesExtracted, ((double)e.EntriesTotal / (double)e.EntriesExtracted) * 100.00)); } 
+7
source share
1 answer

Behavior defined:

from http://cheeso.members.winisp.net/DotNetZipHelp/html/91d797c7-efff-99a3-2b14-6c9a9797b324.htm

RecordsExtracted The amount of data retrieved. This is set only if the EventType is Extracting_BeforeExtractEntry or Extracting_AfterExtractEntry, and Extract () occurs in the scope of the ExtractAll () call.

The reason for this is simple: if you call Extract () in a loop that you yourself control, there is no way for the library to find out how many times you plan to call it, how many times you called it in a row, whether to read the first 5 times with the next 5 times and so on. Therefore, DotNetZip cannot give you reliable information about how much you have done.

You have two simple solutions:

  • use an int counter that you increment for every call to Extract () in your loop.

  • calling ExtractAll (), in which case it will throw ExtractProgressEvents with the EntriesExtracted value set to a significant number.

option 1:

  int n; using(ZipFile zip = ZipFile.Read(zipFile)) { zip.ExtractProgress += zip_ExtractProgress; n = 0; foreach (ZipEntry entry in zip) { n++; entry.Extract(destination, ExtractExistingFileAction.OverwriteSilently); } Console.WriteLine("DONE"); } 

option 2:

  using(ZipFile zip = ZipFile.Read(zipFile)) { zip.ExtractProgress += zip_ExtractProgress; zip.ExtractAll(destination, ExtractExistingFileAction.OverwriteSilently); Console.WriteLine("DONE"); } 
+4
source

All Articles