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).
source share