compress

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package compress provides compression utilities:

  • Gzip: standard library gzip with configurable level
  • Zstd: high-ratio fast compression (pure Go fallback)
  • Snappy: fast compression (pure Go fallback)
  • LZ4: fast compression (pure Go fallback)
  • Flate/Deflate: standard library compress/flate

Quick start

data := []byte("hello world hello world hello world")
compressed, _ := compress.GzipCompress(data, compress.LevelBest)
decompressed, _ := compress.GzipDecompress(compressed)

Index

Constants

View Source
const (
	LevelNone    = gzip.NoCompression
	LevelBest    = gzip.BestCompression
	LevelFastest = gzip.BestSpeed
	LevelDefault = gzip.DefaultCompression
)

Compression levels matching compress/gzip constants.

Variables

This section is empty.

Functions

func Compress

func Compress(alg Algorithm, data []byte, level int) ([]byte, error)

Compress compresses data using the specified algorithm.

func Decompress

func Decompress(alg Algorithm, data []byte) ([]byte, error)

Decompress decompresses data using the specified algorithm.

func FlateCompress

func FlateCompress(data []byte, level int) ([]byte, error)

FlateCompress compresses data using raw deflate.

func FlateDecompress

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

FlateDecompress decompresses raw deflate data.

func GzipCompress

func GzipCompress(data []byte, level int) ([]byte, error)

GzipCompress compresses data using gzip with the given level.

func GzipCompressDefault

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

GzipCompressDefault compresses with default level.

func GzipCompressStream

func GzipCompressStream(w io.Writer, r io.Reader, level int) error

GzipCompressStream compresses data from r and writes to w.

func GzipDecompress

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

GzipDecompress decompresses gzip data.

func GzipDecompressStream

func GzipDecompressStream(w io.Writer, r io.Reader) error

GzipDecompressStream decompresses gzip data from r and writes to w.

func LZ4Compress

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

LZ4Compress compresses data using LZ4-like compression. Falls back to flate BestSpeed.

func LZ4Decompress

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

LZ4Decompress decompresses LZ4 data.

func NewGzipReader

func NewGzipReader(r io.Reader) (*gzip.Reader, error)

NewGzipReader wraps an io.Reader with a gzip reader.

func NewGzipWriter

func NewGzipWriter(w io.Writer, level int) (*gzip.Writer, error)

NewGzipWriter wraps an io.Writer with a gzip writer.

func Ratio

func Ratio(original, compressed int) float64

Ratio returns the compression ratio as a percentage. e.g. 30% means compressed is 30% of original size.

func SnappyCompress

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

SnappyCompress compresses data using snappy-like compression. Falls back to flate BestSpeed.

func SnappyDecompress

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

SnappyDecompress decompresses snappy data.

func ZlibCompress

func ZlibCompress(data []byte, level int) ([]byte, error)

ZlibCompress compresses data using zlib with the given level.

func ZlibDecompress

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

ZlibDecompress decompresses zlib data.

func ZstdCompress

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

ZstdCompress compresses data using zstd-like compression. Falls back to gzip BestCompression if zstd is not available.

func ZstdDecompress

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

ZstdDecompress decompresses zstd data.

Types

type Algorithm

type Algorithm string

Algorithm represents a compression algorithm.

const (
	AlgGzip   Algorithm = "gzip"
	AlgZlib   Algorithm = "zlib"
	AlgFlate  Algorithm = "flate"
	AlgZstd   Algorithm = "zstd"
	AlgSnappy Algorithm = "snappy"
	AlgLZ4    Algorithm = "lz4"
)

func BestAlgorithm

func BestAlgorithm(data []byte) (Algorithm, []byte, error)

BestAlgorithm returns the algorithm that produces the smallest output for the given data. Tests all algorithms and returns the best one.

Jump to

Keyboard shortcuts

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