compression

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package compression provides unified compression/decompression utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoDecompress

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

AutoDecompress automatically detects compression type and decompresses data.

func Close

func Close(c Compressor)

Close closes a compressor if it implements Closeable.

Types

type Closeable

type Closeable interface {
	Close()
}

Closeable is an optional interface for compressors that hold resources.

type Compressor

type Compressor interface {
	// Compress compresses the input data
	Compress(data []byte) ([]byte, error)
	// Decompress decompresses the input data
	Decompress(data []byte) ([]byte, error)
	// Type returns the compression type
	Type() Type
	// Name returns the human-readable name of the compressor
	Name() string
}

Compressor provides a unified interface for compression operations.

func Best

func Best() Compressor

Best returns a compressor optimized for compression ratio.

func Default

func Default() Compressor

Default returns the default compressor (zstd with default level). Falls back to gzip if zstd initialization fails.

func Fast

func Fast() Compressor

Fast returns a fast compressor optimized for speed.

func New

func New(t Type, level Level) (Compressor, error)

New creates a compressor by type and level.

type GzipCompressor

type GzipCompressor struct {
	// contains filtered or unexported fields
}

GzipCompressor implements Compressor using gzip.

func NewGzipCompressor

func NewGzipCompressor(level Level) *GzipCompressor

NewGzipCompressor creates a new gzip compressor.

func (*GzipCompressor) Compress

func (c *GzipCompressor) Compress(data []byte) ([]byte, error)

Compress compresses data using gzip.

func (*GzipCompressor) Decompress

func (c *GzipCompressor) Decompress(data []byte) ([]byte, error)

Decompress decompresses gzip data.

func (*GzipCompressor) Name

func (c *GzipCompressor) Name() string

Name returns "gzip".

func (*GzipCompressor) Type

func (c *GzipCompressor) Type() Type

Type returns TypeGzip.

type Level

type Level int

Level represents the compression level.

const (
	// LevelFastest prioritizes speed over compression ratio
	LevelFastest Level = 1
	// LevelDefault balances speed and compression ratio
	LevelDefault Level = 3
	// LevelBest prioritizes compression ratio over speed
	LevelBest Level = 9
)

type NoOpCompressor

type NoOpCompressor struct{}

NoOpCompressor is a pass-through compressor that does not compress data.

func NewNoOpCompressor

func NewNoOpCompressor() *NoOpCompressor

NewNoOpCompressor creates a new no-op compressor.

func (*NoOpCompressor) Compress

func (c *NoOpCompressor) Compress(data []byte) ([]byte, error)

Compress returns the data unchanged.

func (*NoOpCompressor) Decompress

func (c *NoOpCompressor) Decompress(data []byte) ([]byte, error)

Decompress returns the data unchanged.

func (*NoOpCompressor) Name

func (c *NoOpCompressor) Name() string

Name returns "none".

func (*NoOpCompressor) Type

func (c *NoOpCompressor) Type() Type

Type returns TypeNone.

type Type

type Type uint8

Type represents the compression algorithm used.

const (
	// TypeGzip uses gzip compression (legacy, slower but widely compatible)
	TypeGzip Type = 0
	// TypeZstd uses zstd compression (faster and better compression ratio)
	TypeZstd Type = 1
	// TypeNone represents no compression
	TypeNone Type = 255
)

func DetectType

func DetectType(data []byte) Type

DetectType detects the compression type from magic bytes. Returns TypeGzip for gzip (0x1f 0x8b), TypeZstd for zstd (0x28 0xb5 0x2f 0xfd).

type ZstdCompressor

type ZstdCompressor struct {
	// contains filtered or unexported fields
}

ZstdCompressor implements Compressor using zstd.

func NewZstdCompressor

func NewZstdCompressor(level Level) (*ZstdCompressor, error)

NewZstdCompressor creates a new zstd compressor. The compressor is reusable and thread-safe for encoding.

func (*ZstdCompressor) Close

func (c *ZstdCompressor) Close()

Close releases resources used by the compressor.

func (*ZstdCompressor) Compress

func (c *ZstdCompressor) Compress(data []byte) ([]byte, error)

Compress compresses data using zstd.

func (*ZstdCompressor) Decompress

func (c *ZstdCompressor) Decompress(data []byte) ([]byte, error)

Decompress decompresses zstd data.

func (*ZstdCompressor) Name

func (c *ZstdCompressor) Name() string

Name returns "zstd".

func (*ZstdCompressor) Type

func (c *ZstdCompressor) Type() Type

Type returns TypeZstd.

Jump to

Keyboard shortcuts

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