处理HTTP ContentEncoding“deflate”

发布于 2024-09-27 10:02:32 字数 634 浏览 1 评论 0原文

应使用哪种 InputStream 类型来处理将 HTTP Content-Encoding 设置为 deflate 的 URLConnection 流?

对于 gzip 或 zip 的内容编码,我使用 GZIPInputStream,没问题。

对于“deflate”的内容编码,我尝试使用 InflaterInputStreamDeflaterInputStream 但我得到

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏月而来 2024-10-04 10:02:32

在 HTTP/1.1 中,Content-encoding: deflate 实际上指的是 DEFLATE 压缩算法,由 RFC 1951,封装在 zlib 数据格式中,由 RFC 1950

然而,一些供应商只是实现 RFC 1951 定义的 DEFLATE 算法,完全忽略 RFC 1950(无 zlib 标头)。

其他人也遇到了同样的问题:

为了解决此问题,请尝试实例化 InflaterInputStream 并传递 < code>Inflater 是在 nowrap 参数设置为 true 的情况下创建的:

in = new InflaterInputStream(conn.getInputStream()), new Inflater(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 an Inflater that was created with the nowrap parameter set to true:

in = new InflaterInputStream(conn.getInputStream()), new Inflater(true));
涫野音 2024-10-04 10:02:32

不幸的是,将 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文