简体   繁体   English

通过Decompress()对象扩展数据时,python zlib产生-3错误

[英]python zlib produces a -3 error when inflating data through a Decompress() object

I have created a decoder to essentially parse, decompress and extract a single file from a zlib encoded file downloaded through a urllib2 file-like object. 我创建了一个解码器,从本质上解析,解压缩并从通过类似urllib2文件的对象下载的zlib编码文件中提取单个文件。 The idea is to utilize as little memory and disk space as possible, so I am using a reader / writer pattern with the "decoder" in the middle to uncompress the data coming from urllib2, feed it into a cpio subprocess and finally write the file data to disk: 这个想法是要利用尽可能少的内存和磁盘空间,所以我正在使用带有中间“解码器”的读取器/写入器模式来解压缩来自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()

The decoder is pretty simple too: 解码器也非常简单:

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())

I am seeing an error before anything is even passed to the cpio pipe, so I felt omitting it here was sensible for clarity. 我什至在将任何内容传递给cpio管道之前就看到了一个错误,因此为了清晰起见,我觉得在这里省略它是明智的。

The interesting thing, is that to verify the data could in fact be uncompressed by zlib, I wrote the raw data data_in being passed to decode() to stdout: 有趣的是,为了验证数据实际上是否可以被zlib压缩,我将原始数据data_in写入了传递给decode()的stdout中:

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

Then ran: 然后运行:

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

As you can see, zcat was quite happy about the data it was given on stdin and the resultant file is a cpio archive. 如您所见,zcat对在stdin上给出的数据感到非常满意,并且生成的文件是cpio存档。 But the zlib decompress method is reporting: 但是zlib decompress方法正在报告:

error: Error -3 while decompressing: incorrect header check

\\x1f\\x9d are the first two bytes of the old Unix compress format. \\x1f\\x9d是旧的Unix压缩格式的前两个字节。 zlib can't help you decompress it. zlib无法帮助您解压缩它。 gzip can decompress it just to be compatible with the old compress utility. gzip可以解压缩它只是为了与旧的compress实用程序兼容。

You can pull the code from pigz for decompressing that format and use it directly. 您可以从Pigz中提取代码以解压缩该格式,然后直接使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM