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

╄→尐↘猪︶ㄣ 提交于 2019-12-11 05:38:46

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/39265680/how-to-convert-tar-gz-file-to-zip-using-python-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!