Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

zip

tar

Does anybody has any code for converting tar.gz file into zip using only Python code? I have been facing many issues with tar.gz as mentioned in the How can I read tar.gz file using pandas read_csv with gzip compression option?

like image 786
Geet Avatar asked Sep 17 '25 14:09

Geet


1 Answers

You would have to use the tarfile module, with mode 'r|gz' for reading. Then use zipfile for writing.

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 could also use shutil, but I think it cannot work on memory.

PS: From the brief testing that I performed, you may have issues with members m which are directories. If so, you may have to use is_dir(), or even first get the info on each tar file member with tarf.getmembers(), and the open the tar.gz file for transferring to zip, since you cannot do it after tarf.getmembers() (you cannot seek backwards).

like image 125
sancho.s ReinstateMonicaCellio Avatar answered Sep 19 '25 06:09

sancho.s ReinstateMonicaCellio