version: 1.10

package zlib

import "compress/zlib"

Overview

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

The implementation provides filters that uncompress during reading and compress
during writing. For example, to write compressed data to a buffer:

  1. var b bytes.Buffer
  2. w := zlib.NewWriter(&b)
  3. w.Write([]byte("hello, world\n"))
  4. w.Close()

and to read that data back:

  1. r, err := zlib.NewReader(&b)
  2. io.Copy(os.Stdout, r)
  3. r.Close()

Index

Examples

Package files

reader.go writer.go

Constants

  1. const (
  2. NoCompression = flate.NoCompression
  3. BestSpeed = flate.BestSpeed
  4. BestCompression = flate.BestCompression
  5. DefaultCompression = flate.DefaultCompression
  6. HuffmanOnly = flate.HuffmanOnly
  7. )

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

Variables

  1. var (
  2. // ErrChecksum is returned when reading ZLIB data that has an invalid checksum.
  3. ErrChecksum = errors.New("zlib: invalid checksum")
  4. // ErrDictionary is returned when reading ZLIB data that has an invalid dictionary.
  5. ErrDictionary = errors.New("zlib: invalid dictionary")
  6. // ErrHeader is returned when reading ZLIB data that has an invalid header.
  7. ErrHeader = errors.New("zlib: invalid header")
  8. )

func NewReader

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

NewReader creates a new ReadCloser. Reads from the returned ReadCloser read and
decompress data from r. If r does not implement io.ByteReader, the decompressor
may read more data than necessary from r. It is the caller’s responsibility to
call Close on the ReadCloser when done.

The ReadCloser returned by NewReader also implements Resetter.


Example:

  1. buff := []byte{120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207,
  2. 47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 33, 231, 4, 147}
  3. b := bytes.NewReader(buff)
  4. r, err := zlib.NewReader(b)
  5. if err != nil {
  6. panic(err)
  7. }
  8. io.Copy(os.Stdout, r)
  9. // Output: hello, world
  10. r.Close()

func NewReaderDict

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

type Resetter

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

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

type Writer

  1. type Writer struct {
  2. // contains filtered or unexported fields
  3. }

A Writer takes data written to it and writes the compressed form of that data to
an underlying writer (see NewWriter).

func NewWriter

  1. 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 WriteCloser when done.
Writes may be buffered and not flushed until Close.


Example:

  1. var b bytes.Buffer
  2. w := zlib.NewWriter(&b)
  3. w.Write([]byte("hello, world\n"))
  4. w.Close()
  5. fmt.Println(b.Bytes())
  6. // Output: [120 156 202 72 205 201 201 215 81 40 207 47 202 73 225 2 4 0 0 255 255 33 231 4 147]

func NewWriterLevel

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

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

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

  1. func (z *Writer) Flush() error

Flush flushes the Writer to its underlying io.Writer.

func (*Writer) Reset

  1. 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) Write

  1. func (z *Writer) Write(p []byte) (n int, err error)

Write writes a compressed form of p to the underlying io.Writer. The compressed
bytes are not necessarily flushed until the Writer is closed or explicitly
flushed.