compressx

package
v0.23.0 Latest Latest
Warning

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

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

Documentation

Overview

Package compressx 提供压缩算法注册表(roadmap 11.10-⑨ 压缩算法扩展):

  • Algorithm 是压缩算法名:gzip(默认)/ zstd / brotli;Parse 对非法值报错 (不静默回退 gzip——显式语义,调用方决定 400 还是回退)。
  • NewReader / NewWriter 封装三个实现(zstd 用 klauspost/compress/zstd, brotli 用 andybalholm/brotli——均纯 Go、社区活跃、API 稳定)。

使用场景:归档(archive)压缩与存储传输的可选压缩;zstd 流**不具备** gzip 的 逐块可读性(有 frame 边界),分块传输场景须按「块级」(每 chunk 独立 frame) 组装,见 design 2026-09-24-compression-zstd.md。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewReader

func NewReader(algo Algorithm, r io.Reader) (io.ReadCloser, error)

NewReader 构造按算法解压的读取器。未知算法 / 构造失败返回 error。

func NewWriter

func NewWriter(algo Algorithm, w io.Writer, level int) (io.WriteCloser, error)

NewWriter 构造按算法压缩的写入器(level 为压缩级别:0 = 算法默认)。 未知算法 / 非法 level 返回 error。返回的 io.WriteCloser 必须 Close 才能 完成流收尾(zstd/brotli 的 frame 尾部在 Close 时写出)。

Types

type Algorithm

type Algorithm string

Algorithm 是压缩算法名(gzip 默认;zstd/brotli 为扩展)。

const (
	// Gzip 是默认压缩算法(零回归,与既有归档/传输行为一致)。
	Gzip Algorithm = "gzip"
	// Zstd 是高压缩比扩展(klauspost/compress/zstd)。
	Zstd Algorithm = "zstd"
	// Brotli 是归档备选算法(andybalholm/brotli)。
	Brotli Algorithm = "brotli"
)

func Parse

func Parse(s string) (Algorithm, error)

Parse 解析压缩算法名(大小写不敏感)。非法值返回 error(不静默回退 gzip)。

func (Algorithm) ContentType

func (a Algorithm) ContentType() string

ContentType 返回该算法的归档 Content-Type(gzip→application/gzip(RFC 6713), zstd→application/zstd(RFC 8878),brotli→application/x-brotli(未注册,惯例前缀))。

func (Algorithm) Ext

func (a Algorithm) Ext() string

Ext 返回归档文件扩展名(gzip→gz / zstd→zst / brotli→br,如 .tar.gz / .tar.zst)。

Jump to

Keyboard shortcuts

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