Documentation
¶
Overview ¶
Package zstd provides Go bindings to the Zstandard (zstd) compression library using purego to avoid CGo dependencies.
This package embeds the zstd shared libraries for supported platforms and extracts them at runtime, allowing for easy cross-compilation and deployment without external dependencies.
Currently supported platforms: - Linux amd64 (glibc 2.17+) - macOS arm64 (Apple Silicon)
Index ¶
- Constants
- Variables
- func Compress(src []byte) ([]byte, error)
- func CompressBest(src []byte) ([]byte, error)
- func CompressFast(src []byte) ([]byte, error)
- func CompressLevel(src []byte, level int) ([]byte, error)
- func Decompress(src []byte, maxSize int) ([]byte, error)
- func IsError(code uint64) bool
- func NewErrorReader(err error) io.Reader
- func NewErrorWriter(err error) io.Writer
- func NewReader(r io.Reader) (io.ReadCloser, error)
- func NewWriter(w io.Writer) (io.WriteCloser, error)
- func NewWriterLevel(w io.Writer, level int) (io.WriteCloser, error)
- func VersionInfo() string
- type Dictionary
- type Error
- type Options
- type Reader
- type Writer
- type Zstd
- func (z *Zstd) Close() error
- func (z *Zstd) Compress(src []byte, level int) ([]byte, error)
- func (z *Zstd) CompressBound(srcSize int) int
- func (z *Zstd) CompressUsingDict(src []byte, dict *Dictionary, level int) ([]byte, error)
- func (z *Zstd) Decompress(src []byte, maxSize int) ([]byte, error)
- func (z *Zstd) DecompressUsingDict(src []byte, dict *Dictionary, maxSize int) ([]byte, error)
- func (z *Zstd) LoadDictionary(dictData []byte) (*Dictionary, error)
- func (z *Zstd) NewReader(r io.Reader) io.ReadCloser
- func (z *Zstd) NewWriter(w io.Writer, level int) io.WriteCloser
- func (z *Zstd) Version() uint32
- func (z *Zstd) VersionString() string
- type ZstdInBuffer
- type ZstdOutBuffer
Constants ¶
const ( // Fast compression levels (negative values) BestSpeed = 1 FastSpeed = 1 DefaultSpeed = 3 // Regular compression levels DefaultCompression = 3 BetterCompression = 7 BestCompression = 19 // Highest practical level, very slow UltraCompression = 22 // Maximum possible level )
Constants defining Zstandard compression levels
const ( // End operation modes for compressStream2 EndContinue = 0 // More data to come EndFlush = 1 // Flush pending data EndEnd = 2 // End the frame )
Constants for stream operations
const ( // Version is the semantic version of the library Version = "0.1.0" // CommitHash can be set during build using ldflags CommitHash = "unknown" )
Version information for the library
Variables ¶
var ( ErrInvalidLevel = fmt.Errorf("zstd: invalid compression level") ErrCompression = fmt.Errorf("zstd: compression error") ErrDecompression = fmt.Errorf("zstd: decompression error") ErrOutputTooSmall = fmt.Errorf("zstd: output buffer too small") ErrInputTooLarge = fmt.Errorf("zstd: input too large") ErrContextCreation = fmt.Errorf("zstd: failed to create context") ErrEmptyInput = fmt.Errorf("zstd: empty input, nothing to compress") ErrMaxSizeExceeded = fmt.Errorf("zstd: maximum size exceeded") ErrUnsupported = fmt.Errorf("zstd: unsupported platform") ErrAlreadyClosed = fmt.Errorf("zstd: already closed") )
Common errors
Functions ¶
func CompressBest ¶
CompressBest compresses the input data using a high compression level (19).
func CompressFast ¶
CompressFast compresses the input data using the fastest compression level (1).
func CompressLevel ¶
CompressLevel compresses the input data using the specified compression level. Level should be between 1 (fastest) and 22 (highest compression ratio).
func Decompress ¶
Decompress decompresses the input data. The maxSize parameter limits the maximum size of the decompressed data to prevent decompression bombs. Use 0 for the library default max size.
func NewErrorReader ¶
NewErrorReader creates a reader that always returns the specified error
func NewErrorWriter ¶
NewErrorWriter creates a writer that always returns the specified error
func NewReader ¶
func NewReader(r io.Reader) (io.ReadCloser, error)
NewReader creates an io.ReadCloser for decompressing data from the provided reader. The returned reader should be closed with Close() when done.
func NewWriter ¶
func NewWriter(w io.Writer) (io.WriteCloser, error)
NewWriter creates an io.WriteCloser for compressing data to the provided writer using the default compression level. The returned writer should be closed with Close() when done.
func NewWriterLevel ¶
NewWriterLevel creates an io.WriteCloser for compressing data to the provided writer using the specified compression level. The returned writer should be closed with Close() when done.
func VersionInfo ¶
func VersionInfo() string
VersionInfo returns a formatted string with version and build info
Types ¶
type Dictionary ¶
type Dictionary struct {
// contains filtered or unexported fields
}
Dictionary represents a pre-trained compression dictionary
type Options ¶
type Options struct {
CompressionLevel int // Compression level (1-22, default 3)
WindowSize int // Window size limit (0 = default)
ReadBufferSize int // Read buffer size for streaming operations
WriteBufferSize int // Write buffer size for streaming operations
MaxDecompressSize int64 // Maximum size limit for decompression (0 = no limit)
}
Options contains configuration options for the Zstd compressor/decompressor
func BestOptions ¶
func BestOptions() Options
BestOptions returns options optimized for compression ratio
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns the default compression options
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader implements an io.ReadCloser for reading and decompressing data
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer implements an io.WriteCloser for compressing and writing data
type Zstd ¶
type Zstd struct {
// contains filtered or unexported fields
}
Zstd represents an instance of the Zstandard library.
func New ¶
New creates a new Zstandard instance. It handles loading the appropriate library for the current platform. The returned instance should be closed with Close() when done.
func (*Zstd) Close ¶
Close releases all resources associated with the Zstd instance. After Close is called, the Zstd instance cannot be used anymore.
func (*Zstd) Compress ¶
Compress compresses the data from src and returns the compressed data. Level can be between 1 (fastest) and 22 (highest compression ratio).
func (*Zstd) CompressBound ¶
CompressBound returns the maximum compressed size in the worst case scenario.
func (*Zstd) CompressUsingDict ¶
CompressUsingDict compresses data using the dictionary
func (*Zstd) Decompress ¶
Decompress decompresses the data from src and returns the decompressed data. The maxSize parameter limits the maximum size of the decompressed data to prevent decompression bombs. Use 0 for the library default max size.
func (*Zstd) DecompressUsingDict ¶
DecompressUsingDict decompresses data using the dictionary
func (*Zstd) LoadDictionary ¶
func (z *Zstd) LoadDictionary(dictData []byte) (*Dictionary, error)
LoadDictionary loads a pre-trained dictionary for compression/decompression
func (*Zstd) NewReader ¶
func (z *Zstd) NewReader(r io.Reader) io.ReadCloser
NewReader creates an io.ReadCloser for decompressing data from the provided reader. It will read and decompress data on demand.
func (*Zstd) NewWriter ¶
NewWriter creates an io.WriteCloser for compressing data to the provided writer. The compressed data will be written to the provided writer. The caller must call Close() when done to ensure all data is flushed.
func (*Zstd) VersionString ¶
VersionString returns the library version as a string (e.g., "1.5.5")
type ZstdInBuffer ¶
ZstdInBuffer represents a buffer for zstd input operations