Search code examples
pythontarziptarfile

NameError while converting tar.gz to zip


I got the following code from my question on how to convert the tar.gz file to zip file.

import tarfile, zipfile
tarf = tarfile.open(name='sample.tar.gz', mode='r|gz' )
zipf = zipfile.ZipFile.open( name='myzip.zip', mode='a', compress_type=ZIP_DEFLATED )
for m in tarf.getmembers():
    f = tarf.extractfile( m )
    fl = f.read()
    fn = m.name
    zipf.writestr( fn, fl )
tarf.close()
zipf.close()

but when I run it I get the error.

What should I change in the code to make it work?

NameError: name 'ZIP_DEFLATED' is not defined

Solution

  • ZIP_DEFLATED is a name defined by the zipfile module; reference it from there:

    zipf = zipfile.ZipFile(
        'myzip.zip', mode='a',
        compression=zipfile.ZIP_DEFLATED)
    

    Note that you don't use the ZipFile.open() method here; you are not opening members in the archive, you are writing to the object.

    Also, the correct ZipFile class signature names the 3rd argument compression. compress_type is only used as an attribute on ZipInfo objects and for the ZipFile.writestr() method. The first argument is not named name either; it's file, but you normally would just pass in the value as a positional argument.

    Next, you can't seek in a gzip-compressed tarfile, so you'll have issues accessing members in order if you use tarf.getmembers(). This method has to do a full scan to find all members to build a list, and then you can't go back to read the file data anymore.

    Instead, iterate directly over the object, and you'll get member objects in order at a point you can still read the file data too:

    for m in tarf:
        f = tarf.extractfile( m )
        fl = f.read()
        fn = m.name
        zipf.writestr( fn, fl )