compression

package
v0.6.20 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Rendered for darwin/amd64

Overview

Package compression provides zero-cgo Go bindings to Apple's /usr/lib/libcompression.dylib via purego.

It supports buffer-level compression and decompression (Encode, Decode) for LZ4, LZ4Raw, LZFSE, Zlib, LZMA, and LZBitmap, as well as streaming io.WriteCloser and io.ReadCloser adapters (NewWriter, NewReader) for the stream-capable algorithms (LZ4, LZFSE, Zlib, LZMA).

Basic usage:

encoded, err := compression.Encode(compression.LZFSE, data)
if err != nil {
	return err
}
decoded, err := compression.Decode(compression.LZFSE, encoded)
if err != nil {
	return err
}
Example
package main

import (
	"bytes"
	"fmt"

	"github.com/tmc/apple/x/compression"
)

func main() {
	src := bytes.Repeat([]byte("apple-libcompression-"), 64)
	encoded, err := compression.Encode(compression.LZFSE, src)
	if err != nil {
		panic(err)
	}
	decoded, err := compression.Decode(compression.LZFSE, encoded)
	if err != nil {
		panic(err)
	}
	fmt.Printf("equal=%v compressed=%v\n", bytes.Equal(src, decoded), len(encoded) < len(src))
}
Output:
equal=true compressed=true

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupported reports that libcompression or the requested operation
	// (such as streaming with LZ4Raw or LZBitmap) is not supported.
	ErrUnsupported = errors.New("compression: unsupported")

	// ErrInvalidAlgorithm reports an unrecognized [Algorithm] value.
	ErrInvalidAlgorithm = errors.New("compression: invalid algorithm")

	// ErrCorrupt reports malformed, truncated, or un-decodable compressed input.
	ErrCorrupt = errors.New("compression: corrupt input")

	// ErrClosed reports an operation attempted on a closed [Writer] or [Reader].
	ErrClosed = errors.New("compression: closed")

	// ErrFailed reports an internal libcompression encoding or decoding failure.
	ErrFailed = errors.New("compression: operation failed")
)

Functions

func Decode

func Decode(alg Algorithm, src []byte) ([]byte, error)

Decode decompresses src using alg and returns the uncompressed bytes.

func Encode

func Encode(alg Algorithm, src []byte) ([]byte, error)

Encode compresses src using alg and returns the compressed bytes.

Types

type Algorithm

type Algorithm uint32

Algorithm identifies a libcompression codec.

const (
	// LZ4 is Apple's framed LZ4 format (high speed, moderate ratio).
	LZ4 Algorithm = 0x100

	// LZ4Raw is unframed raw LZ4 (buffer API only).
	LZ4Raw Algorithm = 0x101

	// Zlib is raw DEFLATE (RFC 1951, level 5 encoder).
	Zlib Algorithm = 0x205

	// LZMA is LZMA (level 6 encoder, high ratio).
	LZMA Algorithm = 0x306

	// LZBitmap is Apple's LZBitmap codec (buffer API only, macOS 12+).
	LZBitmap Algorithm = 0x702

	// LZFSE is Apple's LZFSE codec (balanced speed and ratio).
	LZFSE Algorithm = 0x801
)

func (Algorithm) String

func (a Algorithm) String() string

String returns the canonical name of the algorithm.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader decompresses data read from an underlying io.Reader. Callers must call Reader.Close to release native libcompression stream state.

The zero value is not usable; create a Reader with NewReader.

func NewReader

func NewReader(r io.Reader, alg Algorithm) (*Reader, error)

NewReader returns a streaming decompressor reading from r using alg. Only stream-capable algorithms (LZ4, LZFSE, Zlib, LZMA) are supported; LZ4Raw and LZBitmap return an error wrapping ErrUnsupported.

func (*Reader) Close

func (r *Reader) Close() error

Close frees native stream resources. It does not close the underlying reader. Close is idempotent.

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

Read decompresses data into p.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer compresses data written to it and writes the compressed stream to an underlying io.Writer. Callers must call Writer.Close to flush the final frame and release native libcompression stream state.

The zero value is not usable; create a Writer with NewWriter.

func NewWriter

func NewWriter(w io.Writer, alg Algorithm) (*Writer, error)

NewWriter returns a streaming compressor writing to w using alg. Only stream-capable algorithms (LZ4, LZFSE, Zlib, LZMA) are supported; LZ4Raw and LZBitmap return an error wrapping ErrUnsupported.

Example
package main

import (
	"bytes"
	"fmt"
	"io"

	"github.com/tmc/apple/x/compression"
)

func main() {
	var buf bytes.Buffer
	w, err := compression.NewWriter(&buf, compression.LZ4)
	if err != nil {
		panic(err)
	}
	if _, err := io.WriteString(w, "streaming lz4 payload"); err != nil {
		panic(err)
	}
	if err := w.Close(); err != nil {
		panic(err)
	}

	r, err := compression.NewReader(&buf, compression.LZ4)
	if err != nil {
		panic(err)
	}
	defer r.Close()

	out, err := io.ReadAll(r)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(out))
}
Output:
streaming lz4 payload

func (*Writer) Close

func (w *Writer) Close() error

Close flushes any buffered compressed data, writes the end-of-stream marker, and frees native stream resources. It does not close the underlying writer. Close is idempotent.

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write compresses p into the underlying writer.

Jump to

Keyboard shortcuts

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