Package flate

import "compress/flate"
Overview
Index
Examples

Overview ▾

Package flate implements the DEFLATE compressed data format, described in RFC 1951. The gzip and zlib packages implement access to DEFLATE-based file formats.

范例(字典)

预设字典可用于提高压缩率. 使用字典的不利之处在于,压缩器和解压缩器必须事先同意要使用的字典.

Decompressed output using the dictionary:
<?xml version="1.0"?>
<book>
	<meta name="title" content="The Go Programming Language"/>
	<meta name="authors" content="Alan Donovan and Brian Kernighan"/>
	<meta name="published" content="2015-10-26"/>
	<meta name="isbn" content="978-0134190440"/>
	<data>...</data>
</book>

Substrings matched by the dictionary are marked with #:
#####################
######
	############title###########The Go Programming Language"/#
	############authors###########Alan Donovan and Brian Kernighan"/#
	############published###########2015-10-26"/#
	############isbn###########978-0134190440"/#
	######...</#####
</#####

示例(重置)

在对性能有严格要求的应用中,Reset可用于丢弃当前的压缩器或解压缩器状态,并通过利用先前分配的内存来快速将其重新初始化.

Don't communicate by sharing memory, share memory by communicating.
Concurrency is not parallelism.
The bigger the interface, the weaker the abstraction.
Documentation is for users.

示例(同步)

DEFLATE适用于在网络上传输压缩数据.

Received 1 bytes: A
Received 4 bytes: long
Received 4 bytes: time
Received 3 bytes: ago
Received 2 bytes: in
Received 1 bytes: a
Received 6 bytes: galaxy
Received 4 bytes: far,
Received 3 bytes: far
Received 7 bytes: away...

Constants

const (
    NoCompression      = 0
    BestSpeed          = 1
    BestCompression    = 9
    DefaultCompression = -1

    // HuffmanOnly disables Lempel-Ziv match searching and only performs Huffman
    // entropy encoding. This mode is useful in compressing data that has
    // already been compressed with an LZ style algorithm (e.g. Snappy or LZ4)
    // that lacks an entropy encoder. Compression gains are achieved when
    // certain bytes in the input stream occur more frequently than others.
    //
    // Note that HuffmanOnly produces a compressed output that is
    // RFC 1951 compliant. That is, any valid DEFLATE decompressor will
    // continue to be able to decompress this output.
    HuffmanOnly = -2
)

func NewReader

func NewReader(r io.Reader) io.ReadCloser

NewReader返回一个新的ReadCloser,可用于读取r的未压缩版本. 如果r还没有实现io.ByteReader,则解压缩器可能会从r读取比所需更多的数据. 完成阅读后,调用方有责任在ReadCloser上调用Close.

NewReader返回的ReadCloser也实现了Resetter.

func NewReaderDict

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

NewReaderDict类似于NewReader,但是使用预设字典初始化阅读器. 返回的Reader的行为就像未压缩的数据流以给定的字典(已被读取)开始. NewReaderDict通常用于读取由NewWriterDict压缩的数据.

NewReader返回的ReadCloser也实现了Resetter.

type CorruptInputError

CorruptInputError报告在给定偏移量下存在损坏的输入.

type CorruptInputError int64

func (CorruptInputError) Error

func (e CorruptInputError) Error() string

type InternalError

一个InternalError报告Flate代码本身中的错误.

type InternalError string

func (InternalError) Error

func (e InternalError) Error() string

type ReadError

ReadError报告读取输入时遇到的错误.

不推荐使用:不再返回.

type ReadError struct {
    Offset int64 // byte offset where error occurred
    Err    error // error returned by underlying Read
}

func (*ReadError) Error

func (e *ReadError) Error() string

type Reader

NewReader所需的实际读取接口. 如果传入的io.Reader也没有ReadByte,则NewReader将引入自己的缓冲.

type Reader interface {
    io.Reader
    io.ByteReader
}

type Resetter 1.4

重置程序重置由NewReader或NewReaderDict返回的ReadCloser,以切换到新的基础Reader. 这允许重新使用ReadCloser而不是分配新的ReadCloser.

type Resetter interface {
    // Reset discards any buffered data and resets the Resetter as if it was
    // newly initialized with the given reader.
    Reset(r io.Reader, dict []byte) error
}

type WriteError

WriteError报告写入输出时遇到的错误.

不推荐使用:不再返回.

type WriteError struct {
    Offset int64 // byte offset where error occurred
    Err    error // error returned by underlying Write
}

func (*WriteError) Error

func (e *WriteError) Error() string

type Writer

Writer接收写入其中的数据,并将该数据的压缩形式写入基础写入器(请参见NewWriter).

type Writer struct {
    // contains filtered or unexported fields
}

func NewWriter

func NewWriter(w io.Writer, level int) (*Writer, error)

NewWriter返回给定级别的新Writer压缩数据. 在zlib之后,级别范围从1(BestSpeed)到9(BestCompression); 较高的级别通常运行较慢,但压缩程度更高. 级别0(NoCompression)不尝试任何压缩; 它仅添加必要的DEFLATE帧. 级别-1(DefaultCompression)使用默认压缩级别. 级别-2(仅HuffmanOnly)将仅使用Huffman压缩,可以对所有类型的输入进行非常快速的压缩,但会牺牲可观的压缩效率.

如果级别在[-2,9]范围内,则返回的错误为零. 否则,返回的错误将为非零.

func NewWriterDict

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

NewWriterDict类似于NewWriter,但使用预设字典初始化新Writer. 返回的Writer的行为就像已将字典写入其中而不产生任何压缩的输出. 写入w的压缩数据只能由使用同一词典初始化的Reader进行解压缩.

func (*Writer) Close

func (w *Writer) Close() error

Close刷新并关闭编写器.

func (*Writer) Flush

func (w *Writer) Flush() error

刷新将所有未决数据刷新到基础写入器. 主要在压缩网络协议中有用,以确保远程读取器具有足够的数据来重构数据包. 在写入数据之前,刷新不会返回. 当没有待处理的数据时调用Flush仍然会使Writer发出至少4个字节的同步标记. 如果基础编写器返回错误,则Flush返回该错误.

在zlib库的术语中,Flush等效于Z_SYNC_FLUSH.

func (*Writer) Reset 1.2

func (w *Writer) Reset(dst io.Writer)

重置将丢弃编写器的状态,并使它等效于用dst和w的级别和字典调用的NewWriter或NewWriterDict的结果.

func (*Writer) Write

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

Write将数据写入w,最终将压缩的数据形式写入其基础写入器.

by  ICOPY.SITE