使用 Java 压缩 使用 PHP 解压

发布于 2024-08-03 23:48:27 字数 386 浏览 4 评论 0原文

我遇到过这样的情况:servlet 向 PHP 脚本提供压缩数据。我在Java端压缩数据没问题,但PHP好像无法解压。

这是相关的代码片段 Java 端:

  OutputStream o=response.getOutputStream();

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

PHP 端:

$xml= gzuncompress($xml);

有人可以指出我正确的方向吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你怎么这么可爱啊 2024-08-10 23:48:27

我刚刚看到你的问题并且很好奇。所以我做了自己的测试用例。我将所有与 Servlet 相关的内容排除在问题之外,并编写了如下代码:

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

GNU Gunzip 能够解压缩数据。然后我尝试用 PHP 解压缩它。它失败了,并出现与您相同的错误。我进一步调查并发现以下方法:

  • gzdecode()
  • gzinflate()

gzinflate 也不起作用,gzdecode 仅随我尚未安装的 PHP6 一起提供。也许你可以试试这个。 (根据 http://bugs.php.net/bug.php?id=22123 这会起作用)

我怀疑问题出在Java方面,因为GNU的gunzip可以压缩数据,所以它一定是正确的。您可能想进一步研究 PHP 方面。

对于 .NET 和 PHP 有一个相关问题,原始海报与您有相同的问题: PHP 可以解压缩使用 .NET GZipStream 类压缩的文件吗?。 PHP 似乎也无法从 GZIPOutputStream 的 .NET 等效项中解压缩数据。

抱歉,我没有“解决方案”,但无论如何我可能已经为您指出了正确的方向。

编辑:我在 PHP Bugtracker 中找到了一个条目,它解释了这个问题:
http://bugs.php.net/bug.php?id=22967 看来 gzuncompress 无法解压缩带有标头的 GZIP 数据,这些数据将由 GZIPOutputStream 生成。如 Bugtracker 条目中所述,尝试裁剪标题。

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.

山川志 2024-08-10 23:48:27

尝试使用 DeflaterOutputStream 而不是 GZIPOutputStream

PHP 函数调用实现 DEFLATE 的 libzlib。 Gzip 是一个完全不同的野兽。

将 PHP 函数命名为 gzXXX 只会增加混乱。

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.

小情绪 2024-08-10 23:48:27

我找到了一种解决该问题的方法,以 Devon_C_Miller 和 Malax 的帖子为指导。

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端:

   $content = http_get_request_body();

   $uncontent = gzuncompress($content);

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);
信仰 2024-08-10 23:48:27

'Java Deflater + PHP gzuncompress' 的组合对我有用。客户端:Android 4.1.1,服务器站点:php 5.3

对于谁正在寻找在某些情况下仅压缩部分请求正文的解决方案,例如像我一样,使用 HTTP 表单来发布一些参数和文件,以下是我在Android端使用的片段:

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文