Compressing with Java Decompressing with PHP

和自甴很熟 提交于 2019-12-18 12:27:22

问题


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.


回答1:


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.




回答2:


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.




回答3:


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);



回答4:


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;
    }


来源:https://stackoverflow.com/questions/1406917/compressing-with-java-decompressing-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!