In this article we will go through some examples using Apache commons compress to create SevenZip (7zip or 7z) file & then un-seven-zip or unzip 7z file.
Dependency (maven, gradle, ivy etc.)
https://mvnrepository.com/artifact/org.apache.commons/commons-compress
You will also need below dependency in your project otherwise you might get exception java.lang.NoClassDefFoundError: org/tukaani/xz/FilterOptions
https://mvnrepository.com/artifact/org.tukaani/xz
Project structure (Basic maven project)
- compress-me –> Folder to compress. Files range from 5 MB to 12 MB.
- output –> output folder
1 2 3 4 |
07/22/2019 11:11 PM 4,823,568 file-to-compress-1.txt 07/22/2019 11:12 PM 5,358,150 file-to-compress-2.txt 07/24/2019 05:34 PM 12,690,822 some-file-1.txt 07/24/2019 05:33 PM 10,716,300 some-file-2.txt |
Create SevenZip (7z) file from folders, sub folders
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public static void createSevenZipFile() { // Create 7z file. try (SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File("output/sample.7z"))) { File folderToZip = new File("compress-me"); // Walk through files, folders & sub-folders. Files.walk(folderToZip.toPath()).forEach(p -> { File file = p.toFile(); // Directory is not streamed, but its files are streamed into 7z file with // folder in it's path if (!file.isDirectory()) { System.out.println("Seven Zipping file - " + file); try (FileInputStream fis = new FileInputStream(file)) { SevenZArchiveEntry entry_1 = sevenZOutput.createArchiveEntry(file, file.toString()); sevenZOutput.putArchiveEntry(entry_1); sevenZOutput.write(Files.readAllBytes(file.toPath())); sevenZOutput.closeArchiveEntry(); } catch (IOException e) { e.printStackTrace(); } } }); // Complete archive entry addition. sevenZOutput.finish(); } catch (IOException e) { e.printStackTrace(); } } |
1 2 3 4 |
Seven Zipping file - compress-me\file-to-compress-1.txt Seven Zipping file - compress-me\file-to-compress-2.txt Seven Zipping file - compress-me\some_folder\some-file-1.txt Seven Zipping file - compress-me\some_folder\some-file-2.txt |
Unzip or UnSevenZip SevenZip (7z) file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public static void unSevenZipFile() { // Get 7zip file. try (SevenZFile sevenZFile = new SevenZFile(new File("output/sample.7z"))) { SevenZArchiveEntry entry; while ((entry = sevenZFile.getNextEntry()) != null) { File file = new File("output/" + entry.getName()); System.out.println("Un seven zipping - " + file); // Create directory before streaming files. String dir = file.toPath().toString().substring(0, file.toPath().toString().lastIndexOf("\\")); Files.createDirectories(new File(dir).toPath()); // Stream file content byte[] content = new byte[(int) entry.getSize()]; sevenZFile.read(content); Files.write(file.toPath(), content); } } catch (IOException e) { e.printStackTrace(); } } |
1 2 3 4 |
Un seven zipping - output\compress-me\file-to-compress-1.txt Un seven zipping - output\compress-me\file-to-compress-2.txt Un seven zipping - output\compress-me\some_folder\some-file-1.txt Un seven zipping - output\compress-me\some_folder\some-file-2.txt |
Here is the output directory with created 7zip & unzipped folder.
Here is 7z file opened in 7z software.

Further Reading
Apache commons compress | TAR, GZip, BZip2, XZ, Snappy, Deflate Examples | un-GZip, UnTar Examples
Apache commons compress | Simplest zip, zip with directory, compression level, unzip