通过Decompress()对象膨胀数据时,python zlib会产生-3错误

时间:2015-03-16 22:09:18

标签: python zlib

我创建了一个解码器,用于从通过类似urllib2文件的对象下载的zlib编码文件中解析,解压缩和提取单个文件。我的想法是利用尽可能少的内存和磁盘空间,因此我使用了一个读写器模式和#34;解码器"在中间解压缩来自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()

解码器也非常简单:

class Decoder(object):
    def __init__(self):
        self.__zcat = zlib.decompressobj()
        # cpio initialisation

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

    def __consume(self, zcat_data_in):
        # cpio operations
        return data_out

    def flush(self):
        return self.__consume(self.__zcat.flush())

在看到任何内容传递给cpio管道之前我看到了一个错误,所以我觉得在这里省略它是明智的。

有趣的是,为了验证数据实际上可以被zlib解压缩,我将原始数据data_in写入传递给decode()到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

1 个答案:

答案 0 :(得分:1)

\x1f\x9d是旧Unix压缩格式的前两个字节。 zlib无法帮助您解压缩它。 gzip可以解压缩它只是为了与旧的压缩实用程序兼容。

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

相关问题