Android解压缩下载的.xml.gz文件 [英] Android Decompress downloaded .xml.gz file

查看:203
本文介绍了Android解压缩下载的.xml.gz文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式解压缩.xml.gz文件.这似乎很简单,因为互联网上有许多示例可以告诉您如何解压缩.gz文件.但是,每次尝试执行此操作时,都会出现异常:java.io.IOException:格式未知(幻数d4d4).

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).

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

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.

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

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.

推荐答案

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

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

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

这篇关于Android解压缩下载的.xml.gz文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆