version: 1.10

package flate

import "compress/flate"

Overview

Package flate implements the DEFLATE compressed data format, described in RFC

  1. The gzip and zlib packages implement access to DEFLATE-based file formats.


Example:

  1. // The dictionary is a string of bytes. When compressing some input data,
  2. // the compressor will attempt to substitute substrings with matches found
  3. // in the dictionary. As such, the dictionary should only contain substrings
  4. // that are expected to be found in the actual data stream.
  5. const dict = `<?xml version="1.0"?>` + `<book>` + `<data>` + `<meta name="` + `" content="`
  6. // The data to compress should (but is not required to) contain frequent
  7. // substrings that match those in the dictionary.
  8. const data = `<?xml version="1.0"?>
  9. <book>
  10. <meta name="title" content="The Go Programming Language"/>
  11. <meta name="authors" content="Alan Donovan and Brian Kernighan"/>
  12. <meta name="published" content="2015-10-26"/>
  13. <meta name="isbn" content="978-0134190440"/>
  14. <data>...</data>
  15. </book>
  16. `
  17. var b bytes.Buffer
  18. // Compress the data using the specially crafted dictionary.
  19. zw, err := flate.NewWriterDict(&b, flate.DefaultCompression, []byte(dict))
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. if _, err := io.Copy(zw, strings.NewReader(data)); err != nil {
  24. log.Fatal(err)
  25. }
  26. if err := zw.Close(); err != nil {
  27. log.Fatal(err)
  28. }
  29. // The decompressor must use the same dictionary as the compressor.
  30. // Otherwise, the input may appear as corrupted.
  31. fmt.Println("Decompressed output using the dictionary:")
  32. zr := flate.NewReaderDict(bytes.NewReader(b.Bytes()), []byte(dict))
  33. if _, err := io.Copy(os.Stdout, zr); err != nil {
  34. log.Fatal(err)
  35. }
  36. if err := zr.Close(); err != nil {
  37. log.Fatal(err)
  38. }
  39. fmt.Println()
  40. // Substitute all of the bytes in the dictionary with a '#' to visually
  41. // demonstrate the approximate effectiveness of using a preset dictionary.
  42. fmt.Println("Substrings matched by the dictionary are marked with #:")
  43. hashDict := []byte(dict)
  44. for i := range hashDict {
  45. hashDict[i] = '#'
  46. }
  47. zr = flate.NewReaderDict(&b, hashDict)
  48. if _, err := io.Copy(os.Stdout, zr); err != nil {
  49. log.Fatal(err)
  50. }
  51. if err := zr.Close(); err != nil {
  52. log.Fatal(err)
  53. }
  54. // Output:
  55. // Decompressed output using the dictionary:
  56. // <?xml version="1.0"?>
  57. // <book>
  58. // <meta name="title" content="The Go Programming Language"/>
  59. // <meta name="authors" content="Alan Donovan and Brian Kernighan"/>
  60. // <meta name="published" content="2015-10-26"/>
  61. // <meta name="isbn" content="978-0134190440"/>
  62. // <data>...</data>
  63. // </book>
  64. //
  65. // Substrings matched by the dictionary are marked with #:
  66. // #####################
  67. // ######
  68. // ############title###########The Go Programming Language"/#
  69. // ############authors###########Alan Donovan and Brian Kernighan"/#
  70. // ############published###########2015-10-26"/#
  71. // ############isbn###########978-0134190440"/#
  72. // ######...</#####
  73. // </#####


Example:

  1. proverbs := []string{
  2. "Don't communicate by sharing memory, share memory by communicating.\n",
  3. "Concurrency is not parallelism.\n",
  4. "The bigger the interface, the weaker the abstraction.\n",
  5. "Documentation is for users.\n",
  6. }
  7. var r strings.Reader
  8. var b bytes.Buffer
  9. buf := make([]byte, 32<<10)
  10. zw, err := flate.NewWriter(nil, flate.DefaultCompression)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. zr := flate.NewReader(nil)
  15. for _, s := range proverbs {
  16. r.Reset(s)
  17. b.Reset()
  18. // Reset the compressor and encode from some input stream.
  19. zw.Reset(&b)
  20. if _, err := io.CopyBuffer(zw, &r, buf); err != nil {
  21. log.Fatal(err)
  22. }
  23. if err := zw.Close(); err != nil {
  24. log.Fatal(err)
  25. }
  26. // Reset the decompressor and decode to some output stream.
  27. if err := zr.(flate.Resetter).Reset(&b, nil); err != nil {
  28. log.Fatal(err)
  29. }
  30. if _, err := io.CopyBuffer(os.Stdout, zr, buf); err != nil {
  31. log.Fatal(err)
  32. }
  33. if err := zr.Close(); err != nil {
  34. log.Fatal(err)
  35. }
  36. }
  37. // Output:
  38. // Don't communicate by sharing memory, share memory by communicating.
  39. // Concurrency is not parallelism.
  40. // The bigger the interface, the weaker the abstraction.
  41. // Documentation is for users.


Example:

  1. var wg sync.WaitGroup
  2. defer wg.Wait()
  3. // Use io.Pipe to simulate a network connection.
  4. // A real network application should take care to properly close the
  5. // underlying connection.
  6. rp, wp := io.Pipe()
  7. // Start a goroutine to act as the transmitter.
  8. wg.Add(1)
  9. go func() {
  10. defer wg.Done()
  11. zw, err := flate.NewWriter(wp, flate.BestSpeed)
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. b := make([]byte, 256)
  16. for _, m := range strings.Fields("A long time ago in a galaxy far, far away...") {
  17. // We use a simple framing format where the first byte is the
  18. // message length, followed the message itself.
  19. b[0] = uint8(copy(b[1:], m))
  20. if _, err := zw.Write(b[:1+len(m)]); err != nil {
  21. log.Fatal(err)
  22. }
  23. // Flush ensures that the receiver can read all data sent so far.
  24. if err := zw.Flush(); err != nil {
  25. log.Fatal(err)
  26. }
  27. }
  28. if err := zw.Close(); err != nil {
  29. log.Fatal(err)
  30. }
  31. }()
  32. // Start a goroutine to act as the receiver.
  33. wg.Add(1)
  34. go func() {
  35. defer wg.Done()
  36. zr := flate.NewReader(rp)
  37. b := make([]byte, 256)
  38. for {
  39. // Read the message length.
  40. // This is guaranteed to return for every corresponding
  41. // Flush and Close on the transmitter side.
  42. if _, err := io.ReadFull(zr, b[:1]); err != nil {
  43. if err == io.EOF {
  44. break // The transmitter closed the stream
  45. }
  46. log.Fatal(err)
  47. }
  48. // Read the message content.
  49. n := int(b[0])
  50. if _, err := io.ReadFull(zr, b[:n]); err != nil {
  51. log.Fatal(err)
  52. }
  53. fmt.Printf("Received %d bytes: %s\n", n, b[:n])
  54. }
  55. fmt.Println()
  56. if err := zr.Close(); err != nil {
  57. log.Fatal(err)
  58. }
  59. }()
  60. // Output:
  61. // Received 1 bytes: A
  62. // Received 4 bytes: long
  63. // Received 4 bytes: time
  64. // Received 3 bytes: ago
  65. // Received 2 bytes: in
  66. // Received 1 bytes: a
  67. // Received 6 bytes: galaxy
  68. // Received 4 bytes: far,
  69. // Received 3 bytes: far
  70. // Received 7 bytes: away...

Index

Examples

Package files

deflate.go deflatefast.go dict_decoder.go huffman_bit_writer.go huffman_code.go inflate.go token.go

Constants

  1. const (
  2. NoCompression = 0
  3. BestSpeed = 1
  4. BestCompression = 9
  5. DefaultCompression = -1
  6.  
  7. // HuffmanOnly disables Lempel-Ziv match searching and only performs Huffman
  8. // entropy encoding. This mode is useful in compressing data that has
  9. // already been compressed with an LZ style algorithm (e.g. Snappy or LZ4)
  10. // that lacks an entropy encoder. Compression gains are achieved when
  11. // certain bytes in the input stream occur more frequently than others.
  12. //
  13. // Note that HuffmanOnly produces a compressed output that is
  14. // RFC 1951 compliant. That is, any valid DEFLATE decompressor will
  15. // continue to be able to decompress this output.
  16. HuffmanOnly = -2
  17. )

func NewReader

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

NewReader returns a new ReadCloser that can be used to read the uncompressed
version of r. 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 ReadCloser when finished reading.

The ReadCloser returned by NewReader also implements Resetter.

func NewReaderDict

  1. func NewReaderDict(r io.Reader, dict []byte) io.ReadCloser

NewReaderDict is like NewReader but initializes the reader with a preset
dictionary. The returned Reader behaves as if the uncompressed data stream
started with the given dictionary, which has already been read. NewReaderDict is
typically used to read data compressed by NewWriterDict.

The ReadCloser returned by NewReader also implements Resetter.

type CorruptInputError

  1. type CorruptInputError int64

A CorruptInputError reports the presence of corrupt input at a given offset.

func (CorruptInputError) Error

  1. func (e CorruptInputError) Error() string

type InternalError

  1. type InternalError string

An InternalError reports an error in the flate code itself.

func (InternalError) Error

  1. func (e InternalError) Error() string

type ReadError

  1. type ReadError struct {
  2. Offset int64 // byte offset where error occurred
  3. Err error // error returned by underlying Read
  4. }

A ReadError reports an error encountered while reading input.

Deprecated: No longer returned.

func (*ReadError) Error

  1. func (e *ReadError) Error() string

type Reader

  1. type Reader interface {
  2. io.Reader
  3. io.ByteReader
  4. }

The actual read interface needed by NewReader. If the passed in io.Reader does
not also have ReadByte, the NewReader will introduce its own buffering.

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 WriteError

  1. type WriteError struct {
  2. Offset int64 // byte offset where error occurred
  3. Err error // error returned by underlying Write
  4. }

A WriteError reports an error encountered while writing output.

Deprecated: No longer returned.

func (*WriteError) Error

  1. func (e *WriteError) Error() string

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, level int) (*Writer, error)

NewWriter returns a new Writer compressing data at the given level. Following
zlib, levels range from 1 (BestSpeed) to 9 (BestCompression); higher levels
typically run slower but compress more. Level 0 (NoCompression) does not attempt
any compression; it only adds the necessary DEFLATE framing. Level -1
(DefaultCompression) uses the default compression level. Level -2 (HuffmanOnly)
will use Huffman compression only, giving a very fast compression for all types
of input, but sacrificing considerable compression efficiency.

If level is in the range [-2, 9] then the error returned will be nil. Otherwise
the error returned will be non-nil.

func NewWriterDict

  1. func NewWriterDict(w io.Writer, level int, dict []byte) (*Writer, error)

NewWriterDict is like NewWriter but initializes the new Writer with a preset
dictionary. The returned Writer behaves as if the dictionary had been written to
it without producing any compressed output. The compressed data written to w can
only be decompressed by a Reader initialized with the same dictionary.

func (*Writer) Close

  1. func (w *Writer) Close() error

Close flushes and closes the writer.

func (*Writer) Flush

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

Flush flushes any pending 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.
Calling Flush when there is no pending data still causes the Writer to emit a
sync marker of at least 4 bytes. 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 (w *Writer) Reset(dst io.Writer)

Reset discards the writer’s state and makes it equivalent to the result of
NewWriter or NewWriterDict called with dst and w’s level and dictionary.

func (*Writer) Write

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

Write writes data to w, which will eventually write the compressed form of data
to its underlying writer.