【问题标题】:not getting the original data after decompression with Inflater. inflate用 Inflater 解压后没有得到原始数据。膨胀
【发布时间】:2017-02-15 07:24:39
【问题描述】:

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

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 is 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} 

谢谢!

【问题讨论】:

    标签: java zlib


    【解决方案1】:

    您没有使输出缓冲区足够大。为了测试,我用byte[] buffer = new byte[1000];替换了byte[] buffer = new byte[original.length];,效果很好。

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

    【讨论】:

    • 感谢您的帮助!有用! “如果数据是不可压缩的,那么输出将大于输入。”以前不知道。
    猜你喜欢
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多