Performance Zone is brought to you in partnership with:

Graduated in computer science at the University of Oldenburg, Germany. Develops software for over 20 years now. Likes to program in Java. Wrote and contributed Maven plugins and implemented initially Maven support for IntelliJ IDEA. Ralf is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. You can read more from them at their website. View Full User Profile

How to compress and uncompress a Java byte array using deflater/inflater

02.06.2013
| 21422 views |
  • submit to reddit

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();  
   
   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();  
   
   LOG.debug("Original: " + data.length);  
   LOG.debug("Compressed: " + 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.

Published at DZone with permission of Ralf Quebbemann, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Alex Soto replied on Thu, 2013/02/07 - 10:11am

Be aware of using Deflater and not calling .end() method explicitly, because an OutOfMemory error could occurs - http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ 

Ralf Quebbemann replied on Thu, 2013/02/07 - 11:27am in response to: Alex Soto

Alex, thanks for pointing that out. I have corrected the sample code - calling .end() explicitly to avoid OOM errors.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.