truncated output from GZIPInputStream on Android

529 views Asked by At

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

2

There are 2 answers

0
Martin Ždila On BEST ANSWER

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.

0
fadden On

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.