2012-07-09 28 views
2

因此,我的Java应用程序能够发现一些使用PHP的gzdeflate()生成的数据。 现在我试图用Java来夸大这些数据。这是我到目前为止:Java中的gzinflate

InflaterInputStream inflInstream = new InflaterInputStream(new ByteArrayInputStream(inputData.getBytes()), new Inflater()); 

byte bytes[] = new byte[1024]; 
while (true) { 
    int length = inflInstream.read(bytes, 0, 1024); 
    if (length == -1) break; 

    System.out.write(bytes, 0, length); 
} 

'inputData'是一个包含放缩数据的字符串。

的问题是:尽量不正确头检查

在这个问题上的其他网站只能去重新定向:在.read方法抛出一个异常:

java.util.zip.ZipException我到Inflater类的文档,但显然我不知道如何使用它来与PHP兼容。

回答

6

documentation,PHP gzdeflate()生成原始DEFLATE数据(RFC 1951),但Java的Inflater class是预计zlib(RFC 1950)数据,这是原始缩减数据包裹在zlib头和尾部。 除非指定Inflater构造函数的nowrap as true。然后它将解码原始的放气数据。

InputStream inflInstream = new InflaterInputStream(new ByteArrayInputStream(inputData.getBytes()), 
                new Inflater(true)); 

byte bytes[] = new byte[1024]; 
while (true) { 
    int length = inflInstream.read(bytes, 0, 1024); 
    if (length == -1) break; 

    System.out.write(bytes, 0, length); 
} 
+0

谢谢,这样做 – 2012-07-09 21:17:36

1

使用GZIPInputStream按照例子在(不直接使用充气):

http://java.sun.com/developer/technicalArticles/Programming/compression/

+0

这并不工作 我现在已经改变了第一线的InputStream inflInstream =新GZIPInputStream(新ByteArrayInputStream的(inputData.getBytes())); 其中导致java.util.zip.ZipException:不是GZIP格式。 – 2012-07-09 18:38:00

+0

将数据保存到一个文件并使用一些ZIP工具打开它,并验证格式... – gliptak 2012-07-09 20:02:26