ZeroTurnaround releases YAZLJ (Yet Another Zip Library for Java)
What, another ZIP abstraction?
During the development of ZeroTurnaround products we have had a growing need to uncompress and compress ZIP archives. For example, JRebel enables users to describe classpath and ServletContext resource locations using a rebel.xml file. Archives listed in the configuration file are automatically extracted into temporary directories. Our other product, LiveRebel detects the deployed applications both in exploded and packaged forms, and sends the application archives to the LiveRebel Command Center. The exploded deployments need to be compressed before any sending can happen.
We spent some time looking at different libraries out there, but couldn’t find a good API that would fit our needs. So we started using the Java SE API instead. Soon we abstracted most of the use cases into our own ZIP API that we named zt-zip, and hosted on GitHub. After a year of using this, we thought that we could kill two stones with one bird: release something handy for other developers out there to use, and also make everything easier to manage and release as an open source project.
Main Features
We needed a library to operate with Java archives such as JAR, WAR and EAR files, in general they are plain old ZIP archives. The main features were:
- pack and unpack directories recursively
- iterate through ZIP entries
In addition the API had to:
- use existing APIs as much as possible
- be simple to use
- be effective to use – no traversing an entire ZIP file if only a single entry is needed
- be safe to use – not enabling users to leave streams open and keep files locked
What about alternatives?
We found the following existing APIs:
- java.util.zip package enables to manage ZIP file entries one-by-one in a low-level fashion. It’s all stream-based and there are no ready-to-use methods for compressing or uncompressing directories.
- Apache Commons Compress is a low-level API like java.util.zip but for other compression formats like tar, gzip, bzip2 etc.
- Chilkat Java Zip Library is another low-level API but with extensions, e.g. making ZIP files executable. The API has tons of different methods. It’s still a low-level API, as the user must take care of closing the ZIP files and streams, and instead of using Java exceptions, the user must manually check for errors. This makes the API very easy to misuse.
Like I mentioned earlier, we could not find an existing library to fit our needs. So we created our own based on the java.util.zip package.
Quick Example
Here’s how a ZIP file is usually created in Java:
File dir = new File("demo"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream("demo.zip")); try { File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; ZipEntry entry = new ZipEntry(file.getName()); entry.setSize(file.length()); entry.setTime(file.lastModified()); out.putNextEntry(entry); FileInputStream in = new FileInputStream(file); try { IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); } out.closeEntry(); } } finally { IOUtils.closeQuietly(out); } |
We had to open and close a ZipOutputStream, iterate through a directory and construct the ZipEntry objects. As you see, it’s easy to accidentally leave streams open. What we can do using zt-zip is just a single method call:
ZipUtil.pack(new File("demo"), new File("demo.zip")); |
Conclusions
Developing zt-zip has been a success as we use it widely in many projects, including our own tests. The best features of the library are:
- It has both basic and some advanced features.
- It is simple to use and it guarantees that no streams are left open by accident.
- It is efficient when only single entries are extracted from ZIP files.
So how would you use it? Let us know what you think at support@zeroturnaround.com, or in the comments section below. The library is available at our Github repository.
-
OlegYch
-
http://twitter.com/reinra Rein
-
OlegYch
-
Yonatan Graber
-
http://www.zeroturnaround.com/ Toomas Römer
-
Yonatan Graber
-
Matteo Luigi Riso
-
http://www.zeroturnaround.com/ Toomas Römer
-
Matteo Luigi Riso
-
Matteo Luigi Riso
-
Rein Raudjärv
-
Rein Raudjärv
