Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

java - Handling HTTP ContentEncoding "deflate"

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...