Categorygithub.com/pierrec/lz4/v4
modulepackage
4.1.22
Repository: https://github.com/pierrec/lz4.git
Documentation: pkg.go.dev

# README

lz4 : LZ4 compression in pure Go

Go Reference CI Go Report Card GitHub tag (latest SemVer)

Overview

This package provides a streaming interface to LZ4 data streams as well as low level compress and uncompress functions for LZ4 data blocks. The implementation is based on the reference C one.

Install

Assuming you have the go toolchain installed:

go get github.com/pierrec/lz4/v4

There is a command line interface tool to compress and decompress LZ4 files.

go install github.com/pierrec/lz4/v4/cmd/lz4c@latest

Usage

Usage of lz4c:
  -version
        print the program version

Subcommands:
Compress the given files or from stdin to stdout.
compress [arguments] [<file name> ...]
  -bc
        enable block checksum
  -l int
        compression level (0=fastest)
  -sc
        disable stream checksum
  -size string
        block max size [64K,256K,1M,4M] (default "4M")

Uncompress the given files or from stdin to stdout.
uncompress [arguments] [<file name> ...]

Example

// Compress and uncompress an input string.
s := "hello world"
r := strings.NewReader(s)

// The pipe will uncompress the data from the writer.
pr, pw := io.Pipe()
zw := lz4.NewWriter(pw)
zr := lz4.NewReader(pr)

go func() {
	// Compress the input string.
	_, _ = io.Copy(zw, r)
	_ = zw.Close() // Make sure the writer is closed
	_ = pw.Close() // Terminate the pipe
}()

_, _ = io.Copy(os.Stdout, zr)

// Output:
// hello world

Contributing

Contributions are very welcome for bug fixing, performance improvements...!

  • Open an issue with a proper description
  • Send a pull request with appropriate test case(s)

Contributors

Thanks to all contributors so far!

Special thanks to @Zariel for his asm implementation of the decoder.

Special thanks to @greatroar for his work on the asm implementations of the decoder for amd64 and arm64.

Special thanks to @klauspost for his work on optimizing the code.

# Functions

BlockChecksumOption enables or disables block checksum (default=false).
BlockSizeOption defines the maximum size of compressed blocks (default=Block4Mb).
ChecksumOption enables/disables all blocks or content checksum (default=true).
CompressBlock is equivalent to Compressor.CompressBlock.
CompressBlockBound returns the maximum size of a given buffer of size n, when not compressible.
CompressBlockHC is equivalent to CompressorHC.CompressBlock.
CompressionLevelOption defines the compression level (default=Fast).
ConcurrencyOption sets the number of go routines used for compression.
LegacyOption provides support for writing LZ4 frames in the legacy format.
NewCompressingReader creates a reader which reads compressed data from raw stream.
NewReader returns a new LZ4 frame decoder.
NewWriter returns a new LZ4 frame encoder.
OnBlockDoneOption is triggered when a block has been processed.
SizeOption sets the size of the original uncompressed data (default=0).
UncompressBlock uncompresses the source buffer into the destination one, and returns the uncompressed size.
UncompressBlockWithDict uncompresses the source buffer into the destination one using a dictionary, and returns the uncompressed size.
ValidFrameHeader returns a bool indicating if the given bytes slice matches a LZ4 header.

# Constants

ErrInternalUnhandledState is an internal error.
ErrInvalidBlockChecksum is returned when reading a frame.
ErrInvalidFrame is returned when reading an invalid LZ4 archive.
ErrInvalidFrameChecksum is returned when reading a frame.
ErrInvalidHeaderChecksum is returned when reading a frame.
ErrInvalidSourceShortBuffer is returned by UncompressBlock or CompressBLock when a compressed block is corrupted or the destination buffer is not large enough for the uncompressed data.
ErrOptionClosedOrError is returned when an option is applied to a closed or in error object.
ErrOptionInvalidBlockSize is returned when.
ErrOptionInvalidCompressionLevel is returned when the supplied compression level is invalid.
ErrOptionNotApplicable is returned when trying to apply an option to an object not supporting it.
ErrWriterNotClosed is returned when attempting to reset an unclosed writer.

# Variables

Default options.
Default options.
Default options.

# Structs

A Compressor compresses data into the LZ4 block format.
A CompressorHC compresses data into the LZ4 block format.
Reader allows reading an LZ4 stream.
Writer allows writing an LZ4 stream.

# Type aliases

BlockSizeIndex defines the size of the blocks to be compressed.
CompressionLevel defines the level of compression to use.