data后缀文件解码,将gzip压缩和base64编码的数据解码为可读格式

该博客讨论了在Python 3.4中尝试解压缩并解码Base64编码的数据时遇到的问题。作者指出,使用zlib库解压缩之前用zlib压缩的数据可以成功,但当尝试解压缩gzip压缩的数据时,会遇到错误提示'Error-2 while preparing to decompress data: inconsistent stream state.'。解决方案是使用gzip库来解压缩由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)

Been getting: "Error -2 while preparing to decompress data: inconsistent stream state." Not sure what I'm doing wrong.

解决方案

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 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 "", line 1, in

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

>>> gzip.decompress(base64.b64decode(gzipped_data))

b'data'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值