【问题标题】:Java Decompressing byte array - incorrect data checkJava 解压字节数组 - 不正确的数据检查
【发布时间】:2015-12-18 10:58:32
【问题描述】:

我有一个小问题:我解压缩字节数组,使用以下代码一切正常,但有时使用一些数据,它会抛出 DataFormatException 错误的数据检查。有什么想法吗?

 private byte[] decompress(byte[] compressed) throws DecoderException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressed);
    ByteArrayOutputStream outPutStream = new ByteArrayOutputStream(compressed.length);
    byte temp [] = new byte[8196];
    while (!decompressor.finished()) {

        try {
            int count = decompressor.inflate(temp);
            logger.info("count = " + count);
            outPutStream.write(temp, 0, count);
        }
        catch (DataFormatException e) {
            logger.info(e.getMessage());
            throw new DecoderException("Wrong format", e);
        }
    }
    try {
        outPutStream.close();
    } catch (IOException e) {
        throw new DecoderException("Cant close outPutStream ", e);
    }
    return outPutStream.toByteArray();
}

【问题讨论】:

  • 压缩后的数据好像不能通过Adler32检查。最简单的解决方案是在初始化 Inflater 时使用 nowrap 选项,并删除压缩数据中的前两个字节。

标签: java arrays byte compression


【解决方案1】:

尝试使用不同的压缩级别或使用 nowrap 选项

【讨论】:

    【解决方案2】:

    1 一些警告:你是否在双方都使用相同的算法?

    你使用字节吗? (不是字符串)

    您的数组大小合适?

    2 我建议你一步一步检查,捕捉异常,检查大小,null,比较字节。

    像这样:Using Java Deflater/Inflater with custom dictionary causes IllegalArgumentException

    • 听取您的意见

    • 压缩它

    • 复制你的字节

    • 解压

    • 比较输出和输入

    3如果找不到,再举一个可行的例子,逐步修改

    希望对你有帮助

    【讨论】:

    • 问题是我没有压缩它,我只是通过 Go lang 压缩了值
    【解决方案3】:

    我发现了它发生的原因 字节温度 [] = 新字节 [8196]; 它太大了,它必须是解压缩数组的大小,因为它是早期的 Base64 编码,我如何在解压缩之前得到这个大小?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      • 2013-10-02
      相关资源
      最近更新 更多