Uncompress GZIPed HTTP Response in Java

后端 未结 2 824
南方客
南方客 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: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);
                                }
    

提交回复
热议问题