因此,我的 Java 应用程序显示了一些由 PHP 的 gzdeflate() 生成的数据。
现在我正在尝试使用 Java 来扩充该数据。这是我到目前为止所得到的:
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: incorrect header check
关于这个主题的其他网站只会将我重定向到 Inflater 类的文档,但显然我不知道如何使用它来与 PHP 兼容。
最佳答案
每 documentation , php gzdeflate() 生成原始放气数据(RFC 1951),但 Java 的 Inflater class期待 zlib (RFC 1950) 数据,这是包装在 zlib 头和尾中的原始放气数据。除非您指定 nowrap as true到 Inflater 构造函数。然后它将解码原始放气数据。
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);
}
关于java - Java中的gzinflate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11399350/