Android Decompress downloaded .xml.gz file

android, gzip

Solution

Found a nice way of downloading an decompressing at the same time. It works like a breeze.

protected String doInBackground(String... sUrl) {
    try {
        URL url = new URL(sUrl[0]);
        URLConnection connection = url.openConnection();
        InputStream stream = connection.getInputStream();
        stream = new GZIPInputStream(stream);
        InputSource is = new InputSource(stream);
        InputStream input = new BufferedInputStream(is.getByteStream());
        OutputStream output = new FileOutputStream("Path to the file");
        byte data[] = new byte[2097152];
        long total = 0;
        int count;

        while ((count = input.read(data)) != -1) {
            total += count;
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (BufferOverflowException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Reference: http://abhirampal.com/2012/05/17/android-download-decompress-gzip/

Problem

I am trying to programmatically decompress a .xml.gz file. It seems to be all pretty straightforward as there are many examples available on the internet which tell you how to decompress a .gz file. However, every-time I try to do it, I get an exception: java.io.IOException: unknown format (magic number d4d4). I'm trying to do this in an Android app, is it supposed to be done differently from the way its done otherwise in Java? I'm following this example code available here. Anyone have any idea what I'm doing wrong here? Also, I'm decompressing the file after I have downloaded it from the a web server. The download seems to have worked fine.

Original source