Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Zip文件夹在编辑内容后已损坏_Java_Io_Outputstream_Zipinputstream_Zipoutputstream - Fatal编程技术网

Java Zip文件夹在编辑内容后已损坏

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

我试图使用ZipOutputStream/ZipIntputStream将一个压缩字节数组复制到另一个数组中,但结果数组似乎不等于原始数组,为什么这是错误的

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());