compress

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 3 Imported by: 0

README

Compress (压缩)

提供数据压缩与解压的通用接口及实现。

接口定义

type Compress interface {
    Compress(data []byte) ([]byte, error)
    Decompress(data []byte) ([]byte, error)
}

支持的算法

GZIP

使用 Go 标准库 compress/gzip 实现。

  • NewGZIP
    • 签名: func NewGZIP() Compress
    • 描述: 创建 GZIP 压缩器实例。

示例

import "github.com/fireflycore/go-utils/compress"

func main() {
    c := compress.NewGZIP()
    
    // 压缩
    compressed, err := c.Compress([]byte("hello world"))
    if err != nil {
        panic(err)
    }

    // 解压
    original, err := c.Decompress(compressed)
    if err != nil {
        panic(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Compress

type Compress interface {
	// Compress 将原始数据压缩为字节切片
	// 如果压缩过程中发生错误,返回 error
	Compress(data []byte) ([]byte, error)

	// Decompress 将压缩后的数据解压为原始字节切片
	// 如果解压过程中发生错误(如数据损坏),返回 error
	Decompress(data []byte) ([]byte, error)
}

Compress 定义数据压缩与解压的通用接口 实现该接口的类型应当提供线程安全的压缩和解压方法

func NewGZIP

func NewGZIP() Compress

NewGZIP 创建一个新的 GZIP 压缩器实例。

type GZIP

type GZIP struct{}

GZIP 实现 Compress 接口,提供基于 gzip 的压缩与解压能力。 注意:该实现会将输入与输出完整加载到内存中,不适合超大数据。

func (*GZIP) Compress

func (g *GZIP) Compress(data []byte) ([]byte, error)

Compress 压缩数据

func (*GZIP) Decompress

func (g *GZIP) Decompress(data []byte) ([]byte, error)

Decompress 使用 gzip 算法解压数据 data: gzip 格式的压缩数据 返回解压后的原始数据,如果数据格式不正确或读取失败则返回错误

Jump to

Keyboard shortcuts

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