Overview
While most compression algorithms were originally designed for single-core processors, the multi-core era offers significant potential for speedup. Aspose.ZIP leverages this by providing multi-core optimization for certain archive formats.Parallelizing BZIP2, LZIP, and XZ Archives
Parallelization is enabled for certain archive formats due to their inherent block-based nature. Aspose.ZIP leverages this by employing a common multi-core compression approach at the program level. Users can control the number of threads used for compression via the setCompressionThreads(int) method. When this value is set to greater than one, multiple CPU cores will be utilized for faster compression.
Parallel compression to BZIP2
try (Bzip2Archive archive = new Bzip2Archive()) {
archive.setSource("data.bin");
Bzip2SaveOptions options = new Bzip2SaveOptions();
options.setCompressionThreads(4);
archive.save("archive.bz2", options);
}
Parallel compression to XZ
XzArchiveSettings settings = new XzArchiveSettings();
settings.setCompressionThreads(4);
try (XzArchive archive = new XzArchive(settings)) {
archive.setSource("data.bin");
archive.save("archive.xz");
}
Parallel compression to LZ
LzipArchiveSettings settings = new LzipArchiveSettings(16777216);
settings.setCompressionThreads(4);
try (LzipArchive archive = new LzipArchive(settings)) {
archive.setSource("data.bin");
archive.save("archive.lz");
}
In the Example 1 demonstrates creating a Bzip2 archive using the Bzip2Archive class. First, we specify the source file to be compressed using the archive.setSource("data.bin") method. Next, various compression options, including the number of compression streams, can be configured using the Bzip2SaveOptions object. Finally, the archive is saved with the specified options using the archive.save("archive.bz2", options) method.
In the second example, we configure the compression parameters for the xz format before creating the archive. This configuration is done using the XzArchiveSettings object, where we specify the desired number of compression streams. Then, a new XzArchive is created with these settings passed as an argument. Following that, the source file ("data.bin") is set as the source for compression, and the archive is saved using the archive.save("archive.xz") method.
In the third example, we demonstrate creating an LZ archive using the LzipArchiveSettings object. First, we set the size of the dictionary used by LZMA compression. Next, we set the number of threads to 4. After that, we create a LzipArchive object and pass the LzipArchiveSettings object to the constructor. Finally, we specify the file to be archived and save the archive.
LZMA2 Compression and Multithreading in 7Z Archives
One of the compression methods within the 7z format, LZMA2 , supports multithreaded compression. Similar to previous archive formats, you can control the number of threads used with the setCompressionThreads(int) method.
SevenZipLZMA2CompressionSettings compSettings = new SevenZipLZMA2CompressionSettings();
compSettings.setCompressionThreads(4);
SevenZipEntrySettings settings = new SevenZipEntrySettings(compSettings);
try (SevenZipArchive archive = new SevenZipArchive(settings)) {
archive.createEntry("first.bin", "data.bin");
archive.save("result.7z");
}
- Compression Settings: We begin by creating compression settings using the SevenZipLZMA2CompressionSettings object. In this example, the number of compression streams is set to 4.
- Entry Settings: Next, a SevenZipEntrySettings object is created and assigned the compression settings we defined earlier. This step configures compression for each individual record within the archive.
- Creating the Archive: With all the settings in place, we create a SevenZipArchive object and pass it the entry settings. This archive will contain a single entry named "first.bin" that corresponds to the source file "data.bin".
- Saving the Archive: Finally, the created archive is saved as "result.7z" using the save("result.7z") method.
Speed Up ZIP Archiving with Parallel Compression
Parallelization for faster processing has been implemented at the block or algorithm level for previous archive formats in Aspose.ZIP. However, ZIP archives offer a unique advantage: entry-level parallelization during saving. To enable this, instantiate a ParallelOptions object when saving the ZIP archive.
try (Archive archive = new Archive()) {
archive.createEntries("C:\\Data");
ParallelOptions parOptions = new ParallelOptions();
parOptions.setParallelCompressInMemory(ParallelCompressionMode.Auto);
ArchiveSaveOptions options = new ArchiveSaveOptions();
options.setParallelOptions(parOptions);
archive.save("archive.zip", options);
}
This code utilizes parallel compression to optimize archive creation and creates a ZIP archive named "archive.zip" from all files within the "C:\Data" folder.
Other Supported Aspose.ZIP for Java API Features
With the Aspose.ZIP library in Java, you can seamlessly handle various tasks related to ZIP file documents. Whether you need to convert formats, merge files, edit contents, or extract data, Aspose.ZIP provides comprehensive functionality to streamline your workflow and enhance productivity.
Support and Learning Resources
- Learning Resources
- Documentation
- Source Code
- API References
- Tutorial Videos
- Product Support
- Free Support
- Paid Support
- Blog
- Release Notes
- Why Aspose.ZIP for Java?
- Customers List
- Success Stories