Search code examples
compressionzipunzipcorruption

How to quickly check if a zip file is corrupted?


Does anyone have any ideas for how to pragmatically quickly check if a zip file is corrupted based on file size? Ideally the best way to check if a zip is corrupted is to do a CRC check but this can take a long time especially if there is a lot of large zip files. I would be happy just to be able to do a quick file size or header check.

Thanks in advance.


Solution

  • Section 4.3.7 of this page says that the compressed size is 4 bytes starting from byte 18. You could try reading that and comparing it to the size to the file.

    However, I think it's pretty much useless for checking if the zip file is corrupted for two reasons:

    1. Some zip files contain more bytes than just the zip part. For example, self-extracting archives have an executable part yet they're still valid zip.
    2. The file can be corrupted without changing its size.

    So, I suggest calculating the CRC for a guaranteed method of checking for corruption.