1

I am facing a problem with Gzip uncompressing.

The situation is like this. I have some text in UTF-8 format. Now this text is compressed using gzdeflate() function in PHP and then stored in a blob object in Mysql.

Now I tried to retrieve the blob object and then used Java's Gzip Stream to un compress it. But it throws an error saying that it is not in GZIP format.

I even used Inflater in Java to do the same but now I get "DataFormatException:incorrect header check". The code for the inflater is as below.

 //rs is the resultset
 //blobAsBytes is the byte array
 while(rs.next()){
        blob = rs.getBlob("old_text");
        int blobLength = (int) blob.length();  
        blobAsBytes = blob.getBytes(1, blobLength);
    }

     Inflater decompresser = new Inflater();
     decompresser.setInput(blobAsBytes);
     byte[] result = new byte[100];
     int resultLength = decompresser.inflate(result);  // this is the line where the exception is occurring.
     decompresser.end();

     // Decode the bytes into a String
     String outputString = new String(result, 0, resultLength, "UTF-8");
     System.out.println(outputString);

I have to do this using Java and get all the text back that is stored in the database.

Can someone please help me with this.

Satyam Roy
  • 336
  • 4
  • 18

1 Answers1

1

Use gzencode(), not gzdeflate(). The latter does not produce the gzip format, it produces the deflate format. The former does produce the gzip format. The PHP functions are horribly and confusingly named.

Alternatively, use the java.util.zip.Inflater class with nowrap true in the Inflater constructor. That will decode raw deflate data on the Java end.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • OK that is interesting but here I have the data already compressed using gzdeflate() function and it is stored in the database. – Satyam Roy Apr 25 '13 at 06:18
  • OK that is interesting but here I have the data already compressed using gzdeflate() function and it is stored in the database. My requirement is to get the already stored data. I tried with PHP's gzinflate() function and there it works just fine but when it comes to Java, its all failing to retrieve the data. – Satyam Roy Apr 25 '13 at 06:25