在 Python 2.7 中读取 .tar.Z 文件而不解压缩

Reading .tar.Z file without decompressing in Python 2.7

我是 Python 的新手,正在尝试读取 .tar.Z 文件名并尝试列出其中压缩的文件名。我只需要知道文件名和大小。我正在使用 Python 2.7。我可以用 .tar 文件来做到这一点。有人可以举例说明吗?

谢谢

compress 命令及其 .Z 文件太过时了,以至于 Python doesn't support it directly(可能从来没有)。

我建议

#!/bin/sh
for i in *.tar.Z; do
    tarname=`basename "$i" .Z`
    uncompress "$i"
    gzip "$tarname"
done

那你就可以用'r:gz'模式打开tarfiles as shown in the documentation

如果您不想放弃 1980 年代的压缩技术,那么您可能应该研究一下

zcat tarfile.tar.Z | tar tf -

使用 subprocess module.

(对于那些想说 "but bzip2 is better" 或 "but xz is supported in Python 3" 的人,是的,我同意,但 gzip 只是更标准)。