Apache commons compress | Simplest zip, zip with directory, compression level, unzip

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.)


Project structure (Basic maven project)

  • compress-me –> Folder to compress. Files range from 5 MB to 12 MB.
  • output –> output folder


Simplest zip with single file

This is simplest example with single file in a zip.

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

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

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.

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)


Leave a Reply

Your email address will not be published. Required fields are marked *