Package gzip
Overview ?
Overview ?
Package gzip implements reading and writing of gzip format compressed files, as specified in RFC 1952.
Index
- Constants
- Variables
- type Header
- type Reader
- func NewReader(r io.Reader) (*Reader, error)
- func (z *Reader) Close() error
- func (z *Reader) Read(p []byte) (n int, err error)
- type Writer
- func NewWriter(w io.Writer) *Writer
- func NewWriterLevel(w io.Writer, level int) (*Writer, error)
- func (z *Writer) Close() error
- func (z *Writer) Write(p []byte) (int, error)
Package files
Constants
const ( NoCompression = flate.NoCompression BestSpeed = flate.BestSpeed BestCompression = flate.BestCompression DefaultCompression = flate.DefaultCompression )
These constants are copied from the flate package, so that code that imports "compress/gzip" does not also have to import "compress/flate".
Variables
var ( // ErrChecksum is returned when reading GZIP data that has an invalid checksum. ErrChecksum = errors.New("gzip: invalid checksum") // ErrHeader is returned when reading GZIP data that has an invalid header. ErrHeader = errors.New("gzip: invalid header") )
type Header
type Header struct { Comment string // comment Extra []byte // "extra data" ModTime time.Time // modification time Name string // file name OS byte // operating system type }
The gzip file stores a header giving metadata about the compressed file. That header is exposed as the fields of the Writer and Reader structs.
type Reader
type Reader struct {
Header
// contains filtered or unexported fields
}
A Reader 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 Reader return the concatenation of the uncompressed data of each. Only the first header is recorded in the Reader fields.
Gzip files store a length and checksum of the uncompressed data. The Reader will return a ErrChecksum 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 io.EOF marking the end of the data.
func NewReader
func NewReader(r io.Reader) (*Reader, error)
NewReader creates a new Reader 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 Reader when done.
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)
type Writer
type Writer struct {
Header
// contains filtered or unexported fields
}
A Writer is an io.WriteCloser that satisfies writes by compressing data written to its wrapped io.Writer.
func NewWriter
func NewWriter(w io.Writer) *Writer
NewWriter creates a new Writer that satisfies writes by compressing data 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.
Callers that wish to set the fields in Writer.Header must do so before the first call to Write or Close. The Comment and Name header fields are UTF-8 strings in Go, but the underlying format requires NUL-terminated ISO 8859-1 (Latin-1). NUL or non-Latin-1 runes in those strings will lead to an error on Write.
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, or any integer value between BestSpeed and BestCompression inclusive. The error returned will be nil if the level is valid.
func (*Writer) Close
func (z *Writer) Close() error
Close closes the Writer. It does not close the underlying io.Writer.
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 until the Writer is closed.
Except as noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code is licensed under a BSD license.
Terms of Service | Privacy Policy