Uncompress GZIPed HTTP Response in Java

后端 未结 2 810
南方客
南方客 2021-01-02 05:24

I\'m trying to uncompress a GZIPed HTTP Response by using GZIPInputStream. However I always have the same exception when I try to read the stream : java.u

相关标签:
2条回答
  • 2021-01-02 06:11

    You don't show how you get the tBytes that you use to set up the gzip stream here:

    GZIPInputStream  gzip = new GZIPInputStream (new ByteArrayInputStream (tBytes));
    

    One explanation is that you are including the entire HTTP response in tBytes. Instead, it should be only the content after the HTTP headers.

    Another explanation is that the response is chunked.

    edit: You are taking the data after the content-encoding line as the message body. However, according to the HTTP 1.1 specification the header fields do not come in any particular order, so this is very dangerous.

    As explained in this part of the HTTP specification, the message body of a request or response doesn't come after a particular header field but after the first empty line:

    Request (section 5) and Response (section 6) messages use the generic message format of RFC 822 [9] for transferring entities (the payload of the message). Both types of message consist of a start-line, zero or more header fields (also known as "headers"), an empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields, and possibly a message-body.

    You still haven't show how exactly you compose tBytes, but at this point I think you're erroneously including the empty line in the data that you try to decompress. The message body starts after the CRLF characters of the empty line.

    May I suggest that you use the httpclient library instead to extract the message body?

    0 讨论(0)
  • 2021-01-02 06:22

    Well there is the problem I can see here;

    int  iLength = gzip.read (tByte, 0, 1024);
    

    Use following to fix that;

            byte[] buff = new byte[1024];
    byte[] emptyBuff = new byte[1024];
                                StringBuffer unGzipRes = new StringBuffer();
    
                                int byteCount = 0;
                                while ((byteCount = gzip.read(buff, 0, 1024)) > 0) {
                                    // only append the buff elements that
                                    // contains data
                                    unGzipRes.append(new String(Arrays.copyOf(
                                            buff, byteCount), "utf-8"));
    
                                    // empty the buff for re-usability and
                                    // prevent dirty data attached at the
                                    // end of the buff
                                    System.arraycopy(emptyBuff, 0, buff, 0,
                                            1024);
                                }
    
    0 讨论(0)
提交回复
热议问题