Search code examples
c#dotnetzip

C# DotNetZip progressbar on directory


I have the following code working for a single file zip without any problems. But when I zip (create) from a directory the progressbar goes nuts.

Basically, the progressbar goes back and forward nonstop. The image illustrates. enter image description here

Inside the folder selected I might have like another 10 sub-folders.

using (ZipFile zip = new ZipFile())
{
    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    zip.SaveProgress += zipProgress;

    zip.AddDirectory(folderPath);
    zip.Save(tempPath);
}

private void zipProgress(object sender, SaveProgressEventArgs e)
{
    if (e.EventType == ZipProgressEventType.Saving_EntryBytesRead)
        this.progressbar1.Value = (int)((e.BytesTransferred * 100) / e.TotalBytesToTransfer);

    else if (e.EventType == ZipProgressEventType.Saving_Completed)
        this.progressbar1.Value = 100;
}

I do realize that the fact of progress value continuously goes forward and back is due to the fact that I'm zipping one folder that contains 10 sub-folders.

However i would like to know if there's any way of properly show the progressbar for folder zipping?


Solution

  • Building on @Shazi answer, one solution is to depend on the event that has the type Saving_AfterWriteEntry which happens after writing each entry like this:

    if(e.EventType == ZipProgressEventType.Saving_AfterWriteEntry)
    {
        this.progressbar1.Value = e.EntriesSaved * 100 / e.EntriesTotal;
    }
    

    The problem with such solution is that it treats big files as small files, so the progress bar will have different speeds of progressing at different times (depending on the difference between file sizes).