【问题标题】:python: walking tar archive with gzip filespython:使用 gzip 文件行走 tar 存档
【发布时间】:2011-01-28 18:44:34
【问题描述】:

我有一些 .tar 文件(未压缩)。 他们每个人都有一些 .gz 文件。

我需要浏览 .tar 文件并获取所有其他文件的解压缩内容。

所以我写了:

#!/usr/bin/python2.5 -u

import tarfile
import zlib

ar = tarfile.open('20101231.tar', 'r')

for item in ar:
    if item.name[-3:] == ".gz":
        print zlib.decompress(ar.extractfile(item).read())

f.close()

但它不起作用! 错误:“zlib.error:解压缩数据时出现错误-3:标头检查不正确”

但我可以执行 'tar xvf 20101231.tar && gzip -d 20101231/some_file.gz' 并且一切正常! 但我不能从 python 做到这一点

【问题讨论】:

    标签: python gzip tar


    【解决方案1】:

    尝试tarfile.open('20101231.tar', 'r:') 明确禁用压缩。

    【讨论】:

      【解决方案2】:

      试试这个:

      this_tar = tarfile.open(filepath)
      for file in this_tar.getnames():
          ...do stuff...
      

      返回 TAR 中的每个文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-14
        • 1970-01-01
        • 2020-04-04
        • 2013-08-02
        • 2017-01-23
        • 1970-01-01
        相关资源
        最近更新 更多