xz

package module
v2.0.0-dev.4 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package xz supports the compression and decompression of xz files. It supports version 1.1.0 of the specification without the non-LZMA2 filters. See http://tukaani.org/xz/xz-file-format-1.1.0.txt

Example
const text = "The quick brown fox jumps over the lazy dog."
var buf bytes.Buffer

// compress text
w, err := NewWriter(&buf)
if err != nil {
	log.Fatalf("NewWriter error %s", err)
}
if _, err := io.WriteString(w, text); err != nil {
	log.Fatalf("WriteString error %s", err)
}
if err := w.Close(); err != nil {
	log.Fatalf("w.Close error %s", err)
}

// decompress buffer and write result to stdout
r, err := NewReader(&buf)
if err != nil {
	log.Fatalf("NewReader error %s", err)
}
if _, err = io.Copy(os.Stdout, r); err != nil {
	log.Fatalf("io.Copy error %s", err)
}
Output:

The quick brown fox jumps over the lazy dog.

Index

Examples

Constants

View Source
const (
	None   byte = 0x0
	CRC32  byte = 0x1
	CRC64  byte = 0x4
	SHA256 byte = 0xa
)

Constants for the checksum methods supported by xz.

View Source
const HeaderLen = 12

HeaderLen provides the length of the xz file header.

View Source
const (
	SingleStream = 1 << iota
)

Flags for th Stat function

Variables

This section is empty.

Functions

func NewReader

func NewReader(xz io.Reader) (r io.ReadCloser, err error)

NewReader creates an io.ReadCloser. The function should never fail.

func NewReaderOptions

func NewReaderOptions(xz io.Reader, options ReaderOptions) (r io.ReadCloser, err error)

NewReaderOptions creates an xz reader using the provided configuration. If Workers are larger than one, the LZMA reader will only use single-threaded workers.

func ValidHeader

func ValidHeader(data []byte) bool

ValidHeader checks whether data is a correct xz file header. The length of data must be HeaderLen.

Types

type Info

type Info struct {
	Streams      int64
	Blocks       int64
	Uncompressed int64
	Compressed   int64
	Check        byte
}

Info provides information about an xz-compressed file.

func Stat

func Stat(r io.Reader, flags byte) (info Info, err error)

Stat provides statistics about the data in an xz file. The function will read the reader until EOF or another error.

type ReaderOptions

type ReaderOptions struct {
	// Workers defines the number of readers for parallel reading. The
	// default is the value of GOMAXPROCS.
	Workers int

	// Read a single xz stream from the underlying reader, stop and return
	// EOF. No checks are done whether the underlying reader finishes too.
	SingleStream bool

	// Runs the multiple Workers in LZMA mode. (This is an experimental
	// setup is normally not required.)
	LZMAParallel bool

	// LZMABufferSize provides the buffer size to the LZMA layer. It is only
	// required if LZMAParallel is set.
	LZMABufferSize int
}

ReaderOptions defines the parameters for the xz reader. The SingleStream parameter requests the reader to assume that the underlying stream contains only a single stream without padding.

The workers variable controls the number of parallel workers decoding the file. It only has an effect if the file was encoded in a way that it created blocks with the compressed size set in the headers. If Workers not 1 the Workers variable in LZMAConfig will be ignored.

func (*ReaderOptions) MarshalJSON

func (cfg *ReaderOptions) MarshalJSON() (p []byte, err error)

MarshalJSON creates the jason structure for a ReaderConfig value.

func (*ReaderOptions) UnmarshalJSON

func (cfg *ReaderOptions) UnmarshalJSON(p []byte) error

UnmarshalJSON parses JSON and sets the ReaderConfig accordingly.

type WriteFlushCloser

type WriteFlushCloser interface {
	io.WriteCloser
	Flush() error
}

WriteFlushCloser supports the Write, Flush and Close methods.

func NewWriter

func NewWriter(xz io.Writer) (w WriteFlushCloser, err error)

NewWriter creates a new Writer for xz-compressed data. The Writer uses the preset #5. See Preset and NewWriterOptions for changing the parameters.

func NewWriterOptions

func NewWriterOptions(xz io.Writer, options WriterOptions) (w WriteFlushCloser, err error)

NewWriterOptions creates a WriteFlushCloser instance. If multi-threading is requested by a Workers configuration larger than 1, single threading will be requested for the LZMA writer by setting the Workers variable there to 1.

type WriterOptions

type WriterOptions struct {
	// WindowSize sets the dictionary size.
	WindowSize int
	// BufferSize sets the size of the buffer used by the LZ parser.
	BufferSize int

	// Properties for the LZMA algorithm.
	Properties lzma.Properties
	// FixedProperties indicate that the Properties is indeed zero
	FixedProperties bool

	// Number of workers processing data.
	Workers int
	// LZMAParallel indicates that the parallel execution should be on the
	// LZMA level. (This is an experimental setup and should normally not be
	// used.)
	LZMAParallel bool

	// Configuration for the LZ parser.
	ParserOptions lz.Configurator

	// checksum method: CRC32, CRC64 or SHA256 (default: CRC64)
	Checksum byte

	// Forces NoChecksum (default: false)
	NoChecksum bool
}

WriterOptions describe the parameters for an xz writer. CRC64 is used as the default checksum despite the XZ specification saying a decoder must only support CRC32.

func Preset

func Preset(n int) WriterOptions

Preset returns a WriterConfig with preset parameters. Supported presets are ranging from 1 to 9 from fast to slow with increasing compression rate.

func (*WriterOptions) MarshalJSON

func (opts *WriterOptions) MarshalJSON() (p []byte, err error)

MarshalJSON creates the JSON representation of the WriterOptions value.

func (*WriterOptions) UnmarshalJSON

func (opts *WriterOptions) UnmarshalJSON(p []byte) error

UnmarshalJSON parses a JSON value and set the WriterOptions value accordingly.

Directories

Path Synopsis
internal
randtxt
Package randtxt supports the generation of random text using a trigram model for the English language.
Package randtxt supports the generation of random text using a trigram model for the English language.
stream
Package stream supports implements Streamers which track the offset in the file and allows the discarding of data.
Package stream supports implements Streamers which track the offset in the file and allows the discarding of data.
Package lzma provides support for the encoding and decoding LZMA, LZMA2 and raw LZMA streams without a header.
Package lzma provides support for the encoding and decoding LZMA, LZMA2 and raw LZMA streams without a header.

Jump to

Keyboard shortcuts

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