【发布时间】: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