wwn
wwn

Reputation: 593

Python checking integrity of gzip archive

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

Upvotes: 4

Views: 3743

Answers (2)

nigel222
nigel222

Reputation: 8192

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.

Upvotes: 7

Gaurang Shah
Gaurang Shah

Reputation: 12910

you can use subprocess or os module to execute this command and read the output. something like this

Using os module

import os 
output = os.popen('gunzip -t my_archive.gz').read()

Using Subprocess Module

import subprocess
proc = subprocess.Popen(["gunzip", "-t", "my_archive.gz"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()

Upvotes: 1

Related Questions