LZ4 compression and decompression in pure Go
  • Go 84.8%
  • Assembly 15.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Pierre Curto cd9d7a4f66
Merge pull request #230 from 0xAozora/v4
replace deprecated functions
2025-05-10 17:40:43 +02:00
.github/workflows CI: update go versions to more recent ones 2024-01-08 14:04:50 +01:00
cmd/lz4c Add CLI benchmarks 2024-12-31 15:48:48 +01:00
fuzz Fix bounds check in amd64 decoder 2021-08-28 11:25:12 +02:00
internal replace deprecated functions 2025-05-09 03:48:43 +02:00
testdata Support for Append 2025-04-26 23:32:16 +02:00
.gitignore Fuzz test UncompressBlock and fix OOB write 2020-11-16 11:44:39 +01:00
bench_test.go replace deprecated functions 2025-05-09 03:48:43 +02:00
compressing_reader.go CompressingReader: support older Go versions 2023-12-08 16:34:03 +11:00
compressing_reader_test.go replace deprecated functions 2025-05-09 03:48:43 +02:00
example_test.go Updated example: changed the len to CompressBlockBound func to determine the buffer size so that the code can be used to determine buf length for any length of string. 2022-01-12 20:46:11 -07:00
go.mod Improve allocations in blocks.go 2025-04-09 16:23:12 -04:00
go.sum go mod tidy 2020-12-04 02:14:55 +09:00
LICENSE Added LICENSE 2015-03-14 18:30:44 +01:00
lz4.go implement linked-block decompression 2021-04-03 20:22:37 +02:00
options.go CompressingReader: compressed data reader stream (#210) 2023-11-13 17:50:15 +11:00
options_gen.go first v4 commit: tests are not ready. 2020-04-20 18:43:18 +02:00
reader.go Support for Append 2025-04-26 23:32:16 +02:00
reader_test.go replace deprecated functions 2025-05-09 03:48:43 +02:00
README.md Update README.md: add @latest to cli install command 2023-12-25 05:12:14 +01:00
state.go Fix WriteTo on a started Reader, and the resulting incorrect error message 2025-03-13 18:07:31 +00:00
state_gen.go Writer tests pass 2020-04-22 09:34:49 +02:00
writer.go Writer: initialize the writer on Flush() if wasn't initialized before 2023-11-22 16:39:13 +04:00
writer_test.go replace deprecated functions 2025-05-09 03:48:43 +02:00

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.