Multithread settings for Unix Archives in C#

Overview

XZ, Bzip2 and Lzip are common in Linux. Due to their block structure they can be compressed in several threads. Aspose.ZIP API provides CompressionThreads properties for all of them to utilize several CPU cores on compression. Setting it to more than one entails multi core compression; leaving it one - the default option - leads to compression with single CPU core.

XZ multithreaded compression

There is CompressionThreads property in XzArchiveSettings. The following code example shows how to compress a file using XzArchive instance.

Steps: Create Multi-Threaded Compression XZ Archive by C#

  1. Open a file stream for the XZ archive using FileMode.Create to write the compressed data.
  2. Open the source file (data.bin) using FileMode.Open and set it as the data to be compressed.
  3. Create an instance of XzArchiveSettings, specifying:
    • CheckType as Crc32 for integrity verification.
    • CompressionThreads set to 4 for multi-threaded compression.
  4. Create an instance of XzArchive, passing the settings created above.
  5. Use SetSource to assign the source file for compression.
  6. Save the compressed XZ archive using the Save method
 1using (FileStream xzFile = File.Open("archive.xz", FileMode.Create))
 2{
 3    using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
 4    {
 5        var settings = new XzArchiveSettings() { CheckType = XzCheckType.Crc32, CompressionThreads = 4 };
 6        using (var archive = new XzArchive(settings))
 7        {
 8            archive.SetSource(source);
 9            archive.Save(xzFile);
10        }
11    }
12}

Bzip2 multithreaded compression

There is CompressionThreads property in Bzip2SaveOptions. The following code example shows how to compress a file using Bzip2Archive instance.

Steps: Create Bzip2 Archive with Maximum Compression and Multi-Threading by C#

  1. Open a file stream for the Bzip2 archive using FileMode.Create to store the compressed output.
  2. Open the source file (data.bin) with FileMode.Open and FileAccess.Read for reading the uncompressed data.
  3. Create an instance of Bzip2Archive and use the SetSource method to assign the source file for compression.
  4. Use the Save method to compress the data into the Bzip2 archive, passing:
    • Bzip2SaveOptions(9) to set the maximum compression level.
    • Set CompressionThreads to 4 for multi-threaded compression.
 1    using (FileStream bz2File = File.Open("archive.bz2", FileMode.Create))
 2    {
 3        using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
 4        {
 5            using (Bzip2Archive archive = new Bzip2Archive())
 6            {
 7                archive.SetSource(source);
 8                archive.Save(bz2File, new Bzip2SaveOptions(9) { CompressionThreads = 4 });
 9            }
10        }
11    }

Lzip multithreaded compression

There is CompressionThreads property in XzArchiveSettings. The following code example shows how to compress a file using XzArchive instance.

Steps: Create Lzip Archive with Custom Dictionary Size and Multi-Threading by C#

  1. Open a file stream for the Lzip archive using FileMode.Create to create the compressed output file.
  2. Open the source file (data.bin) with FileMode.Open and FileAccess.Read for reading the uncompressed data.
  3. Create an instance of LzipArchiveSettings, specifying a dictionary size of 16777216 bytes (16 MB) and enabling multi-threading with CompressionThreads = 4.
  4. Create an instance of LzipArchive with the settings defined above.
  5. Use SetSource to assign the source file for compression.
  6. Compress and save the archive using the Save method, passing the output Lzip file stream.
 1    using (FileStream lzFile = File.Open("archive.lz", FileMode.Create))
 2    {
 3        using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
 4        {
 5            var settings = new LzipArchiveSettings(16777216) { CompressionThreads = 4 }
 6            using (LzipArchive archive = new LzipArchive(settings))
 7            {
 8                archive.SetSource(source);
 9                archive.Save(lzFile);
10            }
11        }
12    }

Four threads are used in all samples.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.