Attached code throws java.util.zip.ZipException: i

2019-08-07 15:20发布

问题:

I have a dat file which is encoded using flate. I am trying to decode that file using InflateInputStream which is based on ZLib library. But using the below sample gives me

     Exception in thread "main" java.util.zip.ZipException: incorrect data check

The .dat file is kept here

Code:-

    int buflength = 1;
    byte[] buf = new byte[buflength];
    FileInputStream is = new FileInputStream(new File(INPUT_DIRECTORY + INPUT_FILE));
    Inflater decompresser = new Inflater();
    //decompresser.setInput(buf);
    InflaterInputStream ifis = new InflaterInputStream(is,decompresser,buflength);



    FileOutputStream os = new FileOutputStream(new File(OUTPUT_DIRECTORY + OUTPUT_FILE));
    /*Deflater compressor = new Deflater();
    DeflaterOutputStream dfos = new DeflaterOutputStream(os, compressor, buflength);
    */

    int counter = 0;
    //long bytesToCopy = 40000;
    int bytesThisRead = 0;
    long bytesCopied = 0;

    while ((bytesThisRead = ifis.read(buf, 0, buf.length)) != -1) 
    {
        os.write(buf, 0, bytesThisRead);
        System.out.println((counter++) + " " + bytesThisRead);

        //bytesToCopy -= bytesThisRead;
        bytesCopied += bytesThisRead;
    }

I tried debugging the code in InflatterInputStream library, there i could find that there is some issue with reading the last byte in the raw data stream. (keep buf size as 1 then error comes after last byte)

Here for this data file , size of the raw stream is 11005 wheras for decoded stream is 36963.

So how to fix such issues in which last byte is corrupt?

回答1:

Whoever made that zlib stream (UL_obj_11_0_raw.dat) did not make a zlib stream. The Adler-32 check at the end is supposed to be stored in big-endian order, but they stored the correct check value in the wrong order, little-endian.