Package lzma package implements reading and writing of LZMA format compressed data.
Offset | Size | Description |
---|---|---|
0 | 1 | Special LZMA properties (lc,lp, pb in encoded form) |
1 | 4 | Dictionary size (little endian) |
5 | 8 | Uncompressed size (little endian). Size -1 stands for unknown size |
import "github.com/conneroisu/lzma-go"
Package lzma package implements reading and writing of LZMA format compressed data.
Reference implementation is LZMA SDK version 4.65 originally developed by Igor Pavlov, available online at:
http://www.7-zip.org/sdk.html
Usage examples. Write compressed data to a buffer:
var b bytes.Buffer
w := lzma.NewWriter(&b)
w.Write([]byte("hello, world\n"))
w.Close()
read that data back:
r := lzma.NewReader(&b)
io.Copy(os.Stdout, r)
r.Close()
If the data is bigger than you'd like to hold into memory, use pipes. Write compressed data to an io.PipeWriter:
pr, pw := io.Pipe()
go func() {
defer pw.Close()
w := lzma.NewWriter(pw)
defer w.Close()
// the bytes.Buffer would be an io.Reader used to read uncompressed data from
io.Copy(w, bytes.NewBuffer([]byte("hello, world\n")))
}()
and read it back:
defer pr.Close()
r := lzma.NewReader(pr)
defer r.Close()
// the os.Stdout would be an io.Writer used to write uncompressed data to
io.Copy(os.Stdout, r)
- Constants
- func NewReader(r io.Reader) io.ReadCloser
- func NewWriter(w io.Writer) (io.WriteCloser, error)
- func NewWriterLevel(w io.Writer, level int) (io.WriteCloser, error)
- func NewWriterSize(w io.Writer, size int64) (io.WriteCloser, error)
- func NewWriterSizeLevel(w io.Writer, size int64, level int) (io.WriteCloser, error)
- type ArgumentValueError
- type HeaderError
- type NWriteError
- type Reader
- type StreamError
- type Writer
const (
// BestSpeed is the fastest compression level.
BestSpeed = 1
// BestCompression is the compression level that gives the best compression ratio.
BestCompression = 9
// DefaultCompression is the default compression level.
DefaultCompression = 5
)
func NewReader
func NewReader(r io.Reader) io.ReadCloser
NewReader returns a new io.ReadCloser that can be used to read the uncompressed version of `r`
It is the caller's responsibility to call Close on the io.ReadCloser when finished reading.
func NewWriter
func NewWriter(w io.Writer) (io.WriteCloser, error)
NewWriter creates a new Writer that compresses data to the given Writer using the default compression level.
Same as NewWriterSizeLevel(w, -1, DefaultCompression).
func NewWriterLevel
func NewWriterLevel(w io.Writer, level int) (io.WriteCloser, error)
NewWriterLevel creates a new Writer that compresses data to the given Writer using the given level.
Level is any integer value between lzma.BestSpeed and lzma.BestCompression.
Same as lzma.NewWriterSizeLevel(w, -1, level).
func NewWriterSize
func NewWriterSize(w io.Writer, size int64) (io.WriteCloser, error)
NewWriterSize creates a new Writer that compresses data to the given Writer using the given size as the uncompressed data size.
If size is unknown, use -1 instead.
Level is any integer value between lzma.BestSpeed and lzma.BestCompression.
Parameter size and the size, lzma.DefaultCompression, (the lzma header) are written to the passed in writer before any compressed data.
If size is -1, last bytes are encoded in a different way to mark the end of the stream. The size of the compressed data will increase by 5 or 6 bytes.
Same as NewWriterSizeLevel(w, size, lzma.DefaultCompression).
func NewWriterSizeLevel
func NewWriterSizeLevel(w io.Writer, size int64, level int) (io.WriteCloser, error)
NewWriterSizeLevel writes to the given Writer the compressed version of data written to the returned io.WriteCloser. It is the caller's responsibility to call Close on the io.WriteCloser when done.
Parameter size is the actual size of uncompressed data that's going to be written to io.WriteCloser. If size is unknown, use -1 instead.
Parameter level is any integer value between lzma.BestSpeed and lzma.BestCompression.
Arguments size and level (the lzma header) are written to the writer before any compressed data.
If size is -1, last bytes are encoded in a different way to mark the end of the stream. The size of the compressed data will increase by 5 or 6 bytes.
The reason for which size is an argument is that, unlike gzip which appends the size and the checksum at the end of the stream, lzma stores the size before any compressed data. Thus, lzma can compute the size while reading data from pipe.
type ArgumentValueError
An ArgumentValueError reports an error encountered while parsing user provided arguments.
type ArgumentValueError struct {
// contains filtered or unexported fields
}
func (*ArgumentValueError) Error
func (e *ArgumentValueError) Error() string
Error returns the error message and implements the error interface on the ArgumentValueError type.
type HeaderError
HeaderError is returned when the header is corrupt.
type HeaderError struct {
// contains filtered or unexported fields
}
func (HeaderError) Error
func (e HeaderError) Error() string
Error returns the error message and implements the error interface on the HeaderError type.
type NWriteError
NWriteError is returned when the number of bytes returned by Writer.Write() didn't meet expectances.
type NWriteError struct {
// contains filtered or unexported fields
}
func (*NWriteError) Error
func (e *NWriteError) Error() string
Error returns the error message and implements the error interface on the NWriteError type.
type Reader
Reader is the actual read interface needed by [NewDecoder].
If the passed in io.Reader does not also have ReadByte, the [NewDecoder] will introduce its own buffering.
type Reader interface {
io.Reader
ReadByte() (c byte, err error)
}
type StreamError
StreamError is returned when the stream is corrupt.
type StreamError struct {
// contains filtered or unexported fields
}
func (*StreamError) Error
func (e *StreamError) Error() string
Error returns the error message and implements the error interface on the StreamError type.
type Writer
Writer is the actual write interface needed by [NewEncoder].
If the passed in io.Writer does not also have WriteByte and Flush, the [NewEncoder] function will wrap it into a bufio.Writer.
type Writer interface {
io.Writer
Flush() error
WriteByte(c byte) error
}
Generated by gomarkdoc