truncated output from GZIPInputStream on Android

妖精的绣舞 提交于 2019-12-11 01:54:23

问题


I must be doing something really wrong. Running following code on Android it produces truncated file (_items_) without any exceptions or problems in the log. Running the same code with OpenJDK 7 it decompresses the file correctly.

try {
    final InputStream fis = new GZIPInputStream(new FileInputStream("/storage/sdcard/_items"));
    try {
        final FileOutputStream fos = new FileOutputStream("/storage/sdcard/_items_");
        try {
            final byte[] buffer = new byte[1024];
            int n;
            while ((n = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, n);
            }
        } finally {
            fos.close();
        }
    } finally {
        fis.close();
    }
} catch (final IOException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
}

I've tried this with Android emulator (API 18) and on Desire HD (Android 2.3.5) with the same buggy result.

Input file (_items): https://drive.google.com/file/d/0B6M72P2gzYmwaHg4SzRTYnRMOVk/edit?usp=sharing

Android truncated output file (_items_): https://drive.google.com/file/d/0B6M72P2gzYmwMUZIZ2FEaHNZUFk/edit?usp=sharing


回答1:


The AOSP bug has been updated with analysis by an engineer from the Dalvik team.

Summary: the gzip stream has multiple members concatenated together, and the decompressor is expected to process them all, but Dalvik's implementation is stopping after the first.

Unless there's a way to convince the data source to compress its streams differently, you will need to find a replacement for GZIPInputStream.




回答2:


Workaround is to use GZIPInputStream from JZlib (currently only in concatenated_gzip_streams branch). See https://github.com/ymnk/jzlib/issues/12 for more details.



来源:https://stackoverflow.com/questions/20661312/truncated-output-from-gzipinputstream-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!