python gzip加密_在Python中,如何解码GZIP编码?

本文介绍了如何使用Python内置的gzip模块解压缩字节流,并澄清了gzip、zlib和deflate之间的混淆。文章强调了在不同情况下选择不同模块的重要性,特别是当处理不同压缩格式时,如zlib用于字符串解压缩,而gzip适用于已压缩的文件。还提供了zlib模块中wbits参数的详细说明,解释了如何根据windowBits值来确定解压缩的格式。
摘要由CSDN通过智能技术生成

使用内置的gzip模块解压缩字节流。

如果您有任何问题,请显示您使用的最小代码、确切的错误消息和回溯,以及print repr(your_byte_stream[:100])的结果

更多信息

1.有关gzip/zlib/deflate混淆的解释,请阅读this Wikipedia article的“其他用途”部分。

2.如果您有字符串而不是文件,那么使用zlib模块比使用gzip模块更容易。不幸的是Python docs不完整/错误:zlib.decompress(string[, wbits[, bufsize]])

...The absolute value of wbits is the base two logarithm of the size of the history buffer (the “window size”) used when compressing data. Its absolute value should be between 8 and 15 for the most recent versions of the zlib library, larger values resulting in better compression at the expense of greater memory usage. The default value is 15. When wbits is negative, the standard gzip header is suppressed; this is an undocumented feature of the zlib library, used for compatibility with unzip‘s compression file format.

首先,8<=log2_window_size<=15,其含义如上所述。然后应该是一个单独的arg的部分会被放到上面:

arg==log2_window_size意味着假设字符串是zlib格式(RFC 1950;HTTP 1.1rfc 2616混淆地称之为“deflate”)。

arg==-log2_window_size意味着假设字符串是deflate格式(RFC 1951;没有仔细阅读HTTP 1.1rfc的人实际上实现了什么)

arg==16+log_2_window_size意味着假定字符串为gzip格式(RFC 1952)。所以你可以用31。

以上信息记录在zlib C library manual。。。Ctrl-F搜索windowBits。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值