Linesofcode
Linesofcode

Reputation: 5891

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?

Upvotes: 0

Views: 3870

Answers (2)

Yacoub Massad
Yacoub Massad

Reputation: 27861

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

Upvotes: 3

Shazi
Shazi

Reputation: 1569

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)

Upvotes: 4

Related Questions