Python解压zlib模块报错?

Python zlib module error when decompressing?

几天前我写了一个python程序来压缩一些Html数据并插入到数据库中。我使用 zlib 压缩了它们。

html = "<html><head><title>Title</title></head><body><p>Paragraph</p></body></html>"
compressed_html = str(zlib.compress(html.encode('utf-8'))).replace('b\'', '').replace('\'', '')

然后 compressed_html 变量类似于,

x\x9c\xd4\xbd\xfbv\xdb\xb6\xb30\xfa\x7f\xd6\xfa\xde...

今天我试着像这样解压它们。

html = html.encode('utf-8')
# html is retrieved from database.
# html is like now b'x\x9c\xd4\xbd\xfbv\xdb\xb6\xb30\xfa\x7f\xd6\xfa\xde...'
decompressed = zlib.decompress(html)

这会引发错误:

Traceback (most recent call last):

File "C:/Users/Sakith Karunasena/PycharmProjects/Twibot Repairer/main.py", line 16, in

decompressed = zlib.decompress(html)

zlib.error: Error -3 while decompressing data: incorrect header check

压缩它使用这个

compressed_html = zlib.compress(html.encode(), level=6)

level 可以在 -1 到 9 之间,具体取决于所需的压缩比 here

存储:

with open('filename.txt','wb') as outfile:
    outfile.write(compressed_html)

阅读

with open('filename.txt','rb') as infile:
    compressed_html = infile.read()

解压回来

decompressed_html = zlib.decompress(compressed_html).decode()