簡體   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