wolf
wolf

Reputation: 21

python: walking tar archive with gzip files

I have some .tar files (ungzipped). Each of them has some .gz files.

I need to walk through .tar file and get ungzipped content of all other files.

so I wrote:

#!/usr/bin/python2.5 -u

import tarfile
import zlib

ar = tarfile.open('20101231.tar', 'r')

for item in ar:
    if item.name[-3:] == ".gz":
        print zlib.decompress(ar.extractfile(item).read())

f.close()

but it doesn't work! Error: "zlib.error: Error -3 while decompressing data: incorrect header check"

but I can do 'tar xvf 20101231.tar && gzip -d 20101231/some_file.gz' and all works perfectly! But I can't do it from python

Upvotes: 2

Views: 809

Answers (2)

Marc Maxmeister
Marc Maxmeister

Reputation: 4699

try this:

this_tar = tarfile.open(filepath)
for file in this_tar.getnames():
    ...do stuff...

returns each file in the TAR.

Upvotes: 0

nmichaels
nmichaels

Reputation: 51029

Try tarfile.open('20101231.tar', 'r:') to explicitly disable compression.

Upvotes: 3

Related Questions