Friday, June 17, 2016

Content decompress (Base64 + Zlib)

I wanted to decompress data getting send to GSA (google search appliance) to verify the actual content. Google Search Appliance uses Base64 encode and Zlib compress technique which improves performance, because less data is sent across the network.

Here is link on GSA Content Compression: https://www.google.com/support/enterprise/static/gsa/docs/admin/72/gsa_doc_set/feedsguide/feedsguide.html#1074587

There is api provides by Java that can be used to  compress and decompress data using the java.util.zip package. - http://www.oracle.com/technetwork/articles/java/compress-1565076.html

To decode base64 string you can use class org.apache.commons.codec.binary.Base64 provided by apache commons library.

code snippet that follows:  
String string = "SmF2YWNvZGVnZWVrcw==";
// Get bytes from string
byte[] byteArray = Base64.decodeBase64(string.getBytes());
// Print the decoded array
System.out.println(Arrays.toString(byteArray));


To decompress zlib content you can use Java Byte Array Using Inflater
code snippet that follows: 
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; 
}  

Here is the reference link: https://dzone.com/articles/how-compress-and-uncompress


Download complete source code: https://app.box.com/s/w2wyffurcwdpeetouk56kg2tmg40wjtl

No comments:

Post a Comment