Search code examples
c#.netdotnetzip

DotNetZip ExtractProgress Bug?


The ExtractProgressEventArgs.EntriesTotal and ExtractProgressEventArgs.EntriesExtracted is 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));
}

Solution

  • It's defined behavior:

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

    EntriesExtracted Number of entries extracted so far. This is set only if the EventType is Extracting_BeforeExtractEntry or Extracting_AfterExtractEntry, and the Extract() is occurring witin the scope of a call to ExtractAll().

    The reason for this is simple: If you call Extract() in a loop that you yourself control, there's no way for the library to know how many times you plan to call it, how many times you've called it in a row, whether to count the first 5 times with the next 5 times, and so on. So DotNetZip can't give you reliable informaiton about how many you've done.

    You have two easy workarounds available:

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

    • call ExtractAll(), In that case it will issue ExtractProgressEvents with the EntriesExtracted value set to a meaningful 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");
        }