Multithread settings for Unix Archives in Java

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 setCompressionThreads method 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 setCompressionThreads method in XzArchiveSettings. The following code example shows how to compress a file using XzArchive instance.

 1try (FileOutputStream xzFile = new FileOutputStream("archive.xz")) {
 2    try (FileInputStream source = new FileInputStream("data.bin")) {
 3        XzArchiveSettings settings = new XzArchiveSettings();
 4        settings.setCheckType(XzCheckType.Crc32);
 5        settings.setCompressionThreads(4);
 6        try (XzArchive archive = new XzArchive(settings)) {
 7            archive.setSource(source);
 8            archive.save(xzFile);
 9        }
10    }
11} catch (IOException ex) {
12}

Bzip2 multithreaded compression

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

 1try (FileOutputStream bz2File = new FileOutputStream("archive.bz2")) {
 2    try (FileInputStream source = new FileInputStream("data.bin")) {
 3        try (Bzip2Archive archive = new Bzip2Archive()) {
 4            archive.setSource(source);
 5            Bzip2SaveOptions options = new Bzip2SaveOptions(9);
 6            options.setCompressionThreads(4);
 7            archive.save(bz2File, options);
 8        }
 9    }
10} catch (IOException ex) {
11}

Lzip multithreaded compression

There is setCompressionThreads method in LzipArchiveSettings. The following code example shows how to compress a file using LzipArchive instance.

 1try (FileOutputStream lzFile = new FileOutputStream("archive.lz")) {
 2    try (FileInputStream source = new FileInputStream("data.bin")) {
 3        LzipArchiveSettings settings = new LzipArchiveSettings(16777216);
 4        settings.setCompressionThreads(4);
 5        try (LzipArchive archive = new LzipArchive(settings)) {
 6            archive.setSource(source);
 7            archive.save(lzFile);
 8        }
 9    }
10} catch (IOException ex) {
11}

Four threads are used in all samples.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.