解压缩GZIP

时间:2012-06-25 12:42:12

标签: java android gzip

我想知道在gzip编码响应的情况下我应该做什么。这是处理我的回答的方法:

private InputStream getResultStream(Response response) throws IOException
{
    InputStream resultStream = null;
    if(response != null)
    {
        String encoding = response.getHeader("Content-Encoding");
        if((encoding != null) && (encoding.equalsIgnoreCase("gzip")))
        {
            // What to do here ?
            Log.d("Stream :", "Read GZIP");

        } else if ((encoding != null) && encoding.equalsIgnoreCase("deflate")) {
            resultStream = new InflaterInputStream(response.getStream(), new Inflater(true));
            Log.d("Stream :", "Read Deflated.");
        } else {
            resultStream = response.getStream();
            Log.d("Stream :","Read Normal.");
        }       
    }

    return resultStream;
}

我该如何处理?

4 个答案:

答案 0 :(得分:2)

将您的信息流包裹在GZIPInputStream中并从中读取。

resultStream = new GZIPInputStream(resultStream);
//proceed reading as usual

答案 1 :(得分:0)

如果您只是想知道如何阅读gzip流,只需使用GZIPInputStream

包装输入流
 InputStream is = ...
 GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(is));
 try {
     // Reading from 'zis' gets you the uncompressed bytes...
     processStream(zis);
 } finally {
     zis.close();
 }

答案 2 :(得分:0)

执行以下操作:

resultStream = new java.util.zip.GZIPInputStream(response.getStream()); 

答案 3 :(得分:0)

免责声明:我没有机会对此进行测试。

根据Android’s HTTP Clients博客文章所说,如果您使用的是Android 2.3+,那么HttpURLConnection可以自动为您完成。