Have you ever had the requirement to compress or uncompress files or byte streams from within your java applications? This requirement seems to be a common need for me in all sorts of applications from B2B data feeds to my client applications. Within my client applications I allow users to attach files to records and I compress them to conserve space before storing them on the server. Once I compress the byte stream, I usually do one of two things. I will either store the byte stream representing the compressed file directly into a BLOB column in my database, or I will write it to a file in a common file system location.

Below I will present my simple ZipUtil java class which allows us to easily compress and uncompress files. My class consists of two convenience methods that take a byte array and call either the java.util.zip.Deflater or java.util.zip.Inflater objects to compress/decompress the byte array, and then it returns the new byte array. These java classes provide support for general purpose compression/decompression using the ZLIB compression library.

This class is not designed to create Zip files per se, but rather to just compress and uncompress byte streams. I will present the class to you below: continue reading…