CPIO
CPIO (Copy In, Copy Out) is a file archiver utility and its associated file format. It is primarily used on Unix-like operating systems for creating archives of files and directories. The CPIO format supports various sub-formats including the new ASCII format, old ASCII format, and old binary format.
Here is a simple example of how to use Compress4J to create and extract CPIO files:
Example Usage
CPIO Creation
try (CpioArchiveCreator cpioCreator = CpioArchiveCreator.builder(Path.of("example.cpio"))
.cpioOutputStream()
.format(CpioConstants.FORMAT_NEW)
.blockSize(1024)
.encoding(UTF_8.name())
.and()
.filter((name, p) -> !name.endsWith("temp.txt"))
.build()) {
// Add files and directories
cpioCreator.addFile("document.txt", Path.of("path/to/document.txt"));
cpioCreator.addDirectory("subdir/", FileTime.from(Instant.now()));
cpioCreator.addFile("subdir/nested.txt", Path.of("path/to/nested.txt"));
// Add directories recursively
cpioCreator.addDirectoryRecursively(Path.of("sourceDir"));
}
CPIO Extraction
try (CpioArchiveExtractor cpioExtractor = CpioArchiveExtractor.builder(Path.of("example.cpio"))
.cpioInputStream()
.blockSize(1024)
.encoding(UTF_8.name())
.and()
.filter(entry -> !entry.name().startsWith("temp"))
.errorHandler((entry, exception) -> RETRY)
.escapingSymlinkPolicy(ArchiveExtractor.EscapingSymlinkPolicy.DISALLOW)
.postProcessor((entry, exception) -> {
// Log successful extraction (in real applications, use a proper logger)
})
.stripComponents(1)
.overwrite(true)
.build()) {
cpioExtractor.extract(Path.of("outputDir"));
}