Documentation
¶
Overview ¶
Package xz supports the compression and decompression of xz files. It supports version 1.0.4 of the specification without the non-LZMA2 filters. See http://tukaani.org/xz/xz-file-format-1.0.4.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 ¶
const ( None byte = 0x0 CRC32 byte = 0x1 CRC64 byte = 0x4 SHA256 byte = 0xa )
Constants for the checksum methods supported by xz.
const HeaderLen = 12
HeaderLen provides the length of the xz file header.
Variables ¶
This section is empty.
Functions ¶
func ValidHeader ¶
ValidHeader checks whether data is a correct xz file header. The length of data must be HeaderLen.
Types ¶
type ParallelReader ¶
type ParallelReader struct {
ParallelReaderConfig
// contains filtered or unexported fields
}
ParallelReader decodes the blocks of an xz file concurrently. It requires random access to the input (io.ReaderAt) and its total size, because the block locations are read from the stream indexes at the end of each stream before any data is decoded. The decoded stream is presented in order through the io.Reader (or io.WriterTo) interface.
Only files consisting of multiple blocks — as produced for example by xz with a block size limit or in multi-threaded mode, or by this package's writer with WriterConfig.BlockSize — decode with real concurrency; a single-block file decodes on one worker. Memory usage is proportional to Workers times the uncompressed block size.
The ParallelReader verifies the block checks, the block sizes against the index, and the header, footer and index checksums of every stream. It is not safe for concurrent use.
func NewParallelReader ¶
func NewParallelReader(xz io.ReaderAt, size int64) (r *ParallelReader, err error)
NewParallelReader creates a reader that decodes the blocks of an xz file concurrently using the default parameters. See ParallelReader for the conditions under which this actually parallelizes.
func (*ParallelReader) Close ¶
func (r *ParallelReader) Close() error
Close stops the background workers. It must be called when the reader is abandoned before io.EOF was reached; it is a no-op otherwise. The error is always nil.
func (*ParallelReader) Read ¶
func (r *ParallelReader) Read(p []byte) (n int, err error)
Read reads the uncompressed data stream. The blocks are decoded concurrently but delivered in order.
func (*ParallelReader) Size ¶
func (r *ParallelReader) Size() int64
Size returns the total number of uncompressed bytes in the file, as recorded in the stream indexes.
type ParallelReaderConfig ¶
ParallelReaderConfig defines the parameters for the parallel xz reader. Workers is the number of blocks decoded concurrently; values below 1 select runtime.GOMAXPROCS(0).
func (ParallelReaderConfig) NewParallelReader ¶
func (c ParallelReaderConfig) NewParallelReader(xz io.ReaderAt, size int64) (r *ParallelReader, err error)
NewParallelReader creates a new parallel reader using the given configuration. It reads and verifies the stream headers, footers and indexes, but does not decode any block data yet.
func (*ParallelReaderConfig) Verify ¶
func (c *ParallelReaderConfig) Verify() error
Verify checks the configuration for errors. Zero values will be replaced by default values.
type Reader ¶
type Reader struct {
ReaderConfig
// contains filtered or unexported fields
}
Reader supports the reading of one or multiple xz streams.
Example ¶
package main
import (
"bufio"
"io"
"log"
"os"
"github.com/forkcloser/xz"
)
func main() {
f, err := os.Open("fox.xz")
if err != nil {
log.Fatalf("os.Open(%q) error %s", "fox.xz", err)
}
defer f.Close()
r, err := xz.NewReader(bufio.NewReader(f))
if err != nil {
log.Fatalf("xz.NewReader(f) 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.
type ReaderConfig ¶
ReaderConfig defines the parameters for the xz reader. The SingleStream parameter requests the reader to assume that the underlying stream contains only a single stream.
func (ReaderConfig) NewReader ¶
func (c ReaderConfig) NewReader(xz io.Reader) (r *Reader, err error)
NewReader creates an xz stream reader. The created reader will be able to process multiple streams and padding unless a SingleStream has been set in the reader configuration c.
func (*ReaderConfig) Verify ¶
func (c *ReaderConfig) Verify() error
Verify checks the reader parameters for Validity. Zero values will be replaced by default values.
type Writer ¶
type Writer struct {
WriterConfig
// contains filtered or unexported fields
}
Writer compresses data written to it. It is an io.WriteCloser.
Example ¶
package main
import (
"fmt"
"log"
"os"
"github.com/forkcloser/xz"
)
func main() {
f, err := os.Create("example.xz")
if err != nil {
log.Fatalf("os.Open(%q) error %s", "example.xz", err)
}
defer f.Close()
w, err := xz.NewWriter(f)
if err != nil {
log.Fatalf("xz.NewWriter(f) error %s", err)
}
defer w.Close()
_, err = fmt.Fprintln(w, "The brown fox jumps over the lazy dog.")
if err != nil {
log.Fatalf("fmt.Fprintln error %s", err)
}
if err = w.Close(); err != nil {
log.Fatalf("w.Close() error %s", err)
}
}
Output:
type WriterConfig ¶
type WriterConfig struct {
Properties *lzma.Properties
DictCap int
BufSize int
BlockSize int64
// checksum method: CRC32, CRC64 or SHA256 (default: CRC64)
CheckSum byte
// Forces NoChecksum (default: false)
NoCheckSum bool
// match algorithm
Matcher lzma.MatchAlgorithm
}
WriterConfig describe the parameters for an xz writer.
func (WriterConfig) NewWriter ¶
func (c WriterConfig) NewWriter(xz io.Writer) (w *Writer, err error)
NewWriter creates a new Writer using the given configuration parameters.
func (*WriterConfig) Verify ¶
func (c *WriterConfig) Verify() error
Verify checks the configuration for errors. Zero values will be replaced by default values.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gxz
command
Command gxz supports the compression and decompression of LZMA files.
|
Command gxz supports the compression and decompression of LZMA files. |
|
xb
command
Command xb supports the xz for Go project builds.
|
Command xb supports the xz for Go project builds. |
|
internal
|
|
|
gflag
Package gflag implements GNU-style command line flag parsing.
|
Package gflag implements GNU-style command line flag parsing. |
|
hash
Package hash provides rolling hashes.
|
Package hash provides rolling hashes. |
|
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. |
|
term
Package term provides the IsTerminal function.
|
Package term provides the IsTerminal function. |
|
xlog
Package xlog provides a simple logging package that allows to disable certain message categories.
|
Package xlog provides a simple logging package that allows to disable certain message categories. |
|
Package lzma supports the decoding and encoding of LZMA streams.
|
Package lzma supports the decoding and encoding of LZMA streams. |
|
Package xio provides tools to handle I/O operations.
|
Package xio provides tools to handle I/O operations. |