Android,如何从URL加载 'gz'文件并在加载时自动解压内容?

标签 android http gzip

<分区>

Possible Duplicate:
Android: HTTP communication should use “Accept-Encoding: gzip”

我的 Android 应用程序下载 temp.bin 文件,但今天它(文件)太大了。我将 Apache 配置为使用 gzip 将其压缩为 temp.bin.gz。所以我得到了实际尺寸的 10%。

到目前为止一切顺利。现在的问题是如何在Android端下载时解压。

我之前用过这个下载算法 fragment :

    URL url = new URL(urlStr);

        // set redirect mode ...        
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setInstanceFollowRedirects(true);

        connection.setConnectTimeout(0);
        connection.setReadTimeout(0);
        connection.setUseCaches(false);
        connection.setAllowUserInteraction(false);

        connection.connect();  

                    ....

InputStream in = null;
        //boolean lengthValid = (contentLength != -1); 

        byte    bytes[] = new byte[256];
        int     total_count = 0;
        int     max_count = 0;

        try {
            in = connection.getInputStream();
            int count = 0;


            while (count != -1){
                count = in.read(bytes);
  ....

我们将不胜感激任何和所有建议。

最佳答案

只需使用 java.util.zip.GZIPInputStream 而不是 InputStream

Also See

关于Android,如何从URL加载 'gz'文件并在加载时自动解压内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13340403/

相关文章:

.net - .NET:是否可以使HttpWebRequest自动解压缩gzip的响应?

android - 如何确定 C 代码是为 Android/NDK 还是 iOS 编译的

Android - 如何将 Gmail HTML 邮件解析为纯文本?

http - Clarifai API 和 cURL?

PHP/MySQL : How to insert a gzcompress-ed string into a text mysql field?

r - 如何直接将R中的write.csv执行为tar.gz格式?

Android:让 TextView 的文本的一部分在运行时不可点击?

android - WebView 中的 NTLM 身份验证

ios - 将 image/jpeg 从 GET 请求转换为 UIImage

asp.net - 找不到 Http 404