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#
- Open the huge.bin file in read mode using File.Open to prepare it for compression.
- Initialize a new instance of the Archive class, specifying PPMdCompressionSettings for the compression.
- Create an archive entry for the huge.bin file using the CreateEntry method, linking it to the source file stream.
- Attach an event handler to the CompressionProgressed event to monitor the progress of compression. It calculates the percentage of the file compressed so far.
- Within the event handler, update the compression percentage and display it in the console whenever the progress increases.
- 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#
- Open the source file (huge.bin) in read mode.
- Create a SevenZipArchive object using LZMA2 compression settings.
- Add the source file to the archive as an entry using the CreateEntry method.
- Subscribe to the CompressionProgressed event to track and calculate the compression progress.
- For every progress update, calculate the percentage of compression completed.
- 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 }