Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python checking integrity of gzip archive

Tags:

python

gzip

Is there a way in Python using gzip or other module to check the integrity of the gzip archive?

Basically is there equivalent in Python to what the following does:

gunzip -t my_archive.gz
like image 934
wwn Avatar asked Oct 19 '25 12:10

wwn


1 Answers

Oops, first answer (now deleted) was result of misreading the question.

I'd suggest using the gzip module to read the file and just throw away what you read. You have to decode the entire file in order to check its integrity in any case. https://docs.python.org/2/library/gzip.html

Something like (Untested code)

import gzip
chunksize=10000000 # 10 Mbytes

ok = True
with gzip.open('file.txt.gz', 'rb') as f:
    try:
        while f.read(chunksize) != b'':
            pass
    # the file is not a gzip file.
    except gzip.BadGzipFile:
        ok = False
    # EOFError: Compressed file ended before the end-of-stream marker was reached
    # a truncated gzip file.
    except EOFError:
        ok = False

I don't know what exception reading a corrupt zipfile will throw, you might want to find out and then catch only this particular one.

like image 102
nigel222 Avatar answered Oct 22 '25 00:10

nigel222