Reporting progress of compression

Progress Event Handler

The compression process may take a long time especially if the data size is huge. For zip archive, there is a CompressionProgressed event to stay posted. This event relies on ProgressEventArgs which contains the number of proceeded bytes so far. This is how we can subscribe to this event using lambda extression:

1   entry.CompressionProgressed += (s, e) => { 
2      Console.WriteLine(string.Format("{0} bytes compressed", e.ProceededBytes)); 
3   };

Reporting Zip Progress Percentage

Each time the CompressionProgressed event raises we divide the ProceededBytes number by the length of the original file. Such we find the ratio of compressed bytes at the moment. Here is the full sample.

Steps: Reporting Zip Progress Percentage in C#

  1. Open the huge.bin file in read mode using File.Open to prepare it for compression.
  2. Initialize a new instance of the Archive class, specifying PPMdCompressionSettings for the compression.
  3. Create an archive entry for the huge.bin file using the CreateEntry method, linking it to the source file stream.
  4. Attach an event handler to the CompressionProgressed event to monitor the progress of compression. It calculates the percentage of the file compressed so far.
  5. Within the event handler, update the compression percentage and display it in the console whenever the progress increases.
  6. Once the compression is complete, save the archive to the specified zipFile.
 1   using (FileStream source = File.Open("huge.bin", FileMode.Open, FileAccess.Read))
 2   {
 3      using (var archive = new Archive(new ArchiveEntrySettings(new PPMdCompressionSettings())))
 4      {
 5         ArchiveEntry entry = archive.CreateEntry("huge.bin", source);
 6         int percentReady = 0;
 7         entry.CompressionProgressed += (s, e) =>
 8               {
 9                  int percent = (int)((100 * (long)e.ProceededBytes) / source.Length);
10                  if (percent > percentReady)                 
11                  {
12                        Console.WriteLine(string.Format("{0}% compressed", percent));
13                        percentReady = percent;
14                  }       
15               };
16         archive.Save(zipFile);
17      }
18   }

Reporting 7z Progress Percentage

Similar approach is for 7z archive. Its entry has own CompressionProgressed event

Steps: Monitoring Compression Progress with LZMA2 in a 7z Archive in C#

  1. Open the source file (huge.bin) in read mode.
  2. Create a SevenZipArchive object using LZMA2 compression settings.
  3. Add the source file to the archive as an entry using the CreateEntry method.
  4. Subscribe to the CompressionProgressed event to track and calculate the compression progress.
  5. For every progress update, calculate the percentage of compression completed.
  6. Save the archive to the specified destination (zipFile).
 1   using (FileStream source = File.Open("huge.bin", FileMode.Open, FileAccess.Read))
 2   {                
 3      using (var archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMA2CompressionSettings())))
 4      {
 5         SevenZipArchiveEntry entry = archive.CreateEntry("huge.bin", source);
 6         int percentReady = 0;
 7         entry.CompressionProgressed += delegate (object s, ProgressEventArgs e) 
 8         {
 9               // s is SevenZipArchiveEntry
10               int percent = (int)((100 * (long)e.ProceededBytes) / source.Length);
11               if (percent > percentReady)
12               {                               
13                  percentReady = percent;      
14               }
15         };
16         archive.Save(zipFile);
17      }
18   }
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.