Hello, everyone! I'm using the third-party class called DotNetZip to create zip files of directories in my application, and no matter what I do, it seems like I can't get the progress of the compression process. Here's my code:
HTML Code:
public void CompressDir()
{
DirSize = 0;
// Create and initialize a new Zip file
using (ZipFile zip = new ZipFile())
{
// Read the progress of the saving
zip.SaveProgress += new EventHandler<SaveProgressEventArgs>(zip_SaveProgress);
// Add the directory to the zip file
zip.AddDirectory(Dir);
// Set the Zip file's compression level to best
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
// Save the Zip file
zip.Save(Dir + ".zip");
// Delete the original folder since we now have the zip file
Directory.Delete(Dir, true);
}
}
private void zip_SaveProgress(object sender, SaveProgressEventArgs e)
{
// Add to the amount of bytes we've transferred so far
DirSize += e.BytesTransferred;
// Find out the percentage complete
double ProgressPercent = (DirSize / TotalDirSize) * 100;
ProgressPercent = Math.Round(ProgressPercent, 2); // Round the percentage to two decimal points
DisplayText = Convert.ToString(ProgressPercent);
context.Send((SendOrPostCallback)delegate { info.UpdateProgressTextBox(this, progress); }, null);
}
What I am doing to obtain the progress of the compression is dividing the total size of the directory (
TotalDirSize) by the current amount of data transferred (
DirSize). Then, I multiply this value by 100 to get a percentage, which I then display to the end user.
Unfortunately, it seems that
e.BytesTransferred almost always equals
0. I've displayed the value of it numerous times, and I'm not sure why it's stuck at zero.
As a result, I tried a different approach to getting the progress of the compression. I tried using
e.EntriesSaved and
e.EntriesTotal. Before starting the Save process, I obtained the total number of entries in the zip file after the line,
zip.AddDirectory(Dir);, and I got a value of
112.
Then, I tried dividing 112 by
e.EntriesSaved; however, the value would only come out to 0. I displayed the value of
e.EntriesSaved, and it came out to 0, so that's why I was always getting 0% as my progress completed. So, I decided to try
e.EntriesTotal, but that ended up being 0 all the time as well.
From the looks of it, it seems like DotNetZip's progress indications are flawed and are not complete; however, I could be wrong. Does anyone think he/she could help me out, please? Everything works fine except displaying the progress of the compression. I've been working on this for two days but still couldn't come up with something that works.
EDIT: Figured it out. Thanks.