Android Decompress下载了.xml.gz文件

Abh*_*pal 3 android gzip

我试图以编程方式解压缩.xml.gz文件.它似乎非常简单,因为互联网上有许多可用的例子告诉你如何解压缩.gz文件.但是,每次我尝试这样做时,都会出现异常:java.io.IOException:未知格式(幻数d4d4).

我试图在一个Android应用程序中这样做,它应该与它在Java中的方式不同吗?

我正在关注此处提供的示例代码.

任何人都知道我在这里做错了什么?此外,我在从Web服务器下载文件后解压缩文件.下载似乎运行良好.

Abh*_*pal 6

找到了一种同时下载解压缩的好方法.它像微风一样工作.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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