Decompress gsipped http response

Hi, Java developers. I get a response with headers and body as shown below, but when I try to unpack it using the following code, it fails with this exception:

 java.io.IOException: Not in GZIP format

Answer:

 HTTP / 1.1 200 OK
 Content-Type: text / xml;  charset = utf-8
 Content-Encoding: gzip
 Server: Jetty (6.1.x)
 ▼ UMs ¢ 0► = 7┐ép? ╙6-C╚ $ ╢gΩ ↓ ╟ ± ╪ ₧ ∟zS╨╓╓ ♦ $ FÆ╒ ÷ ▀G┬╚╞8N≤╤Cf ° ►╦█╖╗o ↨æJÄ + `: ↓ 2
 ♣ »└√S▬L &? ∙ ┬_) U╔ | ♣% ûíyk_à \, æ] hⁿ? ▀xΓ∟o╜4 ♫ ù \ #MAHG? ┤ (Q¶╞⌡▌Ç? ▼ ô [7Fí¼↔ φ☻I% ╓╣Z♂? ¿↨F; x | ♦ o / A╬ ♣ ╘≡∞─≤╝╘U ∙ ♥ 0☺æ? | J% à {(éUmHµ% σl┴ ▼ Ç9 ♣ ┌Ç ? ♫ ╡5╠yë ~ ├╜ ♦ íi ♫ ╥╧
 ╬û? ▓ε? ╞┼ → RtGqè ₧ ójWë ♫ ╩∞j05├╞┘ |> ┘º ∙ ↑ j╪2┐ | = ÷ ²
 eY \ ╛P? # 5wÑqc╙τ ♦ ▓½Θt £ 6q∩? ┌4┼t ♠ ↕ = 7æƒ╙? ╟ | ♂; ║) ∩ ÷ ≈═ ^ ╛ {v⌂┌∞◄> 6ä╝ |

The code:

byte[] b= IOUtils.toByteArray(sock.getInputStream()); ByteArrayInputStream bais = new ByteArrayInputStream(b); GZIPInputStream gzis = new GZIPInputStream(bais); InputStreamReader reader = new InputStreamReader(gzis); BufferedReader in = new BufferedReader(reader); String readed; while ((readed = in.readLine()) != null) { System.out.println("read: "+readed); } 

Please inform.

Thanks,

Pradeep

+8
java gzip
source share
3 answers

The MIME header is NOT in GZIP format, it is in plain text. You must read this before you can unpack the stream.

Also, why not just use this:

 InputStream in = sock.getInputStream(); readHeader(in); InputStream zin = new GZIPInputStream(in); 
+2
source

There are libraries for all of this. You can use, for example, Apache HTTP Components , or you can read it open source to find out what it does. At least read the relevant specification.

+1
source

The second answer is bmarguiles.

Only the body ( response-body in the RFC) is compressed, so you only need to unpack the part that is after \r\n\r\n .

Generally speaking, you can cut the answer in half with these double CRLFs and only unzip the other half.

0
source

All Articles