Java Zip文件夹在编辑内容后已损坏
我试图使用ZipOutputStream/ZipIntputStream将一个压缩字节数组复制到另一个数组中,但结果数组似乎不等于原始数组,为什么这是错误的Java Zip文件夹在编辑内容后已损坏,java,io,outputstream,zipinputstream,zipoutputstream,Java,Io,Outputstream,Zipinputstream,Zipoutputstream,我试图使用ZipOutputStream/ZipIntputStream将一个压缩字节数组复制到另一个数组中,但结果数组似乎不等于原始数组,为什么这是错误的 public static void main(String[] args) throws IOException { File file = new File("folder.zip"); byte[] bFile = new byte[(int) file.length()]; FileInputStream
public static void main(String[] args) throws IOException {
File file = new File("folder.zip");
byte[] bFile = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
byte[] aFile = copyZippedFileBytes(bFile);
System.out.println(Arrays.equals(aFile, bFile));
}
public static byte[] copyZippedFileBytes(byte[] arr) throws IOException {
ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(arr));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(baos);
ZipEntry entry;
while ((entry = zipStream.getNextEntry()) != null) {
zipOutputStream.putNextEntry(entry);
// assume files are small
byte[] indexFileByte = new byte[(int) entry.getSize()];
zipStream.read(indexFileByte);
zipOutputStream.write(indexFileByte);
zipOutputStream.closeEntry();
}
zipOutputStream.close();
zipStream.close();
return baos.toByteArray();
}
通过修改条目的更新CRC解决,如下所示:-
CRC32 crc=新的CRC32();
crc.reset()
对我来说,现在还不清楚只有一种方法可以压缩文件……当然不会
FileInputStream
读取压缩的字节,copyZippedFileBytes
读取未压缩的字节bytes@MadProgrammer,如何解决?假设返回数组已损坏zip文件。修复什么?不能将压缩字节数组与未压缩字节数组进行比较,它们不相等
BufferedInputStream bis = new BufferedInputStream
(new ByteArrayInputStream(data));
int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1) {
crc.update(buffer, 0, bytesRead);
}
entry.setMethod(ZipEntry.STORED);
entry.setCompressedSize(data.length);
entry.setSize(data.length);
entry.setCrc(crc.getValue());