繁体   English   中英

如何将gzip压缩为字符串

[英]How to get gzip compression as string

我需要将一个字符串压缩到 gzip 中,并在此站点上获取 output 字符串: https://www.multiutil.com/text-to-gzip-compress/

示例图片(来自上面的网站)

但是 python 的 gzip 模块将字节作为压缩字符串返回。

是否可以将这些字节转换为字符串,或者以其他方式压缩它,以便 output 是字符串?

我试图在 utf-8 中解码,但它给出了一个错误

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

看起来该网站是base64 编码output 用于显示。 我用这段代码得到了相似(但不完全相同)的结果:

import gzip
import base64

teststring = "Welcome to Multiutil. This is the tool where you can compress your text or decompress your compressed value using Gzip(gz), Defalte or Brotli compression algorithms. Also you can compare the size between your source and result in bytes."

gzipped = gzip.compress(teststring.encode('utf-8'))
encoded = base64.encode(gzipped)

print(encoded.decode('utf-8'))

结果:

H4sIAFSA32IC/1WPwcrCQAyEX2WOCtJ3+EXw5E3wvLZjG4gb2WR/tU/vFlEQQiATvszkRO3tSoTh
UDWkhmiH4ySOVjEtG1PcJxbiaRV9ymjErdB9EQqCj4AVDPzVPxMH/CetRHXJI/az3FbjvN5gx0vS
4MJui4XKFxHLSDpakZiu3uFP3X7cU0uzhHOZiTPjTua3q7fWEykPaJfaS5CM8zPo3QvUlCS87AAA
AA==

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM