开发者

Python script to check if a zip file is corrupt

开发者 https://www.devze.com 2023-02-08 17:02 出处:网络
How do I check if a zip file is corrupt or not? I 开发者_如何学Gohave a zip file with 10 jpg images. I am able to extract say 8 of the images. Two of the images in the zip are corrupt and I am not abl

How do I check if a zip file is corrupt or not? I 开发者_如何学Gohave a zip file with 10 jpg images. I am able to extract say 8 of the images. Two of the images in the zip are corrupt and I am not able to extract those. Is there a way to check for this in a Python script?


This code will either throw an exception (if the zip file is really bad or if it's not a zip file), or show the first bad file in the zip file.

import sys
import zipfile

if __name__ == "__main__":
    args = sys.argv[1:]

    print("Testing zip file: %s" % args[0])

    try:
        the_zip_file = zipfile.ZipFile(args[0])
        ret = the_zip_file.testzip()
        if ret is not None:
            print("First bad file in zip: %s" % ret)
            sys.exit(1)
    except Exception as ex:
        print("Exception:", ex)
        sys.exit(1)

    print("Zip file is good.")


Use the zipfile module testzip function, see http://docs.python.org/library/zipfile.html#zipfile.ZipFile.testzip

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号