本文介绍了将gzip压缩和base64编码的数据解码为可读格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python3.4中尝试将gzip压缩和base64编码的数据解码为可读格式.
Trying to decode gzip compressed and base64 encoded data to a readable format in Python3.4.
import base64
import zlib
original_data = '...jU2X0NCQ19TSEEAAAABAAA=' #Data cut short.
decoded64 = base64.b64decode(original_data) #format:b'\x16xe\x94...\xae\x9a\...'
final_decoded = zlib.decompress(decoded64)
print(final_decoded)
开始得到:准备解压缩数据时出现错误-2:流状态不一致."不知道我在做什么错.
Been getting: "Error -2 while preparing to decompress data: inconsistent stream state." Not sure what I'm doing wrong.
推荐答案
zlib
成功解压缩以前压缩过的数据:
zlib
successfully decompresses data that it has compressed previously:
>>> data = b'data'
>>> import zlib
>>> compressed = zlib.compress(data)
>>> import base64
>>> original_data = base64.b64encode(compressed).decode()
>>> zlib.decompress(base64.b64decode(original_data))
b'data'
zlib
无法解压缩(使用默认设置)gzip
数据:
zlib
fails to decompress (with the default settings) gzip
data:
>>> import gzip
>>> gzipped_data = base64.b64encode(gzip.compress(data)).decode()
>>> gzipped_data != original_data
True
>>> print(zlib.decompress(base64.b64decode(gzipped_data)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect header check
>>> gzip.decompress(base64.b64decode(gzipped_data))
b'data'
这篇关于将gzip压缩和base64编码的数据解码为可读格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!