You are viewing the documentation for a prerelease version.
View Latest
7z
7z is the archive format of the 7-Zip utility. Compress4J creates 7z archives and extracts them, including password-protected ones.
Extraction goes through the same guards as every other format: path traversal is rejected, and escapingSymlinkPolicy, maxEntries, maxEntrySize and maxTotalSize apply.
Example Usage
7z Creation
try (SevenZArchiveCreator sevenZCreator =
SevenZArchiveCreator.builder(Path.of("example.7z")).build()) {
sevenZCreator.addFile(Path.of("path/to/file.txt"));
sevenZCreator.addDirectoryRecursively(Path.of("sourceDir"));
}
7z Extraction
try (SevenZArchiveExtractor sevenZExtractor = SevenZArchiveExtractor.builder(Path.of("example.7z"))
.overwrite(true)
.stripComponents(1)
.filter(entry -> entry.name().endsWith(".txt"))
.errorHandler((entry, exception) -> SKIP)
.escapingSymlinkPolicy(ArchiveExtractor.EscapingSymlinkPolicy.DISALLOW)
.build()) {
sevenZExtractor.extract(Path.of("outputDir"));
}
Extracting a password-protected archive
try (SevenZArchiveExtractor sevenZExtractor = SevenZArchiveExtractor.builder(Path.of("encrypted.7z"))
.password("secret".toCharArray())
.maxEntries(10_000)
.maxTotalSize(1024L * 1024 * 1024)
.build()) {
sevenZExtractor.extract(Path.of("outputDir"));
}
Notes
-
The creator writes to a file path only, because 7z must seek back to patch its header.
-
The creator does not encrypt. Only extraction accepts a password.
-
A symbolic link is stored the way p7zip stores it, as a Unix-mode entry whose content is the link target, and is restored as a symlink on extraction.
-
A wrong or missing password fails with an
IOException.
Want to help? Learn how to contribute to the Compress4J docs ›