Tar archives are plain or compressed. TarArchiveCreator and TarArchiveExtractor handle every variant; the compression is a Compression value, not a separate class.
Example Usage
try (TarArchiveCreator tarCreator = TarArchiveCreator.builder(Path.of("example.tar"))
.blockSize(1024)
.encoding(UTF_8)
.addPaxHeadersForNonAsciiNames(true)
.bigNumberMode(TarBigNumberMode.ERROR)
.longFileMode(TarLongFileMode.GNU)
.filter(s -> !s.name().endsWith("some_file.txt"))
.build()) {
tarCreator.addDirectoryRecursively(Path.of("exampleDir"));
tarCreator.addFile(Path.of("path/to/file.txt"));
}
try (TarArchiveExtractor tarExtractor = TarArchiveExtractor.builder(Path.of("example.tar"))
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, failure) -> entry.name().endsWith(".tmp") ? SKIP : ABORT)
.escapingSymlinkPolicy(EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarExtractor.extract(Path.of("outputDir"));
}
Compression
Pass a Compression to TarArchiveCreator.builder(…).compression(…) to write a compressed tar. The value carries the codec options, such as Compression.gzip().level(9) or Compression.zstd().level(6). Every writable codec in the codec table works except Pack200, and raw Snappy, which needs uncompressedSize(…) that a tar stream rarely knows up front.
The extractor detects the compression from the leading bytes when you set none:
-
gzip, bzip2, xz, Zstandard, framed LZ4, framed Snappy and Unix
.Zhave strong magic numbers and are detected. Concatenated gzip, bzip2, xz and framed LZ4 members are read; the others are not verified for it. -
LZMA has no magic number: call
.compression(Compression.lzma())on the extractor. -
Pack200 is never detected and the tar extractor does not read it.
-
Input with no detected signature reads as plain tar; input that is not a tar archive fails with an
IOException.
An explicit compression(…) on the extractor is used exactly as given.
try (TarArchiveCreator tarGzCreator = TarArchiveCreator.builder(Path.of("example.tar.gz"))
.compression(Compression.gzip()
.bufferSize(1024)
.level(BEST_COMPRESSION)
.comment("comment")
.deflateStrategy(DeflateStrategy.HUFFMAN_ONLY)
.operatingSystem(0))
.longFileMode(TarLongFileMode.POSIX)
.bigNumberMode(TarBigNumberMode.POSIX)
.blockSize(1024)
.encoding(UTF_8)
.addPaxHeadersForNonAsciiNames(true)
.filter(s -> !s.name().endsWith("some_file.txt"))
.build()) {
tarGzCreator.addDirectoryRecursively(Path.of("exampleDir"));
tarGzCreator.addFile(Path.of("path/to/file.txt"));
}
try (TarArchiveExtractor tarGzExtractor = TarArchiveExtractor.builder(Path.of("example.tar.gz"))
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, failure) -> entry.name().endsWith(".tmp") ? SKIP : ABORT)
.escapingSymlinkPolicy(EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarGzExtractor.extract(Path.of("outputDir"));
}
try (TarArchiveCreator tarBzip2Creator = TarArchiveCreator.builder(Path.of("example.tar.bz2"))
.compression(Compression.bzip2().blockSize(9))
.blockSize(1024)
.encoding(UTF_8)
.addPaxHeadersForNonAsciiNames(true)
.bigNumberMode(TarBigNumberMode.ERROR)
.longFileMode(TarLongFileMode.GNU)
.filter(s -> !s.name().endsWith("some_file.txt"))
.build()) {
tarBzip2Creator.addDirectoryRecursively(Path.of("exampleDir"));
tarBzip2Creator.addFile(Path.of("path/to/file.txt"));
}
try (TarArchiveExtractor tarBzip2Extractor = TarArchiveExtractor.builder(Path.of("example.tar.bz2"))
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, failure) -> entry.name().endsWith(".tmp") ? SKIP : ABORT)
.escapingSymlinkPolicy(EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarBzip2Extractor.extract(Path.of("outputDir"));
}
try (TarArchiveCreator tarXzCreator = TarArchiveCreator.builder(Path.of("example.tar.xz"))
.compression(Compression.xz().preset(6))
.blockSize(1024)
.encoding(UTF_8)
.addPaxHeadersForNonAsciiNames(true)
.bigNumberMode(TarBigNumberMode.ERROR)
.longFileMode(TarLongFileMode.GNU)
.filter(s -> !s.name().endsWith("some_file.txt"))
.build()) {
tarXzCreator.addDirectoryRecursively(Path.of("exampleDir"));
tarXzCreator.addFile(Path.of("path/to/file.txt"));
}
try (TarArchiveExtractor tarXzExtractor = TarArchiveExtractor.builder(Path.of("example.tar.xz"))
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, failure) -> entry.name().endsWith(".tmp") ? SKIP : ABORT)
.escapingSymlinkPolicy(EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarXzExtractor.extract(Path.of("outputDir"));
}
try (TarArchiveCreator tarZstdCreator = TarArchiveCreator.builder(Path.of("example.tar.zst"))
.compression(Compression.zstd().level(6))
.blockSize(1024)
.encoding(UTF_8)
.addPaxHeadersForNonAsciiNames(true)
.bigNumberMode(TarBigNumberMode.ERROR)
.longFileMode(TarLongFileMode.GNU)
.filter(s -> !s.name().endsWith("some_file.txt"))
.build()) {
tarZstdCreator.addDirectoryRecursively(Path.of("exampleDir"));
tarZstdCreator.addFile(Path.of("path/to/file.txt"));
}
try (TarArchiveExtractor tarZstdExtractor = TarArchiveExtractor.builder(Path.of("example.tar.zst"))
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, failure) -> entry.name().endsWith(".tmp") ? SKIP : ABORT)
.escapingSymlinkPolicy(EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarZstdExtractor.extract(Path.of("outputDir"));
}
try (TarArchiveCreator tarLzmaCreator = TarArchiveCreator.builder(Path.of("example.tar.lzma"))
.compression(Compression.lzma())
.blockSize(1024)
.encoding(UTF_8)
.addPaxHeadersForNonAsciiNames(true)
.bigNumberMode(TarBigNumberMode.ERROR)
.longFileMode(TarLongFileMode.GNU)
.filter(s -> !s.name().endsWith("some_file.txt"))
.build()) {
tarLzmaCreator.addDirectoryRecursively(Path.of("exampleDir"));
tarLzmaCreator.addFile(Path.of("path/to/file.txt"));
}
try (TarArchiveExtractor tarLzmaExtractor = TarArchiveExtractor.builder(Path.of("example.tar.lzma"))
.compression(Compression.lzma())
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, failure) -> entry.name().endsWith(".tmp") ? SKIP : ABORT)
.escapingSymlinkPolicy(EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarLzmaExtractor.extract(Path.of("outputDir"));
}
try (TarArchiveCreator tarLz4Creator = TarArchiveCreator.builder(Path.of("example.tar.lz4"))
.compression(Compression.lz4Framed())
.blockSize(1024)
.encoding(UTF_8)
.addPaxHeadersForNonAsciiNames(true)
.bigNumberMode(TarBigNumberMode.ERROR)
.longFileMode(TarLongFileMode.GNU)
.filter(s -> !s.name().endsWith("some_file.txt"))
.build()) {
tarLz4Creator.addDirectoryRecursively(Path.of("exampleDir"));
tarLz4Creator.addFile(Path.of("path/to/file.txt"));
}
try (TarArchiveExtractor tarLz4Extractor = TarArchiveExtractor.builder(Path.of("example.tar.lz4"))
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, failure) -> entry.name().endsWith(".tmp") ? SKIP : ABORT)
.escapingSymlinkPolicy(EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarLz4Extractor.extract(Path.of("outputDir"));
}
| Read-only: Compress4J can extract this format but cannot create it. |
try (TarArchiveExtractor tarZExtractor = TarArchiveExtractor.builder(Path.of("example.tar.Z"))
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, failure) -> entry.name().endsWith(".tmp") ? SKIP : ABORT)
.escapingSymlinkPolicy(EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarZExtractor.extract(Path.of("outputDir"));
}