用Inflater解压后得不到原始数据。膨胀

not getting the original data after decompression with Inflater. inflate

这是我的测试代码,问题是resultLength != original.length,result array 和原始array 一样,压缩和解压后不应该吗?

public static void main(String[] args) {

        // generateTyreAlarmEvent()
        byte[] original = {10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1, 56, 1, 64, 0};
        byte[] buffer = new byte[original.length];
        Deflater compresser = new Deflater();
        compresser.setInput(original);
        compresser.finish();
        int compressedLen = compresser.deflate(buffer);
        compresser.end();

        Inflater decompressor = new Inflater();
        decompressor.setInput(buffer, 0, compressedLen);
        byte[] result = new byte[original.length];
        int resultLength = 0;
        try {
            resultLength = decompressor.inflate(result);
        } catch (DataFormatException e) {
        }
        decompressor.end();

        // the problem is resultLength(63) != original.length(67), and result array is same as original array
    }

原字节数组为(len为67):

{10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1, 56, 1, 64, 0}

结果是(len 是 63):

{10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1} 

谢谢!

您没有使输出缓冲区足够大。只是为了测试,我用 byte[] buffer = new byte[1000]; 替换了 byte[] buffer = new byte[original.length];,它工作正常。

如果数据不可压缩,那么输出将大于输入。