How to convert tar.gz file to zip using only Python?

Does anyone have any code to convert tar.gz file to zip using only Python code? I had a lot of problems with tar.gz, as indicated in How can I read a tar.gz file using pandas read_csv with the gzip compression option?

0
source share
1 answer

You will need to use the tarfile module, with the 'r|gz' mode to read. Then use the zipfile to write.

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

You can use is_tarfile() to check for a valid tar file.

Perhaps you can also use shutil , but I think it cannot work in memory.

PS: From the brief testing I performed, there may be problems with members m , which are directories. If so, you may have to use is_dir() or even get information about each member of the tar file first with tarf.getmembers() and open the tar.gz file for transfer to zip , since you cannot do it after tarf.getmembers() (you cannot look back).

+2
source

All Articles