tar
Here is a simple example of how to use Compress4J to create and extract tar files:
Example Usage
Tar Creation
try (TarArchiveCreator tarCreator = TarArchiveCreator.builder(Path.of("example.tar"))
.blockSize(1024)
.encoding(UTF_8.name())
.addPaxHeadersForNonAsciiNames(true)
.bigNumberMode(BIGNUMBER_ERROR)
.longFileMode(LONGFILE_GNU)
.filter((name, p) -> !name.endsWith("some_file.txt"))
.build()) {
tarCreator.addDirectoryRecursively(Path.of("exampleDir"));
tarCreator.addFile(Path.of("path/to/file.txt"));
}
Tar Extraction
try (TarArchiveExtractor tarExtractor = TarArchiveExtractor.builder(Path.of("example.tar"))
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, exception) -> RETRY)
.escapingSymlinkPolicy(ArchiveExtractor.EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarExtractor.extract(Path.of("outputDir"));
}
Tar Gzip Creation
try (TarGzArchiveCreator tarGzCreator = TarGzArchiveCreator.builder(Path.of("example.tar.gz"))
.compressorOutputStreamBuilder()
.bufferSize(1024)
.compressionLevel(BEST_COMPRESSION)
.comment("comment")
.deflateStrategy(HUFFMAN_ONLY)
.operatingSystem(0)
.parentBuilder()
.longFileMode(LONGFILE_POSIX)
.bigNumberMode(BIGNUMBER_POSIX)
.blockSize(1024)
.encoding(UTF_8.name())
.addPaxHeadersForNonAsciiNames(true)
.filter((name, p) -> !name.endsWith("some_file.txt"))
.build()) {
tarGzCreator.addDirectoryRecursively(Path.of("exampleDir"));
tarGzCreator.addFile(Path.of("path/to/file.txt"));
}
Tar Gzip Extraction
try (TarGzArchiveExtractor tarGzExtractor = TarGzArchiveExtractor.builder(Path.of("example.tar.gz"))
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, exception) -> RETRY)
.escapingSymlinkPolicy(ArchiveExtractor.EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarGzExtractor.extract(Path.of("outputDir"));
}
Tar BZip2 Creation
try (TarBZip2ArchiveCreator tarBzip2Creator = TarBZip2ArchiveCreator.builder(Path.of("example.tar.bz2"))
.compressorOutputStreamBuilder()
.blockSize(1024)
.parentBuilder()
.blockSize(1024)
.encoding(UTF_8.name())
.addPaxHeadersForNonAsciiNames(true)
.bigNumberMode(BIGNUMBER_ERROR)
.longFileMode(LONGFILE_GNU)
.filter((name, p) -> !name.endsWith("some_file.txt"))
.build()) {
tarBzip2Creator.addDirectoryRecursively(Path.of("exampleDir"));
tarBzip2Creator.addFile(Path.of("path/to/file.txt"));
}
Tar Bzip2 Extraction
try (TarBZip2ArchiveExtractor tarBzip2Extractor = TarBZip2ArchiveExtractor.builder(Path.of("example.tar.bz2"))
.filter(entry -> !entry.name().startsWith("bad"))
.errorHandler((entry, exception) -> RETRY)
.escapingSymlinkPolicy(ArchiveExtractor.EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {})
.stripComponents(1)
.overwrite(true)
.build()) {
tarBzip2Extractor.extract(Path.of("outputDir"));
}