Hodossy Szabolcs
Hodossy Szabolcs

Reputation: 1668

Inflater throws "incorrect header check" exception on valid input

I have a file in a hex dump, which is a valid *.zip file (I can convert it with Hxd, and then open with Total Commander). But when I do it from java with the following methods:

To convert from hex to byte array:

    int size = hexContent.length() / 2;
    byte[] byteArray = new byte[size];
    for (int i = 0; i < hexContent.length() - 1; i += 2) {
        //grab the hex in pairs convert to character
        byteArray[i / 2] = (byte) (Integer.parseInt(hexContent.substring(i, (i + 2)), 16));
    }
    return byteArray;

To decompress:

    Inflater inflater = new Inflater();
    inflater.setInput(data, 0, data.length);
    byte[] decompressedData = new byte[data.length];
    inflater.inflate(decompressedData);
    return decompressedData;

The inflater.infalte() method throws the exception. Can it be a problem, that java uses signed byte?

UPDATE #1:

Decompress method was wrong, since it used the input byte array length az output array length, which is obviously wrong, so I modified it:

public static byte[] decompress(byte[] data) {
    Inflater inflater = new Inflater();
    inflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    inflater.end();
}

With this I am able to decompress a randomly generated and compressed byte array (using Junit test):

@Test
public void decompressByteArrayTest() {
    byte[] input = new byte[1024];
    byte[] output, result;
    new Random().nextBytes(input);
    Deflater deflater = new Deflater();
    deflater.setInput(input);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(input.length);
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer); // returns the generated code... index  
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    output = outputStream.toByteArray();
    deflater.end();
    result = decompress(output);
    for (int i = 0; i < result.length; i++) {
       Assert.assertEquals(input[i], result[i]);
    }
}

but not this particular one:

1F 8B 08 00 00 00 00 00 00 03 45 4D BB 0A 83 30 14 DD 85 FC 43 F6 92 44 07 97 4C
2D A5 D8 82 05 C1 F4 03 42 B8 4D 03 9A 04 73 2B BA F8 ED 8D 4B 1D 0E 9C 27 A7 B9
29 2A 9E 6B 0F D3 EC 0C 88 E4 C6 94 69 E2 DE 7A 0E 98 1C 0F 93 15 DF EC A5 9C 45
F9 CA 8C D7 75 79 7E E3 8C 3A F1 1D 26 8C C7 6E 19 07 B1 6D 7F 4D EF 4A 75 A2 E2
15 29 AE C1 23 78 64 6A 8D 20 A9 8E 71 70 46 A3 0B 5E 2C 46 47 06 C3 29 6F 8F 5A
0B DE E2 47 D2 92 14 FB 29 BB D8 EC 4A FA E8 FA 96 14 3F DF 31 BB 92 B6 00 00 00

I am getting the above given error.

Upvotes: 5

Views: 20680

Answers (1)

Hodossy Szabolcs
Hodossy Szabolcs

Reputation: 1668

The first four bytes: 1F 8B 08 00 indicates it is a gzip file, so one should use GZIPInputStream for unpacking.

Upvotes: 12

Related Questions