Data compression in Java

In this tutorial, we'll be looking at how to compress data in Java using the built-in compression library. The standard JDK includes the Deflater class for general-purpose compression. This is an implementation of the DEFLATE algorithm (actually a wrapper around the commonly-used zlib library), which can reduce many types of data to between 20% and 50% of its original size. We'll look at what types of data it favours in a moment.

If you need data compression and you have little development time, then passing your data through Deflater will give some compression for many types of data. On the next page, we'll delve straight in and look at how to use Deflater to compress data in Java "out of the box".

For more advanced cases, it can help to know a little about how the compression algorithm works. We'll take an how the Deflater works in broad terms.

Then we'll also see how to apply this knowledge: if you have little bit more development time available, it may be possible to transform your input data to improve the compression ratio offered by the Deflater class. We look at the case of imroving text compression by using the FILTERED strategy on pre-transformed data.


Written by Neil Coffey. Copyright © Javamex UK 2008. All rights reserved.