Android Decompress下载了.xml.gz文件

时间:2012-05-06 09:11:51

标签: android gzip

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

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

我正在关注此示例代码here

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

3 个答案:

答案 0 :(得分: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;
    }
}

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

答案 1 :(得分:2)

以下是使用Retrofit进行此操作的方法:

public interface DownloadApi {
    @Streaming
    @GET("/download")
    Call<ResponseBody> download();
}

private Retrofit getRetrofitClient() {
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    Retrofit.Builder builder =  new Retrofit.Builder()
        .baseUrl(API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create());

     return builder
        .client(httpClient.build())
        .build();
}

public void download() {
    DownloadApi api = getRetrofitClient().create(DownloadApi.class);
    Call<ResponseBody> call = api.download();

    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                unzipAndWriteResponseBodyToDisk(response.body());
            } else {
                //TODO
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            //TODO
        }
    });
}

 private boolean unzipAndWriteResponseBodyToDisk(ResponseBody responseBody) {
    try {
        InputStream compressedInputStream = new GZIPInputStream(responseBody.byteStream());
        InputSource inputSource = new InputSource(compressedInputStream);
        InputStream inputStream = new BufferedInputStream(inputSource.getByteStream());

        File outputFile = new File("path to your file")
        outputFile.getParentFile().mkdirs();
        OutputStream outputStream = new FileOutputStream(outputFile.getAbsoluteFile());

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }

        outputStream.flush();
        outputStream.close();
        inputStream.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

答案 2 :(得分:1)

.xml.gz文件的前十个字节是什么?看起来解压缩器试图告诉你前两个字节是d4 d4。 .gz文件必须以1f 8b开头。