如何解压缩zlib

时间:2017-04-04 04:11:10

标签: java zlib

我希望解压缩zlib文件。 下面是我原来的照片:

enter image description here

当字节no 4不等于0时,需要在第9个字节后解压缩。 解压缩后,我收到以下错误:

"java.util.zip.DataFormatException: incorrect header check"

我的代码如下:

private static void writeToRawInFile(byte[] bData) {
    try {
        int index = 4;
        byteBuffer = null;
        byteBuffer = ByteBuffer.allocate(bData.length);

        byteBuffer.put(bData);
        byteBuffer.flip();
        byte[] convertByte = byteBuffer.array();
        System.out.println("Byte:" + convertByte.toString());
        if (byteBuffer.get(index) == 0) {
            System.out.println("index 0");
            byteBuffer = null;
            byteBuffer = ByteBuffer.allocate(bData.length);

            byteBuffer.put(bData);
            byteBuffer.flip();
            //factoryType.fileServer.fc.write(byteBuffer);
        } else {
            //bData = Arrays.copyOfRange(bData, 1,9);
            bData = decompressByteArray(Arrays.copyOfRange(bData, 0,9));
            byteBuffer = ByteBuffer.allocate(bData.length);
            byteBuffer.put(bData);
            byteBuffer.flip();
            System.out.println("data ="+ byteBuffer);
            //factoryType.fileServer.fc.write(byteBuffer);
        }

        //factoryType.fileServer.fc.write(byteBuffer);
        } catch (Exception e) {
            System.out.println("expMsg .writeToRawInFile()=" + e.getMessage());
            e.printStackTrace();
    }

    if (byteBuffer != null) {
        byteBuffer.clear();
    }
}

public static byte[] decompressByteArray(byte[] bytes) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    System.out.println("Original: " + bytes.length);
    inflater.setInput(bytes);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bytes.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();
    System.out.println("Compressed: " + output.length);
    return output;
}

0 个答案:

没有答案