Let's you use zlib
in Go. Direct fully-featured API bindings to zlib
.
It improves on the current standard libraries zlib and deflate by supporting all the possible options for zlib
.
Easy to use Writer
and Reader
interfaces in addition to direct and memory-safe access to zlib low-level methods.
At the time of writing (Dec 2023), these are the montivations to write this library:
- Hidden configurations: The standard libraries zlib and deflate hide most of the configurations of the underlying compression algorithm. A library like 4kills/go-zlib exposes some of those configurations like
compression strategies
andcompression levels
but still hides other parameters likeWindowBits
andMemoryLevel
. This library exposes all these configurations in addition to giving more control on memory consumption via theBufferSize
parameter. - Limited IO interfaces: The prevously mentioned libraries implement
Writer
interface for compression andReader
interface for decompression. However, there are cases where a compression reader or a decompression writer are needed. This library supports both interfaces for both compression and decompression. - Access to lower level APIs: The previously mentioned libraries only support
Reader
andWriter
interfaces. In addition to supporting these interfaces, this library gives access tocompressor
anddecompressor
viaFeederConsumer
interface which allows incremental feeding of data and consumption of results. In addition to that, it gives direct access to zlibzstream
and its methods.
go get github.com/MeenaAlfons/go-zlib
This library assumes that zlib
is already installed and its include and library files are accessable to build tools.
Many operating systems come with zlib
already installed. If you need to install zlib
, here is a list of commands to help:
- macos:
brew install zlib
- debian:
apt install zlib
- alpine:
apk add zlib
- windows: download binary here
Compression and decompression can be plugged into any pipeline via Writer
and Reader
interfaces. Compression and decompression are supported on both writing and reading sides. Here are the possilities:
NewCompressReader
NewCompressWriter
NewDecompressReader
NewDecompressWriter
Many examples can be found in examples directory. Here is one example:
data := []byte("Hello World!")
r := bytes.NewReader(data)
opts := common.DefaultCompressOptions()
compressorReader, err := zlib.NewCompressReader(r, opts)
if err != nil {
// Error creating compressor reader
}
compressedData, err := io.ReadAll(compressorReader)
if err != nil {
// Error reading from compressor reader
}
return compressedData
Run tests
go test ./...
Run tests with debug logs
go test ./... -tags debug
- zlib is written in C by Jean-loup Gailly and Mark Adler.