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.
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:
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.
Monday, May 16, 2011
Subscribe to: Post Comments (Atom)
I have recently been slacking on content on my blog, between long stressful hours at work and to the wonderful toy that is an iPhone, I have...
I make no claim to be a "computer scientist" or a software "engineer", those titles alone can spark some debate, I regar...
I saw an article (well more of a rant) the other day, by Rob Williams Brain Drain in enterprise Dev . I have to say, I do agree with some o...
This series of posts will be about me getting to grips with JBoss Drools . The reasoning behind it is: SAP bought out my company's curre...
I recently finished 97 Things every programmer should know . Well to be completely honest I did skim over a couple of the 97, but all and al...
> * 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.
you're right I am guilty of jautodoc :)ReplyDelete
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.ReplyDelete
How Synchronization works in Java
Use java.io.File.pathSeparator as path separator.ReplyDelete
Nice, handy tip. I'll update it with File.pathSeparator.ReplyDelete
It is very great that people can get to know about such information.ReplyDelete
Aivivu - đại lý chuyên vé máy bay trong nước và quốc tếReplyDelete
Vé máy bay đi Mỹ
đăng ký về việt nam ở mỹ
chuyến bay từ frankfurt đến hà nội
vé máy bay nga về việt nam
vé máy bay từ anh về việt nam vietnam airlines
vé máy bay từ pháp về việt nam
chuyến bay dành cho chuyên gia
I really like java programming. I keep learning from different sites and your java compression blog is also informative for me.ReplyDelete