Nikunj Satasiya

Nikunj Satasiya

  • 184
  • 10k
  • 3.7m

Compress File Using Ionic.ZIP with Progress Bar Win forms C#

Jun 5 2019 5:45 AM
Hey Friends, in my company one of my friend finding a solution for one of the issues regarding the zip file. so I post this question with detailed information on behalf of him, he also posted same questions in other developers community such as StackOverflow and etc but still didn't get expected solution, please write any solution for this problem.
 
I'm preparing an application that creates a zip file for a given directory.
 
I want the following things to display while creating a zip.
 
Estimated time for completing that zip (Elapsed Time and Time left)
 
The percentage for completion of Zipping
 
Here is my written code:
  1. private void CreateZip(string FilePath)  
  2. {  
  3.     using (ZipFile zip = new ZipFile())  
  4.     {  
  5.         zip.AddProgress += Zip_AddProgress;  
  6.         zip.SaveProgress += Zip_SaveProgress;  
  7.         zip.CompressionMethod = Ionic.Zip.CompressionMethod.Deflate;  
  8.         zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;  
  9.         zip.UseZip64WhenSaving = Zip64Option.AsNecessary;  
  10.         if (!string.IsNullOrEmpty(FilePath))  
  11.         {  
  12.             zip.AddDirectory(FilePath, new DirectoryInfo(FilePath).Name);  
  13.         }  
  14.        var d= zip;  
  15.   
  16.         if (File.Exists(txtDest.Text))  
  17.         {  
  18.             File.Delete(txtDest.Text);  
  19.         }  
  20.         zip.Save(txtDest.Text);  
  21.     }  
  22. }  
  23.   
  24. private void Zip_SaveProgress(object sender, SaveProgressEventArgs e)  
  25. {  
  26.     if (e.EventType == ZipProgressEventType.Saving_Started)  
  27.         lblFileName.Text = "Proccess Started Successfully";  
  28.   
  29.     if (e.EventType == ZipProgressEventType.Saving_AfterSaveTempArchive)  
  30.         lblFileName.Text = "Proccess Completed Successfully";  
  31.   
  32.   
  33.     if (e.BytesTransferred > 0 && e.TotalBytesToTransfer > 0)  
  34.     {  
  35.         int progress = (int)Math.Floor((decimal)((e.BytesTransferred * 100) / e.TotalBytesToTransfer));  
  36.         pbPerFile.Value = progress;  
  37.         lblPercentagePerFile.Text = Convert.ToString(progress) + "%";  
  38.         lblFileName.Text = "File Name: " + e.CurrentEntry.UncompressedSize;  
  39.         Application.DoEvents();  
  40.     }  
  41.   
  42.     if (e.EntriesSaved > 0 && e.EntriesTotal > 0)  
  43.     {  
  44.         int progress = (int)Math.Floor((decimal)((e.EntriesSaved * 100) / e.EntriesTotal));  
  45.         pbTotalFile.Value = progress;  
  46.         Application.DoEvents();  
  47.         lblTotal.Text = Convert.ToString(progress) + "%";             
  48.     }  
  49. }  
The first progress bar works on the size of the file because of e.BytesTransferred and e.TotalBytesToTransfer is return size in Bytes.
 
But, EntriesSaved and e.EntriesTotal will return the length of the saved entries and a total number of entries that we have added.
 
So I want that second Progress Bar to work based on the size of the Entire Selected files and Compressed File.
 
Your efforts will be appreciated.
 
Thanks...

Answers (1)