Java 如何解压zlib
我希望解压缩一个zlib文件。 下面是我原作的照片: 当第4字节不等于0时,需要在第9字节后解压缩。 解压缩后,我收到以下错误:Java 如何解压zlib,java,zlib,Java,Zlib,我希望解压缩一个zlib文件。 下面是我原作的照片: 当第4字节不等于0时,需要在第9字节后解压缩。 解压缩后,我收到以下错误: "java.util.zip.DataFormatException: incorrect header check" 我的代码如下: private static void writeToRawInFile(byte[] bData) { try { int index = 4; byteBuffer = null;
"java.util.zip.DataFormatException: incorrect header check"
我的代码如下:
private static void writeToRawInFile(byte[] bData) {
try {
int index = 4;
byteBuffer = null;
byteBuffer = ByteBuffer.allocate(bData.length);
byteBuffer.put(bData);
byteBuffer.flip();
byte[] convertByte = byteBuffer.array();
System.out.println("Byte:" + convertByte.toString());
if (byteBuffer.get(index) == 0) {
System.out.println("index 0");
byteBuffer = null;
byteBuffer = ByteBuffer.allocate(bData.length);
byteBuffer.put(bData);
byteBuffer.flip();
//factoryType.fileServer.fc.write(byteBuffer);
} else {
//bData = Arrays.copyOfRange(bData, 1,9);
bData = decompressByteArray(Arrays.copyOfRange(bData, 0,9));
byteBuffer = ByteBuffer.allocate(bData.length);
byteBuffer.put(bData);
byteBuffer.flip();
System.out.println("data ="+ byteBuffer);
//factoryType.fileServer.fc.write(byteBuffer);
}
//factoryType.fileServer.fc.write(byteBuffer);
} catch (Exception e) {
System.out.println("expMsg .writeToRawInFile()=" + e.getMessage());
e.printStackTrace();
}
if (byteBuffer != null) {
byteBuffer.clear();
}
}
public static byte[] decompressByteArray(byte[] bytes) throws IOException, DataFormatException {
Inflater inflater = new Inflater();
System.out.println("Original: " + bytes.length);
inflater.setInput(bytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bytes.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();
System.out.println("Compressed: " + output.length);
return output;
}
你是指位置4(零基)
索引4
?如果您需要解压缩索引9之后的所有内容,则这是错误的Arrays.copyOfRange(bData,0,9)
check。我只得到Arrays.copyOf Range的结果。我的意思是当第四个字节为0时,需要进行解压缩。。第四个字节是压缩标志,如果第四个字节检测到0,它就已经解压了,但是如果它检测到的不是0,它只需要首先解压。可能范围0-8
中的字节没有经过zlib压缩。这些字节是什么。你能分享一个例子吗?你说得对。0-8不是zlib压缩的。。字节9之后是zlib。。。文件将显示如果第4个字节的范围为0,则将跳过,第4个字节中的其他值将在第9个字节后进行解压缩。首先,您需要整理输入数据的结构类型。然后找出如何独立处理独立的部件。创建一个处理字节数组的概念证明,已知该字节数组包含有效的zlib压缩数据(78 9c 4b cb cf 4f 4a 2c 02 00 08 ab 02 7a
)。当你有了小零件的工作,然后你可以开始把它们结合成更大的东西。