zstd

package module
v0.0.0-...-edd432c Latest Latest
Warning

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

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

Documentation

Overview

Package zstd provides zstd compression with two interchangeable backends:

  • a CGO-accelerated backend backed by github.com/DataDog/zstd (the native libzstd) when CGO is enabled, and
  • a pure-Go backend backed by github.com/klauspost/compress/zstd when CGO is disabled.

Both backends produce and consume standard zstd frames and are interoperable: data compressed by one can be decompressed by the other. Callers should import this package instead of either underlying library directly, so that a single build can target both CGO and non-CGO environments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compress

func Compress(dst, src []byte) ([]byte, error)

Compress compresses src at DefaultCompression. If dst is large enough it is reused, otherwise a new buffer is allocated.

Level, the compression-level constants and CompressLevel are defined in the build-tagged files (zstd_cgo.go / zstd_nocgo.go), since the level type is an alias of the active backend's native level type.

func CompressBound

func CompressBound(srcSize int) int

CompressBound returns the worst-case size needed to compress srcSize bytes. It mirrors ZSTD_COMPRESSBOUND from the zstd specification and is a valid upper bound for the output of either backend.

func CompressLevel

func CompressLevel(dst, src []byte, level Level) ([]byte, error)

CompressLevel compresses src at the given level using the native libzstd.

func Decompress

func Decompress(dst, src []byte) ([]byte, error)

Decompress decompresses src. If dst is large enough it is reused, otherwise a new buffer is allocated.

func NewReader

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

NewReader returns a streaming zstd reader.

Types

type Level

type Level = int

Level is a zstd compression level. For the CGO backend it is the native libzstd integer level (1 = fastest, higher = more compression).

const (
	BestSpeed          Level = ddzstd.BestSpeed
	BestCompression    Level = ddzstd.BestCompression
	DefaultCompression Level = ddzstd.DefaultCompression
)

Compression-level constants. These re-export the native libzstd values so we never hardcode level literals ourselves.

func LevelFromInt

func LevelFromInt(i int) Level

LevelFromInt converts a standard zstd integer level (as found in Agent configuration) to a Level. For the CGO backend Level is int, so this is an identity conversion.

type Writer

type Writer interface {
	io.WriteCloser
	Flush() error
}

Writer is a streaming zstd encoder. It extends io.WriteCloser with a Flush method so callers can flush partial frames.

func NewWriter

func NewWriter(w io.Writer) (Writer, error)

NewWriter returns a streaming zstd writer at DefaultCompression.

func NewWriterLevel

func NewWriterLevel(w io.Writer, level Level) (Writer, error)

NewWriterLevel returns a streaming zstd writer at the given level.

Jump to

Keyboard shortcuts

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