如何解压缩JAVA中的gzip压缩内容

时间:2014-02-04 13:18:05

标签: java gzip

在HTTP请求和响应中,内容编码是'gzip',内容是gzip压缩的。有没有办法解压缩gzip压缩的内容,所以我们可以看到内容??

GZipped HTTP请求示例

HTTP/1.1 200 OK
Date: mon, 15 Jul 2014 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix)  (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8
Content-Encoding: gzip

//Response body with non type characters

2 个答案:

答案 0 :(得分:0)

这不应该是必要的。您的应用程序服务器应该为您处理此类请求并自动为您解压缩负载。

如果没有发生,您需要将InputStream包裹在GZipInputStream中。但这听起来更像是对服务器的错误配置。

答案 1 :(得分:0)

找到答案。

//reply - Response from Http  byte[] reply = getResponseBytes();
 int i;
             for(i=0;i<reply.length;i++){
              //Finding Raw Response by two new line bytes
                 if(reply[i]==13){
                     if(reply[i+1]==10){
                         if(reply[i+2]==13){
                             if(reply[i+3]==10){
                                 break;
                             }
                         }
                     }
                 }

             }
             //Creating new Bytes to parse it in GZIPInputStream  
             byte[] newb = new byte[4096];
             int y=0;
             for(int st=i+4;st<reply.length;st++){
                 newb[y++]=reply[st];
             }
                       GZIPInputStream  gzip = new GZIPInputStream (new ByteArrayInputStream (newb));
                        InputStreamReader reader = new InputStreamReader(gzip);
                        BufferedReader in = new BufferedReader(reader);

                        String readed;
                        while ((readed = in.readLine()) != null) {
                            //Bingo...
                            System.out.println(readed);
                        }