-1

我正在尝试解压缩一个 Alpha.zip 文件夹,其中包含一个 Beta 目录,其中包含一个 Gamma 文件夹,其中包含 aZ、bZ、cZ、dZ 文件。使用 zip 和 7-zip,我能够提取存储在 .Z 文件中的所有 aD、bD、cD、dD 文件。

我在 python 中使用 Import gzip 和 Import zlib 进行了尝试。

import sys
import os
import getopt
import gzip
f = open('a.d.Z','r')
file_content = f.read()
f.close()

我不断收到各种错误,包括:这不是 zip 文件,返回 codecs.charmap_encode(input self.errors encoding_map) 0。关于如何编码的任何建议?

4

1 回答 1

4

您实际上需要使用某种 zip 库。现在你正在导入gzip,但你没有用它做任何事情。尝试查看gzip文档并使用该库打开文件。

gzip_file = gzip.open('a.d.Z') # use gzip.open instead of builtin open function
file_content = gzip_file.read()

根据您的评论进行编辑:您不能只使用任何压缩库打开各种压缩文件。既然你有一个.Z文件,很可能你想使用zlib而不是gzip,但由于扩展只是约定,只有你知道你的文件是什么压缩格式。要使用zlib,请执行以下操作:

# Note: untested code ahead!
import zlib
with open('a.d.Z', 'rb') as f: # Notice that I open this in binary mode
    file_content = f.read() # Read the compressed binary data
    decompressed_content = zlib.decompress(file_content) # Decompress
于 2013-08-28T23:44:02.597 回答