David Hamilton
David Hamilton

Reputation:

Compressing with Java Decompressing with PHP

I have a situation where a servlet is providing compressed data to a PHP script. I compress the data on the Java side no problem, but PHP seems unable to decompress.

Here is the relevent code snippets Java Side:

  OutputStream o=response.getOutputStream();

GZIPOutputStream gz=new GZIPOutputStream(o);
gz.write(GridCoder.encode(rs,id, perPage, page).getBytes());
gz.close();
o.close();

PHP Side:

$xml= gzuncompress($xml);

Can someone please point me in the right direction.

Upvotes: 10

Views: 5424

Answers (4)

firebear
firebear

Reputation: 774

The combination of 'Java Deflater + PHP gzuncompress' worked for me. Client: Android 4.1.1, server site: php 5.3

For whom is looking for solution of compressing only parts of the request body in some cases, for example as me, using HTTP form to post some parameters and a file, the following is the snippet I used in Android side:

    public static byte[] compressString(String data) throws IOException {
        byte[] compressed = null;
        byte[] byteData = data.getBytes();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(byteData.length);
        Deflater compressor = new Deflater();
        compressor.setLevel(Deflater.BEST_COMPRESSION);
        compressor.setInput(byteData, 0, byteData.length);
        compressor.finish();

        // Compress the data
        final byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
        compressor.end();
        compressed = bos.toByteArray();
        bos.close();
        return compressed;
    }

Upvotes: 2

darkhie
darkhie

Reputation: 263

I found a way to fix it, using post by Devon_C_Miller and Malax as a guidelines.

Code for java:

   String body = "Lorem ipsum shizzle ma nizle";

   URL url = new URL("http://some.url/file.php?id=" + uid);
   URLConnection conn = url.openConnection();
   conn.setDoOutput(true);
   conn.setRequestProperty("Content-encoding", "deflate");
   conn.setRequestProperty("Content-type", "application/octet-stream");

   DeflaterOutputStream dos = new DeflaterOutputStream(conn.getOutputStream());

   dos.write(body.getBytes());
   dos.flush();
   dos.close();

PHP side:

   $content = http_get_request_body();

   $uncontent = gzuncompress($content);

Upvotes: 3

Devon_C_Miller
Devon_C_Miller

Reputation: 16518

Try using DeflaterOutputStream instead of GZIPOutputStream

The PHP functions call into libzlib which implements DEFLATE. Gzip is a different beast entirely.

Naming the PHP functions gzXXX only adds to the confusion.

Upvotes: 6

Malax
Malax

Reputation: 9614

I just saw your question and was curious about it. So i made my own test-case. I left all the Servlet related stuff out of the problem and coded something like this:

import java.io.*;
import java.util.zip.GZIPOutputStream;

public class GZIPTestcase {

    public static void main(String[] args) throws Throwable {
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new FileOutputStream(new File("/Users/malax/foo2.gz")));
        PrintWriter pw = new PrintWriter(gzipOutputStream);

        pw.println("All your base are belong to us!");
        pw.flush();
        pw.close();
    }
}

The GNU gunzip was able to uncompress the data. Then i tries to uncompress it with PHP. It failed with the same error you got. I investigated further and found the following methods:

  • gzdecode()
  • gzinflate()

gzinflate does not work either, gzdecode is only shipped with PHP6 wich i havn't installed. Maybe you could try this. (According to http://bugs.php.net/bug.php?id=22123 this will work)

Im i doubt that the problem is on the Java side because GNU's gunzip can deflate the data so it must be correct. You might want to investigate further on the PHP side.

There is a realted question for .NET and PHP where the original poster has the same problem as you have: Can PHP decompress a file compressed with the .NET GZipStream class?. PHP does not seem to be able to decompress the data from the .NET equivalent of the GZIPOutputStream either.

Sorry that i dont have a "solution" but i might have pointed you in the right direction anyways.

EDIT: I found an entry in the PHP Bugtracker which explains the problem: http://bugs.php.net/bug.php?id=22967 It seems that gzuncompress cannot uncompress GZIP data with headers which will be produced be the GZIPOutputStream. As stated in the Bugtracker Entry, try to crop the header.

Upvotes: 11

Related Questions