2

How can i decompress a String that was zipped by PHP gzcompress() function?

Any full examples?

thx

I tried it now like this:

public static String unzipString(String zippedText) throws Exception
{
    ByteArrayInputStream bais = new ByteArrayInputStream(zippedText.getBytes("UTF-8"));
    GZIPInputStream gzis = new GZIPInputStream(bais);
    InputStreamReader reader = new InputStreamReader(gzis);
    BufferedReader in = new BufferedReader(reader);

    String unzipped = "";
    while ((unzipped = in.readLine()) != null) 
        unzipped+=unzipped;

    return unzipped;
}

but it's not working if i i'm trying to unzip a PHP gzcompress (-ed) string.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
nr1
  • 133
  • 2
  • 6

3 Answers3

9

PHP's gzcompress uses Zlib NOT GZIP

public static String unzipString(String zippedText) {
    String unzipped = null;
    try {
        byte[] zbytes = zippedText.getBytes("ISO-8859-1");
        // Add extra byte to array when Inflater is set to true
        byte[] input = new byte[zbytes.length + 1];
        System.arraycopy(zbytes, 0, input, 0, zbytes.length);
        input[zbytes.length] = 0;
        ByteArrayInputStream bin = new ByteArrayInputStream(input);
        InflaterInputStream in = new InflaterInputStream(bin);
        ByteArrayOutputStream bout = new ByteArrayOutputStream(512);
        int b;
        while ((b = in.read()) != -1) {
            bout.write(b); }
        bout.close();
        unzipped = bout.toString();
    }
    catch (IOException io) { printIoError(io); }
    return unzipped;
 }
private static void printIoError(IOException io)
{
    System.out.println("IO Exception: " + io.getMessage());
}
John
  • 91
  • 1
  • 1
2

Try a GZIPInputStream. See this example and this SO question.

Community
  • 1
  • 1
Yoni Samlan
  • 37,905
  • 5
  • 60
  • 62
0

See

http://developer.android.com/reference/java/util/zip/InflaterInputStream.html

since the DEFLATE algorithm is gzip.

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
  • 1
    Minor correction: gzip uses deflate algorithm, but it also adds bit of header information (like name of file being compressed, file permissions), since deflate just compresses data. Deflate is used by other tools tool, like PNG libraries. – StaxMan Dec 01 '10 at 21:20