pythonzlib在通过Decompress()obj膨胀数据时产生3错误

2024-04-26 01:00:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个解码器,从一个zlib编码的文件中解析、解压和提取一个文件,这个文件是通过类似于urlib2文件的对象下载的。这样做的目的是尽可能少地利用内存和磁盘空间,因此我使用一种读/写模式,中间有“解码器”来解压缩来自urllib2的数据,将其输入cpio子进程,最后将文件数据写入磁盘:

with closing(builder.open()) as reader:
    with open(component, "w+b") as writer:
         decoder = Decoder()

         while True:
             data = reader.read(10240)
             if len(data) == 0:
                 break

             writer.write(decoder.decode(data))

        final = decoder.flush()
        if final is not None:
            writer.write(final)

        writer.flush()

解码器太简单了:

^{pr2}$

在任何东西被传递到cpio管道之前,我看到了一个错误,所以我觉得为了清楚起见,省略它是明智的。在

有趣的是,为了验证zlib是否可以对数据进行解压缩,我将传递给decode()的原始数据data_in写入stdout:

def decode(self, data_in):
    sys.stdout.write(data_in)
    return self.__consume(self.__zcat.decompress(data_in))

然后跑了:

$ bin/myprog.py 2>/dev/null | zcat - | file -
/dev/stdin: cpio archive

如您所见,zcat对stdin上给出的数据非常满意,结果文件是一个cpio归档文件。但是zlib解压方法报告说:

error: Error -3 while decompressing: incorrect header check

Tags: 文件数据inselfdatawith解码器write
1条回答
网友
1楼 · 发布于 2024-04-26 01:00:50

\x1f\x9d是旧Unix压缩格式的前两个字节。zlib帮不了你减压。gzip可以对其进行解压缩,以便与旧的compress实用程序兼容。在

您可以使用the code from pigz来解压缩该格式并直接使用它。在

相关问题 更多 >