An open API service indexing awesome lists of open source software.

https://github.com/conneroisu/lzma-go

lzma implementation in go
https://github.com/conneroisu/lzma-go

7zip compression compression-algorithm golang golang-library golang-package golangci-lint lzma nix-flake

Last synced: 3 days ago
JSON representation

lzma implementation in go

Awesome Lists containing this project

README

        

# lzma

Package lzma package implements reading and writing of LZMA format compressed data.

### LZMA compressed file format

| 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 |

# lzma

```go
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)
```

## Index

- [Constants](<#constants>)
- [func NewReader\(r io.Reader\) io.ReadCloser](<#NewReader>)
- [func NewWriter\(w io.Writer\) \(io.WriteCloser, error\)](<#NewWriter>)
- [func NewWriterLevel\(w io.Writer, level int\) \(io.WriteCloser, error\)](<#NewWriterLevel>)
- [func NewWriterSize\(w io.Writer, size int64\) \(io.WriteCloser, error\)](<#NewWriterSize>)
- [func NewWriterSizeLevel\(w io.Writer, size int64, level int\) \(io.WriteCloser, error\)](<#NewWriterSizeLevel>)
- [type ArgumentValueError](<#ArgumentValueError>)
- [func \(e \*ArgumentValueError\) Error\(\) string](<#ArgumentValueError.Error>)
- [type HeaderError](<#HeaderError>)
- [func \(e HeaderError\) Error\(\) string](<#HeaderError.Error>)
- [type NWriteError](<#NWriteError>)
- [func \(e \*NWriteError\) Error\(\) string](<#NWriteError.Error>)
- [type Reader](<#Reader>)
- [type StreamError](<#StreamError>)
- [func \(e \*StreamError\) Error\(\) string](<#StreamError.Error>)
- [type Writer](<#Writer>)

## Constants

```go
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]()

```go
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]()

```go
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]()

```go
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](<#BestSpeed>) and [lzma.BestCompression](<#BestSpeed>).

Same as lzma.NewWriterSizeLevel\(w, \-1, level\).


## func [NewWriterSize]()

```go
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](<#BestSpeed>) and [lzma.BestCompression](<#BestSpeed>).

Parameter size and the size, [lzma.DefaultCompression](<#BestSpeed>), \(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]()

```go
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](<#BestSpeed>) and [lzma.BestCompression](<#BestSpeed>).

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.

```go
type ArgumentValueError struct {
// contains filtered or unexported fields
}
```


### func \(\*ArgumentValueError\) [Error]()

```go
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.

```go
type HeaderError struct {
// contains filtered or unexported fields
}
```


### func \(HeaderError\) [Error]()

```go
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.

```go
type NWriteError struct {
// contains filtered or unexported fields
}
```


### func \(\*NWriteError\) [Error]()

```go
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.

```go
type Reader interface {
io.Reader
ReadByte() (c byte, err error)
}
```


## type [StreamError]()

StreamError is returned when the stream is corrupt.

```go
type StreamError struct {
// contains filtered or unexported fields
}
```


### func \(\*StreamError\) [Error]()

```go
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.

```go
type Writer interface {
io.Writer
Flush() error
WriteByte(c byte) error
}
```

Generated by [gomarkdoc]()