处理HTTP ContentEncoding“deflate”
应使用哪种 InputStream 类型来处理将 HTTP Content-Encoding 设置为 deflate 的 URLConnection 流?
对于 gzip 或 zip 的内容编码,我使用 GZIPInputStream,没问题。
对于“deflate”的内容编码,我尝试使用 InflaterInputStream 和 DeflaterInputStream 但我得到
java.util.zip.ZipException:未知 压缩方式 在 java.util.zip.InflaterInputStream.read(InflaterInputStream.java:147)
我的理解是“deflate”编码是指 Zlib 压缩,并且根据 docs 这应该由 InflaterInputStream 处理。
What InputStream type should be used to handle URLConnection streams that have HTTP Content-Encoding set to deflate?
For a Content-Encoding of gzip or zip I use a GZIPInputStream, no problem.
For a Content-Encoding of "deflate" I have tried using InflaterInputStream and DeflaterInputStream but I get
java.util.zip.ZipException: unknown
compression method
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:147)
My understanding is that "deflate" encoding refers to Zlib compression, and according to the docs this should be handled by InflaterInputStream.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 HTTP/1.1 中,
Content-encoding: deflate
实际上指的是 DEFLATE 压缩算法,由 RFC 1951,封装在 zlib 数据格式中,由 RFC 1950。然而,一些供应商只是实现 RFC 1951 定义的 DEFLATE 算法,完全忽略 RFC 1950(无 zlib 标头)。
其他人也遇到了同样的问题:
为了解决此问题,请尝试实例化
InflaterInputStream
并传递 < code>Inflater 是在nowrap
参数设置为true
的情况下创建的:In HTTP/1.1,
Content-encoding: deflate
actually refers to the DEFLATE compression algorithm, as defined by RFC 1951, wrapped in the zlib data format, as defined by RFC 1950.However some vendors just implement the DEFLATE algorithm as defined RFC 1951, completely ignoring RFC 1950 (no zlib headers).
Others have been hit by the same issue:
In order to work around this, try to instantiate the
InflaterInputStream
passing anInflater
that was created with thenowrap
parameter set totrue
:不幸的是,将 InflaterInputStream 与 Inflater 对象一起使用并不总是能产生正确的解压缩。我必须检测标头并告诉充气机有效负载的偏移量在哪里。
http://thushw.blogspot.com/2014/05 /decoding-html-pages-with-content.html
Unfortunately, using the InflaterInputStream with an Inflater object did not always produce the correct decompression. I had to detect the headers and tell the Inflater where the offset to the payload was.
http://thushw.blogspot.com/2014/05/decoding-html-pages-with-content.html