Reporting Compression Progress

Overview

Compression may take noticeable time when the source data is large or when stronger codecs are used. Aspose.ZIP for .NET exposes CompressionProgressed events so your application can update a progress bar, log processed bytes, or estimate the remaining time.

Each handler receives ProgressEventArgs. Its ProceededBytes property reports how many source bytes have been processed so far. In most applications, the progress percentage is calculated by dividing ProceededBytes by the original source length.

Aspose.ZIP uses two compression-progress models:

For ZIP, 7z, and XAR, compression progress is reported per entry. If you save several entries and need one archive-wide percentage, aggregate the source lengths yourself and combine the values from all entry handlers.

Track ZIP Entry Compression Progress

ZIP entries expose the CompressionProgressed event directly on ArchiveEntry.

 1    using (FileStream zipFile = File.Open("result.zip", FileMode.Create))
 2    using (FileStream source = File.Open("huge.bin", FileMode.Open, FileAccess.Read))
 3    using (Archive archive = new Archive(new ArchiveEntrySettings(new PPMdCompressionSettings())))
 4    {
 5        ArchiveEntry entry = archive.CreateEntry("huge.bin", source);
 6        int percentReady = 0;
 7
 8        entry.CompressionProgressed += (s, e) =>
 9        {
10            int percent = (int)((100 * (double)e.ProceededBytes) / source.Length);
11            if (percent > percentReady)
12            {
13                Console.WriteLine(string.Format("{0}% compressed", percent));
14                percentReady = percent;
15            }
16        };
17
18        archive.Save(zipFile);
19    }

Track 7z Entry Compression Progress

7z archives report progress through the CompressionProgressed event of SevenZipArchiveEntry.

 1    using (FileStream sevenZipFile = File.Open("result.7z", FileMode.Create))
 2    using (FileStream source = File.Open("kennedy.xls", FileMode.Open, FileAccess.Read))
 3    using (SevenZipArchive archive = new SevenZipArchive(
 4        new SevenZipEntrySettings(new SevenZipLZMA2CompressionSettings())))
 5    {
 6        SevenZipArchiveEntry entry = archive.CreateEntry("kennedy.xls", source);
 7        int percentReady = 0;
 8
 9        entry.CompressionProgressed += (s, e) =>
10        {
11            int percent = (int)((100 * (double)e.ProceededBytes) / source.Length);
12            if (percent > percentReady)
13            {
14                Console.WriteLine(string.Format("{0}% compressed", percent));
15                percentReady = percent;
16            }
17        };
18
19        archive.Save(sevenZipFile);
20    }

Track XAR Entry Compression Progress

XAR also exposes CompressionProgressed on the created entry. In this case the sender is XarFileEntry, so you can use its Length property when calculating percentage.

 1    using (FileStream xarFile = File.Open("result.xar", FileMode.Create))
 2    using (FileStream source = File.Open("alice29.txt", FileMode.Open, FileAccess.Read))
 3    using (XarArchive archive = new XarArchive())
 4    {
 5        XarFileEntry entry = (XarFileEntry)archive.CreateEntry(
 6            "alice29.txt", source, new XarZlibCompressionSettings());
 7        int percentReady = 0;
 8
 9        entry.CompressionProgressed += (s, e) =>
10        {
11            int percent = (int)(100 * ((double)e.ProceededBytes / ((XarFileEntry)s).Length));
12            if (percent > percentReady)
13            {
14                Console.WriteLine(string.Format("{0}% compressed", percent));
15                percentReady = percent;
16            }
17        };
18
19        archive.Save(xarFile);
20    }

Track BZIP2 Compression Progress

BZIP2 compression reports progress through Bzip2SaveOptions. This pattern is used for single-stream compressors where the archive has one data source rather than multiple entries.

 1    using (FileStream bzip2File = File.Open("result.bz2", FileMode.Create))
 2    using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
 3    {
 4        Bzip2SaveOptions options = new Bzip2SaveOptions(1);
 5        int percentReady = 0;
 6
 7        options.CompressionProgressed += (s, e) =>
 8        {
 9            int percent = (int)((100 * (double)e.ProceededBytes) / source.Length);
10            if (percent > percentReady)
11            {
12                Console.WriteLine(string.Format("{0}% compressed", percent));
13                percentReady = percent;
14            }
15        };
16
17        using (Bzip2Archive archive = new Bzip2Archive())
18        {
19            archive.SetSource(source);
20            archive.Save(bzip2File, options);
21        }
22    }

Track LZMA Compression Progress

LZMA uses CompressionProgressed from LzmaArchiveSettings.

 1    using (FileStream lzmaFile = File.Open("result.lzma", FileMode.Create))
 2    using (FileStream source = File.Open("alice29.txt", FileMode.Open, FileAccess.Read))
 3    {
 4        LzmaArchiveSettings settings = new LzmaArchiveSettings();
 5        int percentReady = 0;
 6
 7        settings.CompressionProgressed += (s, e) =>
 8        {
 9            int percent = (int)((100 * (double)e.ProceededBytes) / source.Length);
10            if (percent > percentReady)
11            {
12                Console.WriteLine(string.Format("{0}% compressed", percent));
13                percentReady = percent;
14            }
15        };
16
17        using (LzmaArchive archive = new LzmaArchive(settings))
18        {
19            archive.SetSource(source);
20            archive.Save(lzmaFile);
21        }
22    }

Track Unix Z Compression Progress

Classic Unix compress format uses ZArchiveSaveOptions for progress reporting.

 1    using (FileStream source = File.Open("alice29.txt", FileMode.Open, FileAccess.Read))
 2    {
 3        ZArchiveSaveOptions options = new ZArchiveSaveOptions();
 4        int percentReady = 0;
 5
 6        options.CompressionProgressed += (s, e) =>
 7        {
 8            int percent = (int)((100 * (double)e.ProceededBytes) / source.Length);
 9            if (percent > percentReady)
10            {
11                Console.WriteLine(string.Format("{0}% compressed", percent));
12                percentReady = percent;
13            }
14        };
15
16        using (ZArchive archive = new ZArchive())
17        {
18            archive.SetSource(source);
19            archive.Save("result.Z", options);
20        }
21    }

Track Zstandard Compression Progress

Zstandard follows the same single-stream model through ZstandardSaveOptions.

 1    using (FileStream source = File.Open("alice29.txt", FileMode.Open, FileAccess.Read))
 2    using (FileStream zstdFile = File.Open("result.zst", FileMode.Create))
 3    {
 4        ZstandardSaveOptions options = new ZstandardSaveOptions();
 5        int percentReady = 0;
 6
 7        options.CompressionProgressed += (s, e) =>
 8        {
 9            int percent = (int)((100 * (double)e.ProceededBytes) / source.Length);
10            if (percent > percentReady)
11            {
12                Console.WriteLine(string.Format("{0}% compressed", percent));
13                percentReady = percent;
14            }
15        };
16
17        using (ZstandardArchive archive = new ZstandardArchive())
18        {
19            archive.SetSource(source);
20            archive.Save(zstdFile, options);
21        }
22    }

All of these samples calculate progress from ProceededBytes, but the event source differs by format:

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.