Java中的gzinflate [英] gzinflate in Java

查看:111
本文介绍了Java中的gzinflate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我的Java应用程序公开了一些用PHP的gzdeflate()生成的数据.现在,我正在尝试使用Java扩充该数据.这是到目前为止我得到的:

So, my Java application reveives some data that is generated with PHP's gzdeflate(). Now i'm trying to inflate that data with Java. This is what i've got so far:

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'是一个包含压缩数据的字符串.

'inputData' is a String containing the deflated data.

问题是:.read方法引发异常:

The problem is: the .read method throws an exception:

java.util.zip.ZipException:标头检查不正确

java.util.zip.ZipException: incorrect header check

关于此主题的其他网站仅能将我重定向到Inflater类的文档,但是显然我不知道如何使用它与PHP兼容.

Other websites on this subject only go as far as redirecting me to the Inflater class' documentation, but apparently I don't know how to use it to be compatible with PHP.

推荐答案

根据文档,php gzdeflate()会生成原始的deflate数据(RFC 1951),但是Java的立即设为Inflater构造函数.然后它将解码原始的放气数据.

Per the documentation, php gzdeflate() generates raw deflate data (RFC 1951), but Java's Inflater class is expecting zlib (RFC 1950) data, which is raw deflate data wrapped in zlib header and trailer. Unless you specify nowrap as true to the Inflater constructor. Then it will decode raw deflate data.

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中的gzinflate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆