Total Pageviews

Tuesday, February 05, 2013

Howto compress and uncompress a Java byte array using JDK Deflater/Inflater

If you ever came across the requirement to store binary data somewhere (e.g. filesystem or database) it might be handy to compress those data.
Besides the common ZIP algorithm Java offers the Deflater and Inflater classes that uses the ZLIB compression library. ZLIB is part of the PNG standard and not protected by any patents.
Here is a small code snippet which shows an utility class that offers two methods to compress and extract a Java byte array.

 package de.qu.compression.demo;  
   
 import java.io.ByteArrayOutputStream;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.util.List;  
 import java.util.Map;  
 import java.util.zip.DataFormatException;  
 import java.util.zip.Deflater;  
 import java.util.zip.Inflater;  
   
 public class CompressionUtils {  
  private static final Logger LOG = Logger.getLogger(CompressionUtils.class);  
    
    
  public static byte[] compress(byte[] data) throws IOException {  
   Deflater deflater = new Deflater();  
   deflater.setInput(data);  
   
   ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);   
       
   deflater.finish();  
   byte[] buffer = new byte[1024];   
   while (!deflater.finished()) {  
    int count = deflater.deflate(buffer); // returns the generated code... index  
    outputStream.write(buffer, 0, count);   
   }  
   outputStream.close();  
   byte[] output = outputStream.toByteArray();  
   
   deflater.end();

   LOG.debug("Original: " + data.length / 1024 + " Kb");  
   LOG.debug("Compressed: " + output.length / 1024 + " Kb");  
   return output;  
  }  
   
  public static byte[] decompress(byte[] data) throws IOException, DataFormatException {  
   Inflater inflater = new Inflater();   
   inflater.setInput(data);  
   
   ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);  
   byte[] buffer = new byte[1024];  
   while (!inflater.finished()) {  
    int count = inflater.inflate(buffer);  
    outputStream.write(buffer, 0, count);  
   }  
   outputStream.close();  
   byte[] output = outputStream.toByteArray();  
   
   inflater.end();
   
   LOG.debug("Original: " + data.length);  
   LOG.debug("Uncompressed: " + output.length);  
   return output;  
  }  
 }  
   

It is also possible to receive better compression results by calling the method setLevel of the Deflater class and specify the constant Deflater.BEST_COMPRESSION.

3 comments:

  1. So, to start, I am trying to implement compression right now, and I found your post very helpful.

    However, I would like to point one thing out:
    You make a call to outputStream.close()

    outputStream is a ByteArrayOutputStream, and per the JavaDoc on that Class, calling close() does nothing.

    I think that it would be best to replace that call to close() with a call to flush(). The JavaDoc says that the general contract of flush is that any buffered (i.e. unwritten) bytes are to be immediately written, and I think that's what you're after. To be fair, close() sounds like it ought to do that (and it was my first guess, as well), but it doesn't.

    Best Regards,
    Jeff

    Reference: http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html

    ReplyDelete
  2. T-Mobile's T-Mobile's T-Mobile's T-Mobile's T-Mobile
    T-Mobile titanium quartz has babyliss pro titanium flat iron expanded its reach as T-Mobile prepares to roll out a mobile mobile-first, mobile titanium scrap price sports titanium granite betting 먹튀 app. The company

    ReplyDelete
  3. Thank you for the update, very nice site.. 먹튀폴리스

    ReplyDelete