zipx

package
v0.19.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

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

View Source
const (
	// MinCompressLevel 最小级别:flate.HuffmanOnly(只做 Huffman 编码,不找匹配)
	MinCompressLevel = -2
	// MaxCompressLevel 最大级别:flate.BestCompression
	MaxCompressLevel = 9
)

压缩级别的取值范围,与 compress/flate 一致

View Source
const (
	// GzipPrefix 是用于标识 gzip 压缩数据的前缀
	GzipPrefix = "GZIP:"
	// GzipPrefixLen 是 gzip 前缀的长度
	GzipPrefixLen = len(GzipPrefix)
)
View Source
const (
	// ZlibPrefix 是用于标识 zlib 压缩数据的前缀
	ZlibPrefix = "ZLIB:"
	// ZlibPrefixLen 是 zlib 前缀的长度
	ZlibPrefixLen = len(ZlibPrefix)
)

Variables

This section is empty.

Functions

func GzipCompress

func GzipCompress(data []byte) ([]byte, error)

GzipCompress 使用 gzip 压缩数据

级别为 DefaultCompression若载荷只有几十到几百字节,GzipCompressWithLevel 配 BestSpeed 可快约五倍而体积几乎不变,原因见本文件「压缩级别」一节

func GzipCompressObject

func GzipCompressObject[T any](obj T) ([]byte, error)

GzipCompressObject 泛型压缩函数,支持任意类型自动JSON序列化

func GzipCompressObjectWithSize

func GzipCompressObjectWithSize[T any](obj T) ([]byte, int, error)

GzipCompressObjectWithSize 泛型压缩函数,返回压缩后的数据和原始JSON数据大小

func GzipCompressWithLevel

func GzipCompressWithLevel(data []byte, level int) ([]byte, error)

GzipCompressWithLevel 按指定压缩级别进行 gzip 压缩

level 取值与 compress/flate 一致:BestSpeed(1) 到 BestCompression(9), 另可取 DefaultCompression(-1)、NoCompression(0) 与 HuffmanOnly(-2) 级别之间的速度与体积实测见本文件「压缩级别」一节

func GzipCompressWithPrefix

func GzipCompressWithPrefix(data []byte) ([]byte, error)

GzipCompressWithPrefix 压缩数据并添加 GZIP: 前缀 返回带前缀的压缩数据,适用于需要明确标识压缩格式的场景

func GzipDecompress

func GzipDecompress(compressedData []byte) ([]byte, error)

GzipDecompress 解压缩 gzip 压缩的数据

func GzipDecompressObject

func GzipDecompressObject[T any](compressedData []byte) (T, error)

GzipDecompressObject 泛型解压缩函数,支持自动JSON反序列化

func GzipSmartDecompress

func GzipSmartDecompress(data []byte) ([]byte, error)

GzipSmartDecompress 智能解压缩函数 自动检测数据是否被压缩,如果是则解压,否则直接返回原数据 适用于需要兼容压缩/未压缩数据的场景

func GzipSmartDecompressObject

func GzipSmartDecompressObject[T any](data []byte) (T, error)

GzipSmartDecompressObject 智能解压缩对象函数 自动检测数据是否被压缩,支持自动JSON反序列化 适用于需要兼容压缩/未压缩数据的场景

func IsGzipCompressed

func IsGzipCompressed(data []byte) bool

IsGzipCompressed 检查数据是否带有 GZIP 压缩前缀

func IsZlibCompressed

func IsZlibCompressed(data []byte) bool

IsZlibCompressed 检查数据是否带有 ZLIB 压缩前缀

func MultiGZipCompress

func MultiGZipCompress(data []byte, times int) ([]byte, error)

MultiGZipCompress 支持多次压缩

func MultiGZipCompressObject

func MultiGZipCompressObject[T any](obj T, times int) ([]byte, error)

MultiGZipCompressObject 泛型多次压缩函数,支持任意类型自动JSON序列化

func MultiGZipDecompress

func MultiGZipDecompress(compressedData []byte, times int) ([]byte, error)

MultiGZipDecompress 支持多次解压缩

func MultiGZipDecompressObject

func MultiGZipDecompressObject[T any](compressedData []byte, times int) (T, error)

MultiGZipDecompressObject 泛型多次解压缩函数,支持自动JSON反序列化

func MultiZlibCompress

func MultiZlibCompress(data []byte, times int) ([]byte, error)

MultiZlibCompress 支持多次压缩

func MultiZlibCompressObject

func MultiZlibCompressObject[T any](obj T, times int) ([]byte, error)

MultiZlibCompressObject 泛型多次压缩函数,支持任意类型自动JSON序列化

func MultiZlibDecompress

func MultiZlibDecompress(compressedData []byte, times int) ([]byte, error)

MultiZlibDecompress 支持多次解压缩

func MultiZlibDecompressObject

func MultiZlibDecompressObject[T any](compressedData []byte, times int) (T, error)

MultiZlibDecompressObject 泛型多次解压缩函数,支持自动JSON反序列化

func ZlibCompress

func ZlibCompress(data []byte) ([]byte, error)

func ZlibCompressObject

func ZlibCompressObject[T any](obj T) ([]byte, error)

ZlibCompressObject 泛型压缩函数,支持任意类型自动JSON序列化

func ZlibCompressObjectWithSize

func ZlibCompressObjectWithSize[T any](obj T) ([]byte, int, error)

ZlibCompressObjectWithSize 泛型压缩函数,返回压缩后的数据和原始JSON数据大小

func ZlibCompressWithLevel

func ZlibCompressWithLevel(data []byte, level int) ([]byte, error)

ZlibCompressWithLevel 按指定压缩级别进行 zlib 压缩

level 取值与 compress/flate 一致:BestSpeed(1) 到 BestCompression(9), 另可取 DefaultCompression(-1)、NoCompression(0) 与 HuffmanOnly(-2) 小载荷下换到 BestSpeed 可快约五倍而体积几乎不变,原因与实测见 base.go

func ZlibCompressWithPrefix

func ZlibCompressWithPrefix(data []byte) ([]byte, error)

ZlibCompressWithPrefix 压缩数据并添加 ZLIB: 前缀 返回带前缀的压缩数据,适用于需要明确标识压缩格式的场景

func ZlibDecompress

func ZlibDecompress(compressedData []byte) ([]byte, error)

ZlibDecompress 解压缩数据(使用对象池优化)

func ZlibDecompressObject

func ZlibDecompressObject[T any](compressedData []byte) (T, error)

ZlibDecompressObject 泛型解压缩函数,支持自动JSON反序列化

func ZlibSmartDecompress

func ZlibSmartDecompress(data []byte) ([]byte, error)

ZlibSmartDecompress 智能解压缩函数 自动检测数据是否被压缩,如果是则解压,否则直接返回原数据 适用于需要兼容压缩/未压缩数据的场景

func ZlibSmartDecompressObject

func ZlibSmartDecompressObject[T any](data []byte) (T, error)

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) SavedBytes

func (r *CompressResult) SavedBytes() int

SavedBytes 返回节省的字节数

func (*CompressResult) SavedPercent

func (r *CompressResult) SavedPercent() float64

SavedPercent 返回节省的百分比

func (*CompressResult) String

func (r *CompressResult) String() string

String 返回压缩结果的字符串表示

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL