In this article we will check go through examples to zip/unzip using Apache commons compress.
Examples in this article:
- Simplest zip file with single file.
- Simple zip with files, directory & sub directory or sub folders.
- Customize compression level, compression method, comments etc.
- Unzip zip file with files & directories.
Dependency (maven, gradle, ivy etc.)
https://mvnrepository.com/artifact/org.apache.commons/commons-compress
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 |
Simplest zip with single file
This is simplest example with single file in a zip.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static void createSimplestZipFile() { // Create zip file stream. try (ZipArchiveOutputStream archive = new ZipArchiveOutputStream(new FileOutputStream("output/samplest.zip"))) { // Add file to zip archive File file_1 = new File("compress-me/file-to-compress-1.txt"); ZipArchiveEntry entry_1 = new ZipArchiveEntry(file_1, "file-to-compress-1.txt"); archive.putArchiveEntry(entry_1); IOUtils.copy(new FileInputStream(file_1), archive); archive.closeArchiveEntry(); // Complete archive entry addition. archive.finish(); } catch (IOException e) { e.printStackTrace(); } } |
Below screenshot shows zip file created with compressed file size.
Here is a zip file opened with compression software.
Zip with files, directory & sub directories
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 createZipFile() { // Create zip file stream. try (ZipArchiveOutputStream archive = new ZipArchiveOutputStream(new FileOutputStream("output/sample.zip"))) { 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 zip file with // folder in it's path if (!file.isDirectory()) { System.out.println("Zipping file - " + file); ZipArchiveEntry entry_1 = new ZipArchiveEntry(file, file.toString()); try (FileInputStream fis = new FileInputStream(file)) { archive.putArchiveEntry(entry_1); IOUtils.copy(fis, archive); archive.closeArchiveEntry(); } catch (IOException e) { e.printStackTrace(); } } }); // Complete archive entry addition. archive.finish(); } catch (IOException e) { e.printStackTrace(); } } |
1 2 3 4 |
Zipping file - compress-me\file-to-compress-1.txt Zipping file - compress-me\file-to-compress-2.txt Zipping file - compress-me\some_folder\some-file-1.txt Zipping file - compress-me\some_folder\some-file-2.txt |
Below screenshot shows zip file created with compressed file size.
Zip opened with software with flat view (All folders sub folders showing in flat structure.)
Zip file with custom compression level & other customization
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 32 33 34 35 36 37 38 39 |
public static void createCustomLevelMethodZipFile() { // Create zip file stream. try (ZipArchiveOutputStream archive = new ZipArchiveOutputStream( new FileOutputStream("output/sample-fast.zip"))) { // Set compression level, method & comments. archive.setLevel(Deflater.BEST_SPEED); archive.setMethod(ZipEntry.DEFLATED); archive.setComment("Created using Java program."); 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 zip file with // folder in it's path if (!file.isDirectory()) { System.out.println("Zipping file - " + file); ZipArchiveEntry entry_1 = new ZipArchiveEntry(file, file.toString()); // Add comment to entry entry_1.setComment("File created using Java program."); try (FileInputStream fis = new FileInputStream(file)) { archive.putArchiveEntry(entry_1); IOUtils.copy(fis, archive); archive.closeArchiveEntry(); } catch (IOException e) { e.printStackTrace(); } } }); // Complete archive entry addition. archive.finish(); } catch (IOException e) { e.printStackTrace(); } } |
In below screenshot you can see that the compression method & comment is showing up when zip is opened in software.
Unzip zip file with files, directories, sub directories
We will use zip file created in above code to unzip. We will also print some attribtues of compressed file entries.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public static void unZipFile() { // Create zip file stream. try (ZipArchiveInputStream archive = new ZipArchiveInputStream( new BufferedInputStream(new FileInputStream("output/sample-fast.zip")))) { ZipArchiveEntry entry; while ((entry = archive.getNextZipEntry()) != null) { // Print values from entry. System.out.println(entry.getName()); System.out.println(entry.getMethod()); // ZipEntry.DEFLATED is int 8 File file = new File("output/" + entry.getName()); System.out.println("Unzipping - " + 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 IOUtils.copy(archive, new FileOutputStream(file)); } } catch (IOException e) { e.printStackTrace(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
compress-me/file-to-compress-1.txt 8 Unzipping - output\compress-me\file-to-compress-1.txt compress-me/file-to-compress-2.txt 8 Unzipping - output\compress-me\file-to-compress-2.txt compress-me/some_folder/some-file-1.txt 8 Unzipping - output\compress-me\some_folder\some-file-1.txt compress-me/some_folder/some-file-2.txt 8 Unzipping - output\compress-me\some_folder\some-file-2.txt |
Below is unzip directory.
Further Reading
Apache commons compress | TAR, GZip, BZip2, XZ, Snappy, Deflate examples | un-GZip, UnTar examples
Apache commons compress | SevenZip unSevenZip Examples (.7z)