zlib

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package zlib implements reading and writing of zlib format compressed data, as specified in RFC 1950.

This is a parallel drop-in replacement for "compress/zlib". Writes to a Writer are split into blocks and compressed in parallel using DEFLATE.

Index

Constants

View Source
const (
	NoCompression       = flate.NoCompression
	BestSpeed           = flate.BestSpeed
	BestCompression     = flate.BestCompression
	DefaultCompression  = flate.DefaultCompression
	ConstantCompression = flate.ConstantCompression
	HuffmanOnly         = flate.HuffmanOnly
)

These constants are copied from the flate package, so that code that imports "compress/zlib" does not also have to import "compress/flate".

Variables

View Source
var (
	// ErrChecksum is returned when reading ZLIB data that has an invalid checksum.
	ErrChecksum = errors.New("zlib: invalid checksum")
	// ErrDictionary is returned when reading ZLIB data that has an invalid dictionary.
	ErrDictionary = errors.New("zlib: invalid dictionary")
	// ErrHeader is returned when reading ZLIB data that has an invalid header.
	ErrHeader = errors.New("zlib: invalid header")
)

Functions

func NewReader

func NewReader(r io.Reader) (io.ReadCloser, error)

NewReader creates a new ReadCloser reading the given reader. The implementation buffers input and may read more data than necessary from r. It is the caller's responsibility to call Close on the ReadCloser when done.

The io.ReadCloser returned by NewReader also implements Resetter.

func NewReaderDict

func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, error)

NewReaderDict is like NewReader but uses a preset dictionary. NewReaderDict ignores the dictionary if the compressed data does not refer to it. If the compressed data refers to a different dictionary, NewReaderDict returns ErrDictionary.

The ReadCloser returned by NewReaderDict also implements Resetter.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

A Reader is an io.ReadCloser that can be read to retrieve uncompressed data from a zlib-format compressed file.

func NewReaderN

func NewReaderN(r io.Reader, blockSize, blocks int) (*Reader, error)

NewReaderN creates a new Reader reading the given reader with custom block settings.

func NewReaderNDict

func NewReaderNDict(r io.Reader, dict []byte, blockSize, blocks int) (*Reader, error)

NewReaderNDict creates a new Reader reading the given reader with a preset dictionary and custom block settings.

func (*Reader) Close

func (z *Reader) Close() error

Close closes the Reader. It does not close the underlying io.Reader.

func (*Reader) Read

func (z *Reader) Read(p []byte) (n int, err error)

Read reads uncompressed data from the underlying reader.

func (*Reader) Reset

func (z *Reader) Reset(r io.Reader, dict []byte) error

Reset discards the Reader z's state and makes it equivalent to the result of its original state from NewReaderDict, but reading from r instead.

func (*Reader) WriteTo

func (z *Reader) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes uncompressed data directly to w.

type Resetter

type Resetter interface {
	// Reset discards any buffered data and resets the Resetter as if it was
	// newly initialized with the given reader.
	Reset(r io.Reader, dict []byte) error
}

Resetter resets a ReadCloser returned by NewReader or NewReaderDict to switch to a new underlying Reader. This permits reusing a ReadCloser instead of allocating a new one.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

A Writer is an io.WriteCloser. Writes to a Writer are compressed and written to w.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer. Writes to the returned Writer are compressed and written to w.

It is the caller's responsibility to call Close on the Writer when done. Writes may be buffered and not flushed until Close.

func NewWriterLevel

func NewWriterLevel(w io.Writer, level int) (*Writer, error)

NewWriterLevel is like NewWriter but specifies the compression level instead of assuming DefaultCompression.

The compression level can be DefaultCompression, NoCompression, HuffmanOnly or any integer value between BestSpeed and BestCompression inclusive. The error returned will be nil if the level is valid.

func NewWriterLevelDict

func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error)

NewWriterLevelDict is like NewWriterLevel but specifies a dictionary to compress with.

The dictionary may be nil. If not, its contents should not be modified until the Writer is closed.

func (*Writer) Close

func (z *Writer) Close() error

Close closes the Writer, flushing any unwritten data to the underlying io.Writer, but does not close the underlying io.Writer.

func (*Writer) Flush

func (z *Writer) Flush() error

Flush flushes any pending compressed data to the underlying writer.

In the terminology of the zlib library, Flush is equivalent to Z_SYNC_FLUSH.

func (*Writer) Reset

func (z *Writer) Reset(w io.Writer)

Reset clears the state of the Writer z such that it is equivalent to its initial state from NewWriterLevel or NewWriterLevelDict, but instead writing to w.

func (*Writer) SetConcurrency

func (z *Writer) SetConcurrency(blockSize, blocks int) error

SetConcurrency can finetune the concurrency level if needed.

With this you can control the approximate size of your blocks, as well as how many you want to be processing in parallel.

Default values for this is SetConcurrency(defaultBlockSize, runtime.GOMAXPROCS(0)), meaning blocks are split at 1 MB and up to the number of CPU threads can be processing at once before the writer blocks.

func (*Writer) UncompressedSize

func (z *Writer) UncompressedSize() int

UncompressedSize will return the number of bytes written.

func (*Writer) Write

func (z *Writer) Write(p []byte) (int, error)

Write writes a compressed form of p to the underlying io.Writer. The compressed bytes are not necessarily flushed to output until the Writer is closed or Flush() is called.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL