【问题标题】:how to see the content of a particular file in .tar.gz archive without unzipping the contents?如何在不解压缩内容的情况下查看 .tar.gz 存档中特定文件的内容?
【发布时间】:2010-07-30 08:16:27
【问题描述】:

for ex abc.tar.gz 有

abc/file1.txt
abc/file2.txt
abc/abc1/file3.txt
abc/abc2/file4.txt

我需要在不提取文件的情况下读取/显示 file3.txt 的内容。

感谢您的任何意见。

【问题讨论】:

  • 我还以为是同一回事...您的意思是您不想将其解压缩到磁盘上的文件中吗?
  • 不压缩文件是一个大文件,里面有太多文件夹和文件,我没有足够的空间/使用整个未压缩的文件,我也不需要特定文件的全部内容tar.gz.我发现 ans 虽然 tar -xzf file.tar.gz -O file/particularfile.txt 和管道到 csplit 的模式。谢谢!

标签: python bash


【解决方案1】:
import tarfile
spam = tarfile.open( "abc.tar.gz" )
if "abc/abc1/file3.txt" in spam.getnames():
    with spam.extractfile( "abc/abc1/file3.txt" ) as ham:
        print ham.read()

tarfile

【讨论】:

  • 感谢 ans,但我的投票投给了 bash,因为我很容易通过管道查找特定文件上的模式。
  • 很公平——尽管你可以在 python 中做同样的事情; getnames 返回存档中可以搜索/分析的文件数组。问题被标记为python,顺便说一句,有什么特别的原因吗? =p
【解决方案2】:
tar -xzf mytar.tar.gz --to-command=cat filename.in.archive

【讨论】:

  • 你的回答帮助了。我在特定文件中寻找一些模式。到 csplit 的管道帮助我 tar -xzf file.tar.gz -O file/file.txt |csplit -s - "/regex /" {no_of_times} > /dev/null 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多