compress

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 10 Imported by: 0

README

compress

Compression and decompression utilities supporting multiple algorithms.

Supported algorithms

Gzip, Zlib, Flate/Deflate, Zstd, Snappy (block format), LZ4 (frame format).

Key types

  • Algorithm -- algorithm identifier (AlgGzip, AlgZstd, AlgSnappy, AlgLZ4, ...)
  • Compression level constants: LevelNone, LevelBest, LevelFastest, LevelDefault

Key functions

  • GzipCompress(data, level) / GzipDecompress(data)
  • ZlibCompress(data, level) / ZlibDecompress(data)
  • FlateCompress(data, level) / FlateDecompress(data)
  • ZstdCompress(data) / ZstdDecompress(data)
  • SnappyCompress(data) / SnappyDecompress(data)
  • LZ4Compress(data) / LZ4Decompress(data)
  • NewGzipWriter(w, level) / NewGzipReader(r) -- streaming helpers

Quick start

import "github.com/LingByte/ling-base/common/compress"

data := []byte("hello world hello world hello world")

compressed, err := compress.GzipCompress(data, compress.LevelBest)
if err != nil {
    log.Fatal(err)
}

decompressed, err := compress.GzipDecompress(compressed)
if err != nil {
    log.Fatal(err)
}

License

MIT

Documentation

Overview

Package compress provides compression utilities:

  • Gzip: standard library gzip with configurable level
  • Zstd: high-ratio fast compression (github.com/klauspost/compress/zstd)
  • Snappy: fast compression, block format (github.com/klauspost/compress/snappy)
  • LZ4: fast compression, block format (github.com/pierrec/lz4/v4)
  • 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 the LZ4 frame format, which embeds the original size so decompression doesn't need an externally-provided buffer size.

func LZ4Decompress

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

LZ4Decompress decompresses LZ4 frame-format data produced by LZ4Compress.

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 the Snappy block format.

func SnappyDecompress

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

SnappyDecompress decompresses Snappy block-format 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 real Zstandard compression. Uses the default encoder level (SpeedDefault).

func ZstdDecompress

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

ZstdDecompress decompresses real 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