3

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

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

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

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

4

3 回答 3

7

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

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/

于 2012-05-17T10:24:03.413 回答
3

这是一种使用改造的方法:

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;
    }
}
于 2018-05-23T15:38:42.707 回答
1

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

于 2012-05-07T01:37:49.173 回答