有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java解压缩字节数组错误数据检查

我有一个小问题:我解压字节数组,下面的代码一切正常,但有时对于一些数据,它抛出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();
}

共 (3) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    我找到了发生的原因 字节temp[]=新字节[8196]; 它太大了,一定是解压数组的大小,因为它是早期的Base64编码的,如何在解压之前得到这个大小

  3. # 3 楼答案

    1.一些警告:双方使用相同的算法吗

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

    你的阵列大小合适吗

    二, 我建议你一步一步地检查,捕捉异常,检查大小,空值,并比较字节

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

    • 听取您的意见

    • 压缩它

    • 复制你的字节

    • 将其解压缩

    • 将输出与输入进行比较

    3如果你找不到,再举一个有效的例子,一步一步地修改它

    希望有帮助