Python checking integrity of gzip archive

Question:

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
Asked By: wwn

||

Answers:

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()
Answered By: Gaurang Shah

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
    except:
        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.

Answered By: nigel222
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.