跳到主要内容

gzip

gzip 库提供 gzip 压缩与解压能力,用于处理 HTTP 响应、日志、归档等 gzip 编码的数据。

典型使用场景:

  • 压缩/解压:gzip.Compress 压缩数据,gzip.Decompress 解压。
  • 识别:gzip.IsGzip 判断字节流是否为 gzip 格式。

与相邻库的关系:gzip 是数据处理小工具,常与 http/poc(处理压缩响应)、zip(归档)、codec(编解码)配合。

共 3 个函数

函数索引

函数参数返回值说明
gzip.Compressi any[]byte, error使用 gzip 压缩数据,返回压缩后的字节与错误
gzip.Decompressret []byte[]byte, error解压 gzip 数据,返回解压后的字节与错误
gzip.IsGzipraw []bytebool判断给定字节切片是否为 gzip 压缩数据(通过检查魔数头)

函数详情

Compress

Compress(i any) ([]byte, error)

使用 gzip 压缩数据,返回压缩后的字节与错误

参数

参数名类型说明
iany待压缩的数据,支持字符串、字节切片或 io.Reader

返回值

序号类型说明
r1[]byte压缩后的字节切片(带 gzip 魔数头)
r2error错误信息

示例

// 压缩后再解压应还原原始数据(round-trip)
compressed = gzip.Compress("hello yaklang")~
assert gzip.IsGzip(compressed), "compressed output should have gzip magic header"
raw = gzip.Decompress(compressed)~
assert string(raw) == "hello yaklang", "gzip compress then decompress should round-trip"
println("gzip round-trip example passed")

Decompress

Decompress(ret []byte) ([]byte, error)

解压 gzip 数据,返回解压后的字节与错误

参数

参数名类型说明
ret[]byte经过 gzip 压缩的字节切片

返回值

序号类型说明
r1[]byte解压还原后的字节切片
r2error错误信息

示例

// 压缩再解压应还原原始数据(round-trip)
compressed = gzip.Compress("hello yaklang")~
raw = gzip.Decompress(compressed)~
assert string(raw) == "hello yaklang", "gzip decompress should restore original data"
println("gzip decompress example passed")

IsGzip

IsGzip(raw []byte) bool

判断给定字节切片是否为 gzip 压缩数据(通过检查魔数头)

参数

参数名类型说明
raw[]byte待检测的字节切片

返回值

序号类型说明
r1bool是否为 gzip 数据(以 0x1f 0x8b 0x08 开头则为 true)

示例

compressed = gzip.Compress("hello yaklang")~
println(gzip.IsGzip(compressed)) // OUT: true
assert gzip.IsGzip(compressed), "gzip output should be detected as gzip"
assert !gzip.IsGzip("plain text"), "plain text should not be detected as gzip"