Gzip compression and http post from python to java
<p>I want to http post a gzip compressed data from python to java and I want to store it as a BLOB in database. Then I want to gzip decompress that BLOB in java. So I want to know howto post a BLOB in python and how to read a BLOB in java. I have given my python and java code below. In my code I gzip compress a string in python and store that compressed data in a file. Then I read that file in java and decompress it using GZIPInputStream. But I'm getting the below exception.</p>

<pre><code>java.io.IOException: Not in GZIP format
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:154)
    at java.util.zip.GZIPInputStream.&lt;init&gt;(GZIPInputStream.java:75)
    at java.util.zip.GZIPInputStream.&lt;init&gt;(GZIPInputStream.java:85)
    at GZipFile.gunzipIt(GZipFile.java:60)
    at GZipFile.main(GZipFile.java:43)
</code></pre>

<p>If I print the byte array of the compressed data in python I get  </p>

<p>[31, 139, 8, 0, 254, 213, 186, 87, 2, 255, 203, 72, 205, 201, 201, 231, 229, 42, 207, 47, 202, 73, 1, 0, 66, 102, 86, 48, 12, 0, 0, 0]</p>

<p>If I read and print that compressed data from that file in java I get as  </p>

<p>[31, -17, -65, -67, 8, 0, -17, -65, -67, -42, -70, 87, 2, -17, -65, -67, -17, -65, -67, 72, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, 42, -17, -65, -67, 47, -17, -65, -67, 73, 1, 0, 66, 102, 86, 48, 12, 0, 0, 0]</p>

<p>Has you can see there is difference. If I give the printed byte array in python as input to the java code it works fine. So please help me to know how to post a blob(the compressed data) in python and how to read that compressed data in java to decompress it.</p>

<p><strong>This is the compression code in python:</strong></p>

<pre><code>import StringIO  
import gzip  
import base64  
import os  


m='hello'+'\r\n'+'world'  

out = StringIO.StringIO()  
with gzip.GzipFile(fileobj=out, mode="wb") as f:  

    f.write(m.encode('utf-8'))
print list(array.array('B',out.getvalue())[:])
f=open('comp_dump','wb')  
f.write(out.getvalue())  
f.close()
</code></pre>

<p><strong>This is the decompression code in java:</strong></p>

<pre><code>//$Id$

import java.io.*;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.zip.GZIPInputStream;  
import javax.xml.bind.DatatypeConverter;  
import java.util.Arrays;

public class GZipFile
{


public static String readCompressedData()throws Exception
{
        String compressedStr ="";
        String nextLine;
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("comp_dump")));
        try
        {
                while((nextLine=reader.readLine())!=null)
                {
                        compressedStr += nextLine;
                }
        }
        finally
        {
                reader.close();
        }
        return compressedStr;
}

public static void main( String[] args ) throws Exception
{
        GZipFile gZip = new GZipFile();
        byte[] contentInBytes = readCompressedData().getBytes("UTF-8");

        System.out.println(Arrays.toString(contentInBytes));
        String decomp = gZip.gunzipIt(contentInBytes);
        System.out.println(decomp);
}

/**
 * GunZip it
 */
public static String gunzipIt(final byte[] compressed){

        byte[] buffer = new byte[1024];
        StringBuilder decomp = new StringBuilder() ;

        try{

                GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(compressed));

                int len;
                while ((len = gzis.read(buffer)) &gt; 0) {

                        decomp.append(new String(buffer, 0, len));

                }

                gzis.close();

        }catch(IOException ex){
                ex.printStackTrace();
        }
        return decomp.toString();
}
}
</code></pre>