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

ArchiverFactory.createArchiver(ArchiveFormat.TAR, CompressionType.GZIP)

TarArchiveCreator.builder(path).compression(Compression.gzip()) and TarArchiveExtractor.builder(path)

ArchiverFactory.createArchiver(File) (format from the file name)

None. Choose the class for the format. TarArchiveExtractor detects the compression of a tar from its content.

archiver.create(name, destDir, sources…​)

XArchiveCreator.builder(path).build(), then addFile(path) or addDirectoryRecursively(dir)

archiver.extract(archive, destDir)

XArchiveExtractor.builder(archive).build(), then extract(destDir)

archiver.stream(archive) and ArchiveStream.getNextEntry()

extractor.stream(), a Stream<ArchiveItem>; read item.entry() and item.content()

ArchiveEntry

Entry for the metadata and ArchiveItem for the entry with its content

ArchiveFormat and CompressionType

One extractor class per format, a creator class for each format that can be written (not ARJ or UNIX dump), and Compression for the codec

java.io.File

java.nio.file.Path

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 maxEntrySize or maxTotalSize. A rejected entry fails with UnsafeEntryException or LimitExceededException, 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, errorHandler and postProcessor have no JArchiveLib equivalent.

  • Single files. Compressor and Decompressor handle one compressed file without an archive.