Zip4j - A Java library for zip files / streams
Thank you
for rating Zip4j as the best Java library for zip files [1, 2, 3, 4]. It has encouraged me to
bring this project to life again after a gap of several years. I tried to add some of the important features that
were requested over this time, and also made the API much more neater. The newer version (> 2.0.0) now supports streams,
which was understandably, one of the most requested feature. If you have any feedback, bugs to report, feature
requests, etc, please open an issue here on GitHub. I will try to address them as soon as I can. I also monitor the
tag zip4j on Stack Overflow.
About
Zip4j is the most comprehensive Java library for zip files or streams. As of this writing, it is the only Java library
which has support for zip encryption, apart from several other features. It tries to make handling zip files/streams
a lot more easier. No more clunky boiler plate code with input streams and output streams. As you can see in the usage
section below, working with zip files can now even be a single line of code, compared to this. I mean no offense
to the Java's built-in zip support. In fact, this library depends on Java's built-in zip code and it would have been
significantly more complicated challenging if I had to write compression logic as well. But lets be honest, working with zip
files or streams can be a lot of boiler plate code. The main goal of this library is to provide a simple API for all
usual actions of a zip file or streams by doing the heavy lifting within the library and not have developers worry about
having to deal with streams, etc. Apart from usability, another important goal of this library is to provide support for
as many zip features as possible, which brings me to:
Features
Create, Add, Extract, Update, Remove files from a zip file
Support for streams (ZipInputStream and ZipOutputStream)
Read/Write password protected zip files and streams
Support for both AES and zip standard encryption methods
Support for Zip64 format
Store (No Compression) and Deflate compression method
Create or extract files from split zip files (Ex: z01, z02,...zip)
Support for Unicode file names and comments in zip
Progress Monitor - for integration into apps and user facing applications
Background
Zip4j was started by me (Srikanth Reddy Lingala) back in 2008/2009, when I realized the lack of support for majority of zip format
features in Java. And also working with zip files was, as mentioned several times above, a lot of boiler plate code,
having to deal with streams (worse still, it was back in the days when there was no try-with-resources in Java). There
was also no comprehensive library which supports zip features. So, I decided to write one, and approximately after a
year, the first version was out. The response was truly overwhelming, and I got a lot of support right from the next
day of release. It was not put on GitHub as git/GitHub was not as popular as it is now. Code was hosted on my website,
as, guess what, a zip file :). And unfortunately, after a year or two after the initial release, life got busy and I was
not able to support Zip4j as much as I wanted to. But the overwhelming encouragement I got over the years made me start working on Zip4j
once again, and makes me support Zip4j as much as I can.
Requirements
JDK 7 or later*
* Zip4j is written on JDK 8, as some of the features (NIO) that Zip4j supports requires features available only in
JDK 8. However, considering the fact that Zip4j is widely used in Android, and to support older versions of Android,
Zip4j supports JDK 7 as well. In cases where the feature/class from JDK 8 is missing, Zip4j falls back to the features
available in JDK 7. In other words, when running on JDK 7, not all features will be supported.
Maven
net.lingala.zip4j
zip4j
2.7.0
Please check the latest version number on Maven Central.
Usage
Creating a zip file with single file in it / Adding single file to an existing zip
new ZipFile("filename.zip").addFile("filename.ext");
Or
new ZipFile("filename.zip").addFile(new File("filename.ext"));
Creating a zip file with multiple files / Adding multiple files to an existing zip
new ZipFile("filename.zip").addFiles(Arrays.asList(new File("first_file"), new File("second_file")));
Creating a zip file by adding a folder to it / Adding a folder to an existing zip
new ZipFile("filename.zip").addFolder(new File("/users/some_user/folder_to_add"));
Since v2.6, it is possible to exclude