How to check if a generated zip file is corrupted?

后端 未结 8 1710
梦毁少年i
梦毁少年i 2020-12-05 10:33

we have a piece of code which generates a zip file on our system. Everything is ok, but sometimes this zip file while opened by FilZip or WinZip is considered to be corrupte

8条回答
  •  情书的邮戳
    2020-12-05 10:41

    new ZipFile(file) 
    

    compress again the file, so duplicate efforts and that is not what you are looking for. Despite of the fact that only check one file and the question compress n-files.

    Take a look to this: http://www.kodejava.org/examples/336.html

    Create a checksum for your zip:

    CheckedOutputStream checksum = new CheckedOutputStream(fos, new CRC32());
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));
    ...
    

    And when you finish the compression show it

    System.out.println("Checksum   : " + checksum.getChecksum().getValue());
    

    You must do the same reading the zip with java or others tools checking if checksums match.

    see https://stackoverflow.com/a/10689488/848072 for more information

提交回复
热议问题