Compressiopn在php中使用GZIPOutputStream和解压缩

时间:2015-03-27 06:08:30

标签: java php compression

我一直在使用php通过创建套接字与我的Java应用程序进行通信,从Java端,我将压缩结果并将这些字节写入php。

public static byte[] compressString(byte[] b) throws IOException{
        byte[] compressedBytes = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream(b.length);
        try{
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            gzip.write(b, 0, b.length);
            gzip.finish();
            gzip.flush();
            gzip.close();
            compressedBytes = out.toByteArray();
            System.out.println("comp1::"+compressedBytes+", length::"+compressedBytes.length);
        }finally{
            out.close();
        }
        return compressedBytes;
    }

但是当我表演时

$data = fgets($fp);
$decompData = gzuncompress($data);

$ decompData返回null。

请提供解决方案,因为我尝试过Deflater和gzuncompress,gzdecode以及所有可能的选项。我必须在这里找到一些东西。把我当作php中的新手。

1 个答案:

答案 0 :(得分:0)

您需要将gzdecode()用于gzip流,您可能需要使用"rb"打开该文件,以便在不翻译的情况下读取二进制数据。

相关问题