解压缩时,Python zlib模块错误?

时间:2019-09-14 05:01:41

标签: python python-3.x compression zlib

几天前,我编写了一个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 retireved 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 <module>

    decompressed = zlib.decompress(html)

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

1 个答案:

答案 0 :(得分:0)

要压缩它,请使用此

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

级别可以根据所需的压缩比在{-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()