The Go Programming Language

Package gzip

import "compress/gzip"

Package gzip implements reading and writing of gzip format compressed files, as specified in RFC 1952.

Package files

gunzip.go gzip.go

Constants

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

const (
    NoCompression      = flate.NoCompression
    BestSpeed          = flate.BestSpeed
    BestCompression    = flate.BestCompression
    DefaultCompression = flate.DefaultCompression
)

Variables

var ChecksumError = os.NewError("gzip checksum error")
var HeaderError = os.NewError("invalid gzip header")

type Compressor

A Compressor is an io.WriteCloser that satisfies writes by compressing data written to its wrapped io.Writer.

type Compressor struct {
    Header
    // contains filtered or unexported fields
}

func NewWriter

func NewWriter(w io.Writer) (*Compressor, os.Error)

NewWriter calls NewWriterLevel with the default compression level.

func NewWriterLevel

func NewWriterLevel(w io.Writer, level int) (*Compressor, os.Error)

NewWriterLevel creates a new Compressor writing to the given writer. Writes may be buffered and not flushed until Close. Callers that wish to set the fields in Compressor.Header must do so before the first call to Write or Close. It is the caller's responsibility to call Close on the WriteCloser when done. level is the compression level, which can be DefaultCompression, NoCompression, or any integer value between BestSpeed and BestCompression (inclusive).

func (*Compressor) Close

func (z *Compressor) Close() os.Error

Calling Close does not close the wrapped io.Writer originally passed to NewWriter.

func (*Compressor) Write

func (z *Compressor) Write(p []byte) (int, os.Error)

type Decompressor

An Decompressor is an io.Reader that can be read to retrieve uncompressed data from a gzip-format compressed file.

In general, a gzip file can be a concatenation of gzip files, each with its own header. Reads from the Decompressor return the concatenation of the uncompressed data of each. Only the first header is recorded in the Decompressor fields.

Gzip files store a length and checksum of the uncompressed data. The Decompressor will return a ChecksumError when Read reaches the end of the uncompressed data if it does not have the expected length or checksum. Clients should treat data returned by Read as tentative until they receive the successful (zero length, nil error) Read marking the end of the data.

type Decompressor struct {
    Header
    // contains filtered or unexported fields
}

func NewReader

func NewReader(r io.Reader) (*Decompressor, os.Error)

NewReader creates a new Decompressor 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 Decompressor when done.

func (*Decompressor) Close

func (z *Decompressor) Close() os.Error

Calling Close does not close the wrapped io.Reader originally passed to NewReader.

func (*Decompressor) Read

func (z *Decompressor) Read(p []byte) (n int, err os.Error)

The gzip file stores a header giving metadata about the compressed file. That header is exposed as the fields of the Compressor and Decompressor structs.

type Header struct {
    Comment string // comment
    Extra   []byte // "extra data"
    Mtime   uint32 // modification time (seconds since January 1, 1970)
    Name    string // file name
    OS      byte   // operating system type
}

Bugs

Comments and Names don't properly map UTF-8 character codes outside of the 0x00-0x7f range to ISO 8859-1 (Latin-1).

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.