【问题标题】:Decompressing GZIP解压 GZIP
【发布时间】:2012-06-26 17:22:27
【问题描述】:

我想知道在 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;
}

我该如何处理?

【问题讨论】:

标签: java android gzip


【解决方案1】:

将您的流包装在 GZIPInputStream 中并从中读取。

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

【讨论】:

    【解决方案2】:

    如果您只想知道如何读取 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();
     }
    

    【讨论】:

      【解决方案3】:

      执行以下操作:

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

      【讨论】:

      • 试过它导致以下异常:06-25 15:00:06.465: W/System.err(15873): java.io.IOException: stream closed 06-25 15:00:06.465 : W/System.err(15873): 在 libcore.net.http.AbstractHttpInputStream.checkNotClosed(AbstractHttpInputStream.java:68) 06-25 15:00:06.465: W/System.err(15873): 在 libcore.net。 http.FixedLengthInputStream.read(FixedLengthInputStream.java:41)
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2014-10-21
        • 1970-01-01
        • 2017-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多