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指挥官打开)。但是当我从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?
方法抛出异常。java使用有符号的字节会有问题吗?
UPDATE #1:
UPDATE # 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.
我得到了上述的误差。
1 个解决方案
#1
7
The first four bytes: 1F 8B 08 00 indicates it is a gzip file, so one should use GZIPInputStream for unpacking.
前四个字节:1f8b08表示它是一个gzip文件,所以应该使用GZIPInputStream来解压缩。
#1
7
The first four bytes: 1F 8B 08 00 indicates it is a gzip file, so one should use GZIPInputStream for unpacking.
前四个字节:1f8b08表示它是一个gzip文件,所以应该使用GZIPInputStream来解压缩。