在Java中使用Inflater解压缩压缩后的数据 [英] Decompressing gzipped data with Inflater in Java

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

问题描述

我正在尝试使用Inflater解压缩压缩后的数据.根据文档,

I'm trying to decompress gzipped data with Inflater. According to the docs,

如果参数'nowrap'为true,则ZLIB标头和校验和 字段将不被使用.这提供了与 GZIP和PKZIP都使用压缩格式.

If the parameter 'nowrap' is true then the ZLIB header and checksum fields will not be used. This provides compatibility with the compression format used by both GZIP and PKZIP.

注意:使用"nowrap"选项时,还需要提供 一个额外的虚拟"字节作为输入. ZLIB本机要求 库以支持某些优化.

Note: When using the 'nowrap' option it is also necessary to provide an extra "dummy" byte as input. This is required by the ZLIB native library in order to support certain optimizations.

true传递给构造函数,然后尝试解压缩数据会导致抛出DataFormatException: invalid block type.按照此答案中的说明,我在setInput()参数的末尾添加了一个虚拟字节,以无济于事.

Passing true to the constructor, then attempting to decompress data results in DataFormatException: invalid block type being thrown. Following the instructions in this answer, I've added a dummy byte to the end of setInput()'s parameter, to no avail.

我必须改为使用GZIPInputStream吗?我在做什么错了?

Will I have to use GZIPInputStream instead? What am I doing wrong?

推荐答案

Java文档不正确或至少具有误导性:

The Java documentation is incorrect or at least misleading:

nowrap-如果为true,则支持GZIP兼容压缩

nowrap - if true then support GZIP compatible compression

nowrap的意思是原始的压缩数据将被解压缩. gzip流是用gzip标头和尾标包装的原始deflate数据.要使用此类完全解码gzip格式,您需要按照 RFC 1952 中所述处理gzip标头. a>,使用充气机解压缩原始的放气数据,计算 crc32 验证,然后再次按照

What nowrap means is that raw deflate data will be decompressed. A gzip stream is raw deflate data wrapped with a gzip header and trailer. To fully decode the gzip format with this class, you will need to process the gzip header as described in RFC 1952, use inflater to decompress the raw deflate data, compute the crc32 of the uncompressed data using that class, and then verify the crc and length (modulo 2^32) in the gzip trailer, again as specified in the RFC.

这篇关于在Java中使用Inflater解压缩压缩后的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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