问题
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