如何仅使用 Python 将 tar.gz 文件转换为 zip? [英] How to convert tar.gz file to zip using Python only?
问题描述
有没有人有任何仅使用 Python 代码将 tar.gz 文件转换为 zip 的代码?正如 如何使用带有 gzip 压缩选项的 pandas read_csv 读取 tar.gz 文件?
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?
推荐答案
你必须使用 tarfile 模块,读取模式为 'r|gz'
.然后使用 zipfile 进行写入.
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()
您可以使用 is_tarfile()
来检查有效的 tar 文件.
You can use is_tarfile()
to check for a valid tar file.
也许你也可以使用 shutil
,但我认为它不能在内存上工作.
Perhaps you could also use shutil
, but I think it cannot work on memory.
PS:从我执行的简短测试来看,您可能会遇到目录成员 m
的问题.如果是这样,您可能必须使用 is_dir()
,或者甚至首先使用 tarf.getmembers()
获取每个 tar 文件成员的信息,然后打开 tar.gz
文件用于传输到 zip
,因为在 tarf.getmembers()
之后你不能这样做(你不能向后寻找).
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).
这篇关于如何仅使用 Python 将 tar.gz 文件转换为 zip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!