当前位置: 首页 > 知识库问答 >
问题:

读取.tar文件中的.gz文件而不提取

奚才良
2023-03-14

我有一个.tar文件,其中包含文件夹中的许多.gz文件。这些 gz 文件中的每一个都包含一个.txt文件。与此问题相关的其他堆栈溢出问题旨在提取文件。

我试图反复阅读每一个的内容。txt文件,因为。焦油很大。

首先我阅读了. tar文件的内容:

import tarfile
tar = tarfile.open("FILE.tar")
tar.getmembers()

或在Unix中:

tar xvf file.tar -O

然后我尝试使用tarfile提取文件方法,但我得到一个错误:“模块'tarfile'没有属性'提取文件'”。此外,我甚至不确定这是正确的方法。

import gzip
for member in tar.getmembers():
    m = tarfile.extractfile(member)
    file_contents = gzip.GzipFile(fileobj=m).read()

如果要创建示例文件以模拟原始文件:

$ mkdir directory
$ touch directory/file1.txt.gz directory/file2.txt.gz directory/file3.txt.gz
$ tar -c -f file.tar directory

在使用了马克·阿德勒的建议后,这是对我有效的最终版本:

import tarfile
tar = tarfile.open("file.tar")
members = tar.getmembers()

# Here I append the results in a list, because I wasn't able to
# parse the tarfile type returned by .getmembers():
tar_name = []
for elem in members:
    tar_name.append(elem.name)

# Then I changed tarfile.extractfile to tar.extractfile as suggested: 
for member in tar_name:
    # I'm using this because I have other non-gzs in the directory
    if member.endswith(".gz"):    
        m=tar.extractfile(member)
        file_contents = gzip.GzipFile(fileobj=m).read()

共有3个答案

锺英卫
2023-03-14
import gzip
import tarfile

with tarfile.TarFile("data.tar", 'r') as tar_fd:
    for files in tar_fd.getnames():
        if files.endswith(".gz"):
            file = tar_fd.extractfile(files)
            file_content = gzip.GzipFile(fileobj=file).readline()
            print(file_content)
黄正浩
2023-03-14

下面是unix行/bash命令:

要准备文件:

$ git clone https://github.com/githubtraining/hellogitworld.git
$ cd hellogitworld
$ gzip *
$ ls
build.gradle.gz  fix.txt.gz  pom.xml.gz  README.txt.gz  resources  runme.sh.gz  src
$ cd ..
$ tar -cf hellogitworld.tar hellogitworld/

以下是如何查看其自述文件:

$ tar -Oxf hellogitworld.tar hellogitworld/README.txt.gz | zcat

结果:

This is a sample project students can use during Matthew's Git class.

Here is an addition by me

We can have a bit of fun with this repo, knowing that we can always reset it to a known good state.  We can apply labels, and branch, then add new code and merge it in to the master branch.

As a quick reminder, this came from one of three locations in either SSH, Git, or HTTPS format:

* git@github.com:matthewmccullough/hellogitworld.git
* git://github.com/matthewmccullough/hellogitworld.git
* https://matthewmccullough@github.com/matthewmccullough/hellogitworld.git

We can, as an example effort, even modify this README and change it as if it were source code for the purposes of the class.

This demo also includes an image with changes on a branch for examination of image diff on GitHub.

请注意,我没有与这些git存储库相关联。

    < li >标志<代码>-x =提取 < li >标记<代码>-O =不将文件写入文件系统,而是写入标准输出 < li >标志<代码>-f =指定文件

然后剩下的只是将结果传送到zcat,以查看STDOUT中未压缩的明文

周伟泽
2023-03-14

您需要使用tar.extractfile(成员)而不是tarfile.extractfile(成员)tarfile是类,不知道您打开的tar文件。tar是tarfile对象,它引用您打开的. tar文件。

要正确地完成它,请使用< code>next()而不是< code>getmembers()或< code>getnames(),这样您就不必两次读取整个tar文件:

with tarfile.open(sys.argv[1]) as tar:
    while ent := tar.next():
        if ent.name.endswith(".gz"):
            print(gzip.GzipFile(fileobj=tar.extractfile(ent)).read())
 类似资料:
  • 我的 tar 文件位于以下位置: 使用 tar 命令: 命令显示我: 我的计划或更好的愿望是这样处理: 我只想要一个tar文件并将其存储到不同的目录……但是这个带有-C的命令不起作用…它提取tar的所有文件…… 我的问题是,是否可以只提取一个Tar文件,而不将提取到目录中??另一个问题:是否有可能只提取tar文件而不提取文件夹这可能是更好的方法,但我不知道如何。。。? 不,没有路径我就不能保存文件

  • 我想提取Unix tar xvf/home/test/group中的tar文件。tar和提取后得到一个文件夹组,其中包含xls、pdf和txt文件列表。 我怎样才能提取内容的group.tar /home/test/list的xls, pdf文件没有创建组文件夹。 任何特定的命令可用或必须跟随复制和移动?? 谢啦!

  • 我试图在Perl6中逐行读取一个巨大的gz文件。 我正在尝试做这样的事情 但是这会给出一个错误,我有一个格式不正确的UTF-8。我看不到如何从帮助页面读取gzip材料https://docs.perl6.org/language/unicode#UTF8-C8或https://docs.perl6.org/language/io 我想完成和在Perl5中一样的事情:http://blog-en .

  • 我有一个tar存档,其中包含一些其他tar存档。下面的示例完美地提取了primary.tar文件,但我在访问其中的其他.tar文件时遇到了问题。 这将返回以下内容: 所以我可以访问tar1.tar文件对象。但是,我无法弄清楚如何从tar1.tar中获取成员或内容。

  • 那么这里有什么解决方案可以使用php在线阅读excel或google电子表格呢? 更新: 建议@adyson使用相同的头来获取PHP下载的数据。 谢谢。

  • 问题内容: 我有一个tar文件,其中包含许多文件。我需要编写一个python脚本,该脚本将读取文件的内容并提供总数字符的计数,包括字母,空格,换行符的总数,所有内容,而无需解压缩tar文件。 问题答案: 您可以使用 之后,您可以用来将成员提取为文件对象。只是一个例子 对于上面示例中的文件对象,可以使用,等等。