Java Inflater 对有效输入抛出“不正确的标头检查”异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22840410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inflater throws "incorrect header check" exception on valid input
提问by Hodossy Szabolcs
I have a file in a hex dump, which is a valid *.zip file (I can convert it with Hxd, and then open with Total Commander). But when I do it from java with the following methods:
我有一个十六进制转储文件,它是一个有效的 *.zip 文件(我可以用 Hxd 转换它,然后用 Total Commander 打开)。但是,当我使用以下方法从 Java 执行此操作时:
To convert from hex to byte array:
从十六进制转换为字节数组:
int size = hexContent.length() / 2;
byte[] byteArray = new byte[size];
for (int i = 0; i < hexContent.length() - 1; i += 2) {
//grab the hex in pairs convert to character
byteArray[i / 2] = (byte) (Integer.parseInt(hexContent.substring(i, (i + 2)), 16));
}
return byteArray;
To decompress:
解压:
Inflater inflater = new Inflater();
inflater.setInput(data, 0, data.length);
byte[] decompressedData = new byte[data.length];
inflater.inflate(decompressedData);
return decompressedData;
The inflater.infalte() method throws the exception. Can it be a problem, that java uses signed byte?
inflater.infalte() 方法抛出异常。会不会有问题,java 使用有符号字节?
UPDATE #1:
更新#1:
Decompress method was wrong, since it used the input byte array length az output array length, which is obviously wrong, so I modified it:
解压方法不对,因为它使用了输入字节数组长度az输出数组长度,显然是错误的,所以我修改了一下:
public static byte[] decompress(byte[] data) {
Inflater inflater = new Inflater();
inflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = inflater.inflate(buffer);
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte[] output = outputStream.toByteArray();
inflater.end();
}
With this I am able to decompress a randomly generated and compressed byte array (using Junit test):
有了这个,我就可以解压缩随机生成和压缩的字节数组(使用 Junit 测试):
@Test
public void decompressByteArrayTest() {
byte[] input = new byte[1024];
byte[] output, result;
new Random().nextBytes(input);
Deflater deflater = new Deflater();
deflater.setInput(input);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(input.length);
deflater.finish();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count = deflater.deflate(buffer); // returns the generated code... index
outputStream.write(buffer, 0, count);
}
outputStream.close();
output = outputStream.toByteArray();
deflater.end();
result = decompress(output);
for (int i = 0; i < result.length; i++) {
Assert.assertEquals(input[i], result[i]);
}
}
but not this particular one:
但不是这个特定的:
1F 8B 08 00 00 00 00 00 00 03 45 4D BB 0A 83 30 14 DD 85 FC 43 F6 92 44 07 97 4C
2D A5 D8 82 05 C1 F4 03 42 B8 4D 03 9A 04 73 2B BA F8 ED 8D 4B 1D 0E 9C 27 A7 B9
29 2A 9E 6B 0F D3 EC 0C 88 E4 C6 94 69 E2 DE 7A 0E 98 1C 0F 93 15 DF EC A5 9C 45
F9 CA 8C D7 75 79 7E E3 8C 3A F1 1D 26 8C C7 6E 19 07 B1 6D 7F 4D EF 4A 75 A2 E2
15 29 AE C1 23 78 64 6A 8D 20 A9 8E 71 70 46 A3 0B 5E 2C 46 47 06 C3 29 6F 8F 5A
0B DE E2 47 D2 92 14 FB 29 BB D8 EC 4A FA E8 FA 96 14 3F DF 31 BB 92 B6 00 00 00
I am getting the above given error.
我收到上述错误。
采纳答案by Hodossy Szabolcs
The first four bytes: 1F 8B 08 00 indicates it is a gzip file, so one should use GZIPInputStream for unpacking.
前四个字节:1F 8B 08 00 表示它是一个gzip 文件,所以应该使用GZIPInputStream 进行解包。