0

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?

Linesofcode
  • 5,327
  • 13
  • 62
  • 116
  • Check out the documentation for the AddProgress event: http://dotnetzip.herobo.com/DNZHelp/html/842e7449-867a-2bed-dac5-4b7ac456d2ed.htm . SaveProgress reports the progress of every entry added to the zip – sotn0r Dec 16 '15 at 22:23

2 Answers2

3

It's because "e.BytesTransferred" is the bytes transferred per entry not the total.

Check out the docs

"The number of bytes read or written so far for this entry."

Made the exact same mistake myself :) (although I was using it to extract files)

Shazi
  • 1,490
  • 1
  • 10
  • 22
2

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).

Community
  • 1
  • 1
Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62