|
This version is still in development and is not considered stable yet. For the latest stable version, please use Compress4J 5.x! |
Migrating from JArchiveLib
Compress4J is a maintained successor to JArchiveLib. JArchiveLib picks a format at run time through ArchiverFactory; Compress4J has one creator and extractor class per format, and you name the compression with Compression.
Dependency
// before
implementation("org.rauschig:jarchivelib:<version>")
// after
implementation("com.hominux:compress4j:<version>")
Imports change from org.rauschig.jarchivelib to com.hominux.compress4j.archivers and the format subpackages (tar, zip, sevenz, …). Compression lives in com.hominux.compress4j.compressors.
API mapping
| JArchiveLib | Compress4J |
|---|---|
|
|
|
None. Choose the class for the format. |
|
|
|
|
|
|
|
|
|
One extractor class per format, a creator class for each format that can be written (not ARJ or UNIX dump), and |
|
|
Creating
// JArchiveLib
Archiver archiver = ArchiverFactory.createArchiver(ArchiveFormat.TAR, CompressionType.GZIP);
archiver.create("example", destDir, sourceDir);
// Compress4J
try (TarArchiveCreator creator = TarArchiveCreator.builder(destDir.resolve("example.tar.gz"))
.compression(Compression.gzip())
.build()) {
creator.addDirectoryRecursively(sourceDir);
}
The creator takes the full archive path, extension included. JArchiveLib appended the extension to the name you gave it.
Extracting
// JArchiveLib
Archiver archiver = ArchiverFactory.createArchiver(archive);
archiver.extract(archive, destDir);
// Compress4J
try (TarArchiveExtractor extractor =
TarArchiveExtractor.builder(archive).build()) {
extractor.extract(destDir);
}
For an archive you do not trust, also set maxEntrySize and maxTotalSize on the builder: the defaults leave absolute sizes unlimited. See the limits.
Reading entries
// JArchiveLib
try (ArchiveStream stream = archiver.stream(archive)) {
ArchiveEntry entry;
while ((entry = stream.getNextEntry()) != null) {
System.out.println(entry.getName());
}
}
// Compress4J
try (TarArchiveExtractor extractor =
TarArchiveExtractor.builder(archive).build()) {
return extractor.stream().map(item -> item.entry().name()).toList();
}
An item’s content is readable only while it is the current one. See Streaming entries.
Behavior differences
-
Safety. Extraction rejects path traversal and escaping symlinks, and applies default limits of 1,000,000 entries and an expansion ratio of 100 (checked after the first MiB). Absolute sizes are unlimited until you set
maxEntrySizeormaxTotalSize. A rejected entry fails withUnsafeEntryExceptionorLimitExceededException, which no error handler can suppress. See the limits to tune them. -
Resource handling. Creators and extractors are
Closeable; use try-with-resources. -
Formats. Compress4J adds Zstandard, LZ4, Snappy and others, and reads Brotli, Deflate64, ARJ and UNIX dump. See Compress4J for the list. XZ, LZMA, Zstandard and Brotli need an extra dependency; see Additional Formats.
-
Builder options.
filter,stripComponents,overwrite,errorHandlerandpostProcessorhave no JArchiveLib equivalent. -
Single files.
CompressorandDecompressorhandle one compressed file without an archive.