[英]python zlib produces a -3 error when inflating data through a Decompress() object
我創建了一個解碼器,從本質上解析,解壓縮並從通過類似urllib2文件的對象下載的zlib編碼文件中提取單個文件。 這個想法是要利用盡可能少的內存和磁盤空間,所以我正在使用帶有中間“解碼器”的讀取器/寫入器模式來解壓縮來自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 decompress方法正在報告:
error: Error -3 while decompressing: incorrect header check
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.