1

I would like to know what I'm supposed to do in the case of a gzip-encoded response. this is the method handling my responses :

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;
}

How do I approach this ?

CodePrimate
  • 6,646
  • 13
  • 48
  • 86
  • look at this link http://stackoverflow.com/q/6717165/779408. A compress and decompress method is represented there. – Bob Jan 15 '13 at 10:39

4 Answers4

2

Wrap your stream in a GZIPInputStream and read from that.

resultStream = new GZIPInputStream(resultStream);
//proceed reading as usual
Jave
  • 31,598
  • 14
  • 77
  • 90
0

If you just want to know how to read a gziped stream, just wrap your inputstream with a GZIPInputStream

 InputStream is = ...
 GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(is));
 try {
     // Reading from 'zis' gets you the uncompressed bytes...
     processStream(zis);
 } finally {
     zis.close();
 }
Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
0

Do the following :

resultStream = new java.util.zip.GZIPInputStream(response.getStream()); 
Drona
  • 6,886
  • 1
  • 29
  • 35
  • Tried it which caused the following exception : 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): at libcore.net.http.AbstractHttpInputStream.checkNotClosed(AbstractHttpInputStream.java:68) 06-25 15:00:06.465: W/System.err(15873): at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:41) – CodePrimate Jun 25 '12 at 13:00
0

Disclaimer: I have not had a chance to test this.

According to what Android’s HTTP Clients blog post says if you are on Android 2.3+, then HttpURLConnection can do it for you automatically.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91