Android解压下载的.xml.gz文件

标签 android gzip

我正在尝试以编程方式解压缩 .xml.gz 文件。这似乎非常简单,因为互联网上有许多示例可以告诉您如何解压缩 .gz 文件。但是,每次我尝试这样做时,都会出现异常:java.io.IOException: unknown format (magic number d4d4)。

我正在尝试在 Android 应用程序中执行此操作,是否应该采用与在 Java 中不同的方式来执行此操作?

我正在关注可用的示例代码 here .

有人知道我在这里做错了什么吗?另外,我在从网络服务器下载文件后解压缩文件。下载似乎运行良好。

最佳答案

找到一个下载的同时解压的好办法。工作起来轻而易举。

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

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

关于Android解压下载的.xml.gz文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10469407/

相关文章:

java - 不要将文本显示与设置的文本连接起来,而是使用 android 资源?

asp.net-mvc - MVC4 WebAPI 不压缩 GET 响应

c - 高效查找和替换 gzip 内容上的数据

ubuntu - 如何在 ubuntu 中提取 .tar 文件?

java - 如何保存 soapui 的压缩响应?

android - Google Analytics V4 离线事件跟踪

android - TranslateAnimation 将 View 移出屏幕 -Android

java - Android Studio : Virtual Device

Android Studio 2.2 Preview 1 渲染问题

c# - 为什么我的程序压缩会删除文件扩展名?