Overview

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


Example:

Package files

gunzip.go

Constants

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

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

  1. var (
  2. // ErrChecksum is returned when reading GZIP data that has an invalid checksum.
  3. ErrChecksum = errors.("gzip: invalid checksum")
  4. // ErrHeader is returned when reading GZIP data that has an invalid header.
  5. ErrHeader = errors.("gzip: invalid header")
  6. )

  1. type Header struct {
  2. Comment string // comment
  3. Extra [] // "extra data"
  4. ModTime time. // modification time
  5. Name string // file name
  6. OS // operating system type
  7. }

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.

Strings must be UTF-8 encoded and may only contain Unicode code points U+0001
through U+00FF, due to limitations of the GZIP file format.

  1. Header // valid after NewReader or Reader.Reset
  2. // contains filtered or unexported fields
  3. }

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 an 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

  1. func NewReader(r .Reader) (*, error)

NewReader creates a new Reader reading the given reader. If r does not also
implement io.ByteReader, the decompressor may read more data than necessary from
r.

It is the caller’s responsibility to call Close on the Reader when done.

The Reader.Header fields will be valid in the Reader returned.

func (*Reader)

Close closes the Reader. It does not close the underlying io.Reader. In order
for the GZIP checksum to be verified, the reader must be fully consumed until
the io.EOF.

  1. func (z *) Multistream(ok bool)

Multistream controls whether the reader supports multistream files.

If enabled (the default), the Reader expects the input to be a sequence of
individually gzipped data streams, each with its own header and trailer, ending
at EOF. The effect is that the concatenation of a sequence of gzipped files is
treated as equivalent to the gzip of the concatenation of the sequence. This is
standard behavior for gzip readers.

Calling Multistream(false) disables this behavior; disabling the behavior can be
useful when reading file formats that distinguish individual gzip data streams
or mix gzip data streams with other data streams. In this mode, when the Reader
reaches the end of the data stream, Read returns io.EOF. If the underlying
reader implements io.ByteReader, it will be left positioned just after the gzip
stream. To start the next stream, call z.Reset(r) followed by
z.Multistream(false). If there is no next stream, z.Reset(r) will return io.EOF.


Example:

  1. var buf bytes.Buffer
  2. zw := gzip.NewWriter(&buf)
  3. var files = []struct {
  4. name string
  5. comment string
  6. modTime time.Time
  7. data string
  8. }{
  9. {"file-1.txt", "file-header-1", time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC), "Hello Gophers - 1"},
  10. {"file-2.txt", "file-header-2", time.Date(2007, time.March, 2, 4, 5, 6, 1, time.UTC), "Hello Gophers - 2"},
  11. }
  12. for _, file := range files {
  13. zw.Name = file.name
  14. zw.Comment = file.comment
  15. zw.ModTime = file.modTime
  16. if _, err := zw.Write([]byte(file.data)); err != nil {
  17. log.Fatal(err)
  18. }
  19. log.Fatal(err)
  20. }
  21. zw.Reset(&buf)
  22. }
  23. zr, err := gzip.NewReader(&buf)
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. for {
  28. zr.Multistream(false)
  29. fmt.Printf("Name: %s\nComment: %s\nModTime: %s\n\n", zr.Name, zr.Comment, zr.ModTime.UTC())
  30. if _, err := io.Copy(os.Stdout, zr); err != nil {
  31. log.Fatal(err)
  32. }
  33. fmt.Print("\n\n")
  34. err = zr.Reset(&buf)
  35. break
  36. }
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. }
  41. if err := zr.Close(); err != nil {
  42. log.Fatal(err)
  43. }
  44. // Output:
  45. // Name: file-1.txt
  46. // Comment: file-header-1
  47. // ModTime: 2006-02-01 03:04:05 +0000 UTC
  48. //
  49. // Hello Gophers - 1
  50. //
  51. // Name: file-2.txt
  52. // Comment: file-header-2
  53. // ModTime: 2007-03-02 04:05:06 +0000 UTC
  54. //

func (*Reader) Read

  1. func (z *Reader) Read(p []) (n int, err )

Read implements io.Reader, reading uncompressed bytes from its underlying
Reader.

func (*Reader) Reset

  1. func (z *Reader) Reset(r .Reader)

Reset discards the Reader z’s state and makes it equivalent to the result of its
original state from NewReader, but reading from r instead. This permits reusing
a Reader rather than allocating a new one.

type Writer

  1. type Writer struct {
  2. Header // written at first call to Write, Flush, or Close
  3. // contains filtered or unexported fields
  4. }

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

func

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, Flush, or Close.

  1. func NewWriterLevel(w .Writer, level ) (*Writer, )

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 (*Writer) Close

  1. func (z *Writer) Close()

Close closes the Writer by flushing any unwritten data to the underlying
io.Writer and writing the GZIP footer. It does not close the underlying
io.Writer.

func (*Writer) Flush

  1. func (z *Writer) Flush()

Flush flushes any pending compressed data to the underlying writer.

It is useful mainly in compressed network protocols, to ensure that a remote
reader has enough data to reconstruct a packet. Flush does not return until the
data has been written. If the underlying writer returns an error, Flush returns
that error.

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

func (*Writer) Reset

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

Reset discards the Writer z’s state and makes it equivalent to the result of its
original state from NewWriter or NewWriterLevel, but writing to w instead. This
permits reusing a Writer rather than allocating a new one.

  1. func (z *) Write(p []byte) (, error)