package zlib

Import Path
	compress/zlib (on go.dev)

Dependency Relation
	imports 8 packages, and imported by 4 packages

Involved Source Files 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: var b bytes.Buffer w := zlib.NewWriter(&b) w.Write([]byte("hello, world\n")) w.Close() and to read that data back: r, err := zlib.NewReader(&b) io.Copy(os.Stdout, r) r.Close() writer.go
Code Examples package main import ( "bytes" "compress/zlib" "io" "os" ) func main() { buff := []byte{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} b := bytes.NewReader(buff) r, err := zlib.NewReader(b) if err != nil { panic(err) } io.Copy(os.Stdout, r) r.Close() } package main import ( "bytes" "compress/zlib" "fmt" ) func main() { var b bytes.Buffer w := zlib.NewWriter(&b) w.Write([]byte("hello, world\n")) w.Close() fmt.Println(b.Bytes()) }
Package-Level Type Names (total 2)
/* sort by: | */
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. Reset discards any buffered data and resets the Resetter as if it was newly initialized with the given reader. compress/flate.Resetter (interface) Resetter : compress/flate.Resetter
A Writer takes data written to it and writes the compressed form of that data to an underlying writer (see [NewWriter]). Close closes the Writer, flushing any unwritten data to the underlying [io.Writer], but does not close the underlying io.Writer. Flush flushes the Writer to its underlying [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. 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. *Writer : internal/bisect.Writer *Writer : io.Closer *Writer : io.WriteCloser *Writer : io.Writer func NewWriter(w io.Writer) *Writer func NewWriterLevel(w io.Writer, level int) (*Writer, error) func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error)
Package-Level Functions (total 5)
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 [io.ReadCloser] returned by NewReader also implements [Resetter].
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].
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.
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.
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.
Package-Level Variables (total 3)
ErrChecksum is returned when reading ZLIB data that has an invalid checksum.
ErrDictionary is returned when reading ZLIB data that has an invalid dictionary.
ErrHeader is returned when reading ZLIB data that has an invalid header.
Package-Level Constants (total 5)
These constants are copied from the [flate] package, so that code that imports [compress/zlib] does not also have to import [compress/flate].
These constants are copied from the [flate] package, so that code that imports [compress/zlib] does not also have to import [compress/flate].
These constants are copied from the [flate] package, so that code that imports [compress/zlib] does not also have to import [compress/flate].
These constants are copied from the [flate] package, so that code that imports [compress/zlib] does not also have to import [compress/flate].
These constants are copied from the [flate] package, so that code that imports [compress/zlib] does not also have to import [compress/flate].