Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Compress and Uncompress a Java Byte Array Using Deflater/Inflater

DZone's Guide to

How to Compress and Uncompress a Java Byte Array Using Deflater/Inflater

Here is a small code snippet which shows an utility class that offers two methods to compress and extract a Java byte array.

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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.


Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:
java ,high-perf ,performance

Published at DZone with permission of Ralf Quebbemann, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}