Documentation
¶
Overview ¶
Package lzma supports the decoding and encoding of LZMA streams. Reader and Writer support the classic LZMA format. Reader2 and Writer2 support the decoding and encoding of LZMA2 streams.
The package is written completely in Go and doesn't rely on any external library.
Example (Reader) ¶
f, err := os.Open("fox.lzma") if err != nil { log.Fatal(err) } // no need for defer; Fatal calls os.Exit(1) that doesn't execute deferred functions r, err := NewReader(bufio.NewReader(f)) if err != nil { log.Fatal(err) } _, err = io.Copy(os.Stdout, r) if err != nil { log.Fatal(err) } if err := f.Close(); err != nil { log.Fatal(err) }
Output: The quick brown fox jumps over the lazy dog.
Example (Writer) ¶
The example uses the buffered reader and writer from package bufio.
pr, pw := io.Pipe() go func() { bw := bufio.NewWriter(pw) w, err := NewWriter(bw) if err != nil { log.Fatal(err) } input := []byte("The quick brown fox jumps over the lazy dog.") if _, err = w.Write(input); err != nil { log.Fatal(err) } if err = w.Close(); err != nil { log.Fatal(err) } // reader waits for the data if err = bw.Flush(); err != nil { log.Fatal(err) } }() r, err := NewReader(pr) if err != nil { log.Fatal(err) } _, err = io.Copy(os.Stdout, r) if err != nil { log.Fatal(err) }
Output: The quick brown fox jumps over the lazy dog.
Index ¶
- Constants
- Variables
- func ByteReader(r io.Reader) io.ByteReader
- func DecodeDictCap(c byte) (n int64, err error)
- func EncodeDictCap(n int64) byte
- func ValidHeader(data []byte) bool
- type LimitedByteWriter
- type MatchAlgorithm
- type Properties
- type Reader
- type Reader2
- type Reader2Config
- type ReaderConfig
- type Writer
- type Writer2
- type Writer2Config
- type WriterConfig
Examples ¶
Constants ¶
const ( MinDictCap = 1 << 12 MaxDictCap = 1<<32 - 1 )
MinDictCap and MaxDictCap provide the range of supported dictionary capacities.
const HeaderLen = 13
HeaderLen provides the length of the LZMA file header.
Variables ¶
var ErrLimit = errors.New("limit reached")
ErrLimit indicates that the limit of the LimitedByteWriter has been reached.
var ErrNoSpace = errors.New("insufficient space")
ErrNoSpace indicates that there is insufficient space for the Write operation.
Functions ¶
func ByteReader ¶ added in v0.4.1
func ByteReader(r io.Reader) io.ByteReader
ByteReader converts an io.Reader into an io.ByteReader.
func DecodeDictCap ¶ added in v0.4.1
DecodeDictCap decodes the encoded dictionary capacity. The function returns an error if the code is out of range.
func EncodeDictCap ¶ added in v0.4.1
EncodeDictCap encodes a dictionary capacity. The function returns the code for the capacity that is greater or equal n. If n exceeds the maximum support dictionary capacity, the maximum value is returned.
func ValidHeader ¶ added in v0.4.1
ValidHeader checks for a valid LZMA file header. It allows only dictionary sizes of 2^n or 2^n+2^(n-1) with n >= 10 or 2^32-1. If there is an explicit size it must not exceed 256 GiB. The length of the data argument must be HeaderLen.
Types ¶
type LimitedByteWriter ¶ added in v0.4.1
type LimitedByteWriter struct { BW io.ByteWriter N int64 }
LimitedByteWriter provides a byte writer that can be written until a limit is reached. The field N provides the number of remaining bytes.
func (*LimitedByteWriter) WriteByte ¶ added in v0.4.1
func (l *LimitedByteWriter) WriteByte(c byte) error
WriteByte writes a single byte to the limited byte writer. It returns ErrLimit if the limit has been reached. If the byte is successfully written the field N of the LimitedByteWriter will be decremented by one.
type MatchAlgorithm ¶ added in v0.5.1
type MatchAlgorithm byte
MatchAlgorithm identifies an algorithm to find matches in the dictionary.
const ( HashTable4 MatchAlgorithm = iota BinaryTree )
Supported matcher algorithms.
func (MatchAlgorithm) String ¶ added in v0.5.1
func (a MatchAlgorithm) String() string
String returns a string representation of the Matcher.
type Properties ¶
Properties contains the parameters LC, LP and PB. The parameter LC defines the number of literal context bits; parameter LP the number of literal position bits and PB the number of position bits.
func PropertiesForCode ¶ added in v0.4.1
func PropertiesForCode(code byte) (p Properties, err error)
PropertiesForCode converts a properties code byte into a Properties value.
func (Properties) Code ¶ added in v0.4.1
func (p Properties) Code() byte
Code converts the properties to a byte. The function assumes that the properties components are all in range.
func (*Properties) String ¶ added in v0.4.1
func (p *Properties) String() string
String returns the properties in a string representation.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader provides a reader for LZMA files or streams.
func NewReader ¶
NewReader creates a new reader for an LZMA stream using the classic format. NewReader reads and checks the header of the LZMA stream.
type Reader2 ¶ added in v0.4.1
type Reader2 struct {
// contains filtered or unexported fields
}
Reader2 supports the reading of LZMA2 chunk sequences. Note that the first chunk should have a dictionary reset and the first compressed chunk a properties reset. The chunk sequence may not be terminated by an end-of-stream chunk.
func NewReader2 ¶ added in v0.4.1
NewReader2 creates a reader for an LZMA2 chunk sequence.
type Reader2Config ¶ added in v0.5.1
type Reader2Config struct {
DictCap int
}
Reader2Config stores the parameters for the LZMA2 reader. format.
func (Reader2Config) NewReader2 ¶ added in v0.5.1
func (c Reader2Config) NewReader2(lzma2 io.Reader) (r *Reader2, err error)
NewReader2 creates an LZMA2 reader using the given configuration.
func (*Reader2Config) Verify ¶ added in v0.5.1
func (c *Reader2Config) Verify() error
Verify checks the reader configuration for errors. Zero configuration values will be replaced by default values.
type ReaderConfig ¶ added in v0.5.1
type ReaderConfig struct {
DictCap int
}
ReaderConfig stores the parameters for the reader of the classic LZMA format.
func (ReaderConfig) NewReader ¶ added in v0.5.1
func (c ReaderConfig) NewReader(lzma io.Reader) (r *Reader, err error)
NewReader creates a new reader for an LZMA stream in the classic format. The function reads and verifies the the header of the LZMA stream.
func (*ReaderConfig) Verify ¶ added in v0.5.1
func (c *ReaderConfig) Verify() error
Verify checks the reader configuration for errors. Zero values will be replaced by default values.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer writes an LZMA stream in the classic format.
func NewWriter ¶
NewWriter creates a new LZMA writer using the classic format. The function writes the header to the underlying stream.
type Writer2 ¶ added in v0.4.1
type Writer2 struct {
// contains filtered or unexported fields
}
Writer2 supports the creation of an LZMA2 stream. But note that written data is buffered, so call Flush or Close to write data to the underlying writer. The Close method writes the end-of-stream marker to the stream. So you may be able to concatenate the output of two writers as long the output of the first writer has only been flushed but not closed.
Any change to the fields Properties, DictCap must be done before the first call to Write, Flush or Close.
func NewWriter2 ¶ added in v0.4.1
NewWriter2 creates an LZMA2 chunk sequence writer with the default parameters and options.
type Writer2Config ¶ added in v0.5.1
type Writer2Config struct { // The properties for the encoding. If the it is nil the value // {LC: 3, LP: 0, PB: 2} will be chosen. Properties *Properties // The capacity of the dictionary. If DictCap is zero, the value // 8 MiB will be chosen. DictCap int // Size of the lookahead buffer; value 0 indicates default size // 4096 BufSize int // Match algorithm Matcher MatchAlgorithm }
Writer2Config is used to create a Writer2 using parameters.
func (Writer2Config) NewWriter2 ¶ added in v0.5.1
func (c Writer2Config) NewWriter2(lzma2 io.Writer) (w *Writer2, err error)
NewWriter2 creates a new LZMA2 writer using the given configuration.
func (*Writer2Config) Verify ¶ added in v0.5.1
func (c *Writer2Config) Verify() error
Verify checks the Writer2Config for correctness. Zero values will be replaced by default values.
type WriterConfig ¶ added in v0.5.1
type WriterConfig struct { // Properties for the encoding. If the it is nil the value // {LC: 3, LP: 0, PB: 2} will be chosen. Properties *Properties // The capacity of the dictionary. If DictCap is zero, the value // 8 MiB will be chosen. DictCap int // Size of the lookahead buffer; value 0 indicates default size // 4096 BufSize int // Match algorithm Matcher MatchAlgorithm // SizeInHeader indicates that the header will contain an // explicit size. SizeInHeader bool // Size of the data to be encoded. A positive value will imply // than an explicit size will be set in the header. Size int64 // EOSMarker requests whether the EOSMarker needs to be written. // If no explicit size is been given the EOSMarker will be // set automatically. EOSMarker bool }
WriterConfig defines the configuration parameter for a writer.
func (WriterConfig) NewWriter ¶ added in v0.5.1
func (c WriterConfig) NewWriter(lzma io.Writer) (w *Writer, err error)
NewWriter creates a new LZMA writer for the classic format. The method will write the header to the underlying stream.
func (*WriterConfig) Verify ¶ added in v0.5.1
func (c *WriterConfig) Verify() error
Verify checks WriterConfig for errors. Verify will replace zero values with default values.
Source Files
¶
- bintree.go
- bitops.go
- breader.go
- buffer.go
- bytewriter.go
- decoder.go
- decoderdict.go
- directcodec.go
- distcodec.go
- encoder.go
- encoderdict.go
- hashtable.go
- header.go
- header2.go
- lengthcodec.go
- literalcodec.go
- matchalgorithm.go
- operation.go
- prob.go
- properties.go
- rangecodec.go
- reader.go
- reader2.go
- state.go
- treecodecs.go
- writer.go
- writer2.go