使用 python 查看 .gz 存档中包含的文件

Viewing the files contained in a .gz archive using python

我正在寻找一种简单的方法来查看 .gz 存档的内容(文件名)。我看到有一个discussion already on how to do this when using .zip archive. So basically I am looking for the analogue of ZipFile.namelist(),但是对于.gz个档案。

.gz 文件不是,就其本身而言,是档案。 .gz 文件包含一个压缩文件。当你解压缩 foo.bar.gz 时,你会得到文件 foo.bar。所以这是你的名字,在 .gz 文件本身的名称中。

您可能有一个 .tar.gz 文件,它是一个 TAR(磁带存档)文件,其中包含文件目录的表示形式,然后通过 gzip 将其压缩为单个文件。在这种情况下,您需要使用 Python 的 tarfile 来读取文件并列出内容。