Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2024-10-24 11:25:16 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-12-04 18:26:12 * @FilePath: \go-toolbox\pkg\zipx\zlib.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
Index ¶
- Constants
- func GzipCompress(data []byte) ([]byte, error)
- func GzipCompressObject[T any](obj T) ([]byte, error)
- func GzipCompressObjectWithSize[T any](obj T) ([]byte, int, error)
- func GzipCompressWithLevel(data []byte, level int) ([]byte, error)
- func GzipCompressWithPrefix(data []byte) ([]byte, error)
- func GzipDecompress(compressedData []byte) ([]byte, error)
- func GzipDecompressObject[T any](compressedData []byte) (T, error)
- func GzipSmartDecompress(data []byte) ([]byte, error)
- func GzipSmartDecompressObject[T any](data []byte) (T, error)
- func IsGzipCompressed(data []byte) bool
- func IsZlibCompressed(data []byte) bool
- func MultiGZipCompress(data []byte, times int) ([]byte, error)
- func MultiGZipCompressObject[T any](obj T, times int) ([]byte, error)
- func MultiGZipDecompress(compressedData []byte, times int) ([]byte, error)
- func MultiGZipDecompressObject[T any](compressedData []byte, times int) (T, error)
- func MultiZlibCompress(data []byte, times int) ([]byte, error)
- func MultiZlibCompressObject[T any](obj T, times int) ([]byte, error)
- func MultiZlibDecompress(compressedData []byte, times int) ([]byte, error)
- func MultiZlibDecompressObject[T any](compressedData []byte, times int) (T, error)
- func ZlibCompress(data []byte) ([]byte, error)
- func ZlibCompressObject[T any](obj T) ([]byte, error)
- func ZlibCompressObjectWithSize[T any](obj T) ([]byte, int, error)
- func ZlibCompressWithLevel(data []byte, level int) ([]byte, error)
- func ZlibCompressWithPrefix(data []byte) ([]byte, error)
- func ZlibDecompress(compressedData []byte) ([]byte, error)
- func ZlibDecompressObject[T any](compressedData []byte) (T, error)
- func ZlibSmartDecompress(data []byte) ([]byte, error)
- func ZlibSmartDecompressObject[T any](data []byte) (T, error)
- type CompressResult
- func GzipCompressObjectWithInfo[T any](obj T) (*CompressResult, error)
- func GzipCompressWithInfo(data []byte) (*CompressResult, error)
- func GzipCompressWithPrefixInfo(data []byte) (*CompressResult, error)
- func MultiGZipCompressObjectWithInfo[T any](obj T, times int) (*CompressResult, error)
- func MultiGZipCompressWithInfo(data []byte, times int) (*CompressResult, error)
- func MultiZlibCompressObjectWithInfo[T any](obj T, times int) (*CompressResult, error)
- func MultiZlibCompressWithInfo(data []byte, times int) (*CompressResult, error)
- func ZlibCompressObjectWithInfo[T any](obj T) (*CompressResult, error)
- func ZlibCompressWithInfo(data []byte) (*CompressResult, error)
- func ZlibCompressWithPrefixInfo(data []byte) (*CompressResult, error)
Constants ¶
const ( // MinCompressLevel 最小级别:flate.HuffmanOnly(只做 Huffman 编码,不找匹配) MinCompressLevel = -2 // MaxCompressLevel 最大级别:flate.BestCompression MaxCompressLevel = 9 )
压缩级别的取值范围,与 compress/flate 一致
const ( // GzipPrefix 是用于标识 gzip 压缩数据的前缀 GzipPrefix = "GZIP:" // GzipPrefixLen 是 gzip 前缀的长度 GzipPrefixLen = len(GzipPrefix) )
const ( // ZlibPrefix 是用于标识 zlib 压缩数据的前缀 ZlibPrefix = "ZLIB:" // ZlibPrefixLen 是 zlib 前缀的长度 ZlibPrefixLen = len(ZlibPrefix) )
Variables ¶
This section is empty.
Functions ¶
func GzipCompress ¶
GzipCompress 使用 gzip 压缩数据
级别为 DefaultCompression若载荷只有几十到几百字节,GzipCompressWithLevel 配 BestSpeed 可快约五倍而体积几乎不变,原因见本文件「压缩级别」一节
func GzipCompressObject ¶
GzipCompressObject 泛型压缩函数,支持任意类型自动JSON序列化
func GzipCompressObjectWithSize ¶
GzipCompressObjectWithSize 泛型压缩函数,返回压缩后的数据和原始JSON数据大小
func GzipCompressWithLevel ¶
GzipCompressWithLevel 按指定压缩级别进行 gzip 压缩
level 取值与 compress/flate 一致:BestSpeed(1) 到 BestCompression(9), 另可取 DefaultCompression(-1)、NoCompression(0) 与 HuffmanOnly(-2) 级别之间的速度与体积实测见本文件「压缩级别」一节
func GzipCompressWithPrefix ¶
GzipCompressWithPrefix 压缩数据并添加 GZIP: 前缀 返回带前缀的压缩数据,适用于需要明确标识压缩格式的场景
func GzipDecompress ¶
GzipDecompress 解压缩 gzip 压缩的数据
func GzipDecompressObject ¶
GzipDecompressObject 泛型解压缩函数,支持自动JSON反序列化
func GzipSmartDecompress ¶
GzipSmartDecompress 智能解压缩函数 自动检测数据是否被压缩,如果是则解压,否则直接返回原数据 适用于需要兼容压缩/未压缩数据的场景
func GzipSmartDecompressObject ¶
GzipSmartDecompressObject 智能解压缩对象函数 自动检测数据是否被压缩,支持自动JSON反序列化 适用于需要兼容压缩/未压缩数据的场景
func MultiGZipCompress ¶
MultiGZipCompress 支持多次压缩
func MultiGZipCompressObject ¶
MultiGZipCompressObject 泛型多次压缩函数,支持任意类型自动JSON序列化
func MultiGZipDecompress ¶
MultiGZipDecompress 支持多次解压缩
func MultiGZipDecompressObject ¶
MultiGZipDecompressObject 泛型多次解压缩函数,支持自动JSON反序列化
func MultiZlibCompress ¶
MultiZlibCompress 支持多次压缩
func MultiZlibCompressObject ¶
MultiZlibCompressObject 泛型多次压缩函数,支持任意类型自动JSON序列化
func MultiZlibDecompress ¶
MultiZlibDecompress 支持多次解压缩
func MultiZlibDecompressObject ¶
MultiZlibDecompressObject 泛型多次解压缩函数,支持自动JSON反序列化
func ZlibCompress ¶
func ZlibCompressObject ¶
ZlibCompressObject 泛型压缩函数,支持任意类型自动JSON序列化
func ZlibCompressObjectWithSize ¶
ZlibCompressObjectWithSize 泛型压缩函数,返回压缩后的数据和原始JSON数据大小
func ZlibCompressWithLevel ¶
ZlibCompressWithLevel 按指定压缩级别进行 zlib 压缩
level 取值与 compress/flate 一致:BestSpeed(1) 到 BestCompression(9), 另可取 DefaultCompression(-1)、NoCompression(0) 与 HuffmanOnly(-2) 小载荷下换到 BestSpeed 可快约五倍而体积几乎不变,原因与实测见 base.go
func ZlibCompressWithPrefix ¶
ZlibCompressWithPrefix 压缩数据并添加 ZLIB: 前缀 返回带前缀的压缩数据,适用于需要明确标识压缩格式的场景
func ZlibDecompress ¶
ZlibDecompress 解压缩数据(使用对象池优化)
func ZlibDecompressObject ¶
ZlibDecompressObject 泛型解压缩函数,支持自动JSON反序列化
func ZlibSmartDecompress ¶
ZlibSmartDecompress 智能解压缩函数 自动检测数据是否被压缩,如果是则解压,否则直接返回原数据 适用于需要兼容压缩/未压缩数据的场景
func ZlibSmartDecompressObject ¶
ZlibSmartDecompressObject 智能解压缩对象函数 自动检测数据是否被压缩,支持自动JSON反序列化 适用于需要兼容压缩/未压缩数据的场景
Types ¶
type CompressResult ¶
type CompressResult struct {
Data []byte // 压缩后的数据
OriginalSize int // 原始数据大小(字节)
CompressedSize int // 压缩后数据大小(字节)
Ratio float64 // 压缩率(压缩后/压缩前,越小压缩效果越好)
}
CompressResult 压缩结果,包含压缩数据和统计信息
func GzipCompressObjectWithInfo ¶
func GzipCompressObjectWithInfo[T any](obj T) (*CompressResult, error)
GzipCompressObjectWithInfo 泛型压缩函数,支持任意类型自动JSON序列化并返回压缩信息
func GzipCompressWithInfo ¶
func GzipCompressWithInfo(data []byte) (*CompressResult, error)
GzipCompressWithInfo 使用 gzip 压缩数据并返回压缩信息
func GzipCompressWithPrefixInfo ¶
func GzipCompressWithPrefixInfo(data []byte) (*CompressResult, error)
GzipCompressWithPrefixInfo 压缩数据并添加 GZIP: 前缀,同时返回压缩信息
func MultiGZipCompressObjectWithInfo ¶
func MultiGZipCompressObjectWithInfo[T any](obj T, times int) (*CompressResult, error)
MultiGZipCompressObjectWithInfo 泛型多次压缩函数,支持任意类型自动JSON序列化并返回压缩信息
func MultiGZipCompressWithInfo ¶
func MultiGZipCompressWithInfo(data []byte, times int) (*CompressResult, error)
MultiGZipCompressWithInfo 支持多次压缩并返回压缩信息
func MultiZlibCompressObjectWithInfo ¶
func MultiZlibCompressObjectWithInfo[T any](obj T, times int) (*CompressResult, error)
MultiZlibCompressObjectWithInfo 泛型多次压缩函数,支持任意类型自动JSON序列化并返回压缩信息
func MultiZlibCompressWithInfo ¶
func MultiZlibCompressWithInfo(data []byte, times int) (*CompressResult, error)
MultiZlibCompressWithInfo 支持多次压缩并返回压缩信息
func ZlibCompressObjectWithInfo ¶
func ZlibCompressObjectWithInfo[T any](obj T) (*CompressResult, error)
ZlibCompressObjectWithInfo 泛型压缩函数,支持任意类型自动JSON序列化并返回压缩信息
func ZlibCompressWithInfo ¶
func ZlibCompressWithInfo(data []byte) (*CompressResult, error)
ZlibCompressWithInfo 压缩数据并返回压缩信息
func ZlibCompressWithPrefixInfo ¶
func ZlibCompressWithPrefixInfo(data []byte) (*CompressResult, error)
ZlibCompressWithPrefixInfo 压缩数据并添加 ZLIB: 前缀,同时返回压缩信息
func (*CompressResult) SavedPercent ¶
func (r *CompressResult) SavedPercent() float64
SavedPercent 返回节省的百分比