2012-07-09 32 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