compression

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package compression provides data compression and decompression using Zstd and S2 algorithms.

Example (RoundTrip)
package main

import (
	"fmt"

	"github.com/primandproper/platform-go/v3/compression"
)

func main() {
	c, err := compression.NewCompressor("zstd")
	if err != nil {
		panic(err)
	}

	original := []byte("hello, world!")
	compressed, err := c.CompressBytes(original)
	if err != nil {
		panic(err)
	}

	decompressed, err := c.DecompressBytes(compressed)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(decompressed))
}
Output:
hello, world!

Index

Examples

Constants

View Source
const (
	// AlgorithmZstd selects the Zstandard compression algorithm.
	AlgorithmZstd Algorithm = "zstd"
	// AlgorithmS2 selects the S2 compression algorithm.
	AlgorithmS2 Algorithm = "s2"

	// DefaultMaxDecompressedBytes bounds how many bytes DecompressBytes will produce for a
	// single input, guarding against decompression bombs (a small hostile payload that expands
	// to gigabytes). It matches zstd's own default decoder memory limit. Override per-Compressor
	// with WithMaxDecompressedBytes.
	DefaultMaxDecompressedBytes uint64 = 64 << 20 // 64 MiB
)

Variables

View Source
var (
	// ErrInvalidAlgorithm is returned when an unsupported compression algorithm is requested.
	ErrInvalidAlgorithm = errors.New("invalid compression algorithm")
	// ErrDecompressedTooLarge is returned when decompressing an input would exceed the
	// configured maximum decompressed size.
	ErrDecompressedTooLarge = errors.New("decompressed output exceeds configured maximum")
)

Functions

This section is empty.

Types

type Algorithm

type Algorithm string

Algorithm identifies a supported compression algorithm. It is a named string type so callers can select an algorithm from a runtime config string via a plain conversion (e.g. Algorithm(cfg.Algorithm)).

type Compressor

type Compressor interface {
	CompressBytes(in []byte) ([]byte, error)
	DecompressBytes(in []byte) ([]byte, error)
}

Compressor compresses and decompresses byte slices.

func NewCompressor

func NewCompressor(a Algorithm, opts ...Option) (Compressor, error)

NewCompressor returns a new Compressor for the given Algorithm. An unknown or empty Algorithm yields ErrInvalidAlgorithm.

type Option

type Option func(*compressor)

Option configures a Compressor.

func WithMaxDecompressedBytes

func WithMaxDecompressedBytes(n uint64) Option

WithMaxDecompressedBytes overrides the maximum number of bytes DecompressBytes will produce for a single input. A value of 0 leaves the default (DefaultMaxDecompressedBytes) in place.

Jump to

Keyboard shortcuts

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