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 ¶
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 ¶
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 ¶
WithMaxDecompressedBytes overrides the maximum number of bytes DecompressBytes will produce for a single input. A value of 0 leaves the default (DefaultMaxDecompressedBytes) in place.