Monday, May 16, 2011

Java Compression.

In a recent project, we had to do something I had personally never really had to look at; Compression. We needed to take a couple files and images, zip them up and make them available for FTP, and yes somedays it does feel like we are back in the 90's. Besides the FTP trip into the past its was good opportunity to spend a little bit of time on the subject.

Compressing Files
So above the usual IO classes BufferedInputStream, FileOutputStream and File there are:
ZipInputStream - An input stream for reading files in the ZIP file format. Zip entries are not cached, unlike ZipFile.
ZipOutputStream - An output stream for writing files in the ZIP file format. This has a default internal buffer of 512, a BufferedOutputStream can be used to increase this.
ZipEntry - Represents an entry int a zip file.
ZipFile - Used to read entries from a zip file. The entries are cached.
CRC32 - Used to compute the CRC-32 of a data stream.


Below is an example showing how to compress and decompress files in a folder, with and without a checksum:


Compressing Objects
We didn't end up using object compression but I had a look at it anyways. I did a little generic compress / expand util, don't know if it will ever be useful. I left the input params as OutputStream and InputStream as this could theoretically be used with any stream implementation from socket communication to string manipulation.

The compression related classes being used here:
GZIPInputStream - An input stream filter for reading compressed data in the GZIP file format.
GZIPOutputStream - An output stream filter for writing compressed data in the GZIP file format. Default internal buffer of 512, use BufferedOutputStream if you require more.

8 comments:

  1. > /**
    > * Zip files in path.
    > * @param zipFileName the zip file name
    > * @param filePath the file path
    > * @throws IOException Signals that an I/O exception has occurred.

    Most useless comments ever.

    ReplyDelete
  2. you're right I am guilty of jautodoc :)

    http://jautodoc.sourceforge.net/

    ReplyDelete
  3. zipping is nice thing I remember on one of my project we have client server architecture and server transfer huge messages to client via network but zipping those message and then transferring we had improved performance and latency significantly.

    Javin
    How Synchronization works in Java

    ReplyDelete
  4. Use java.io.File.pathSeparator as path separator.

    ReplyDelete
  5. Nice, handy tip. I'll update it with File.pathSeparator.

    ReplyDelete
  6. It is very great that people can get to know about such information.

    ReplyDelete
  7. I really like java programming. I keep learning from different sites and your java compression blog is also informative for me.

    ReplyDelete

Popular Posts

Followers