Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decompressing a gzipped http response

Tags:

java

gzip

Hello fellow java developers. I receive a response with headers and body as below, but when I try to decompress it using the code below, it fails with this exception:

java.io.IOException: Not in GZIP format

Response:

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ä╝|

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 advise.

Thanks,

Pradeep

like image 912
Bill Avatar asked May 17 '13 01:05

Bill


1 Answers

The MIME header is NOT in the GZIP format, it's in plain text. You have to read that first before you can decompress the stream.

Also, why not just use this:

InputStream in = sock.getInputStream();
readHeader(in);
InputStream zin = new GZIPInputStream(in);
like image 165
Leo Izen Avatar answered Nov 16 '22 12:11

Leo Izen