多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# package flate `import "compress/flate"` flate包实现了deflate压缩数据格式,参见[RFC 1951](http://tools.ietf.org/html/rfc1951)。gzip包和zlib包实现了对基于deflate的文件格式的访问。 ## Index * [Constants](#pkg-constants) * [type CorruptInputError](#CorruptInputError) * [func (e CorruptInputError) Error() string](#CorruptInputError.Error) * [type InternalError](#InternalError) * [func (e InternalError) Error() string](#InternalError.Error) * [type ReadError](#ReadError) * [func (e \*ReadError) Error() string](#ReadError.Error) * [type WriteError](#WriteError) * [func (e \*WriteError) Error() string](#WriteError.Error) * [type Reader](#Reader) * [func NewReader(r io.Reader) io.ReadCloser](#NewReader) * [func NewReaderDict(r io.Reader, dict []byte) io.ReadCloser](#NewReaderDict) * [type Writer](#Writer) * [func NewWriter(w io.Writer, level int) (\*Writer, error)](#NewWriter) * [func NewWriterDict(w io.Writer, level int, dict []byte) (\*Writer, error)](#NewWriterDict) * [func (w \*Writer) Reset(dst io.Writer)](#Writer.Reset) * [func (w \*Writer) Write(data []byte) (n int, err error)](#Writer.Write) * [func (w \*Writer) Flush() error](#Writer.Flush) * [func (w \*Writer) Close() error](#Writer.Close) ## Constants ``` const ( NoCompression = 0 BestSpeed = 1 BestCompression = 9 DefaultCompression = -1 ) ``` ## type [CorruptInputError](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L26 "View Source") ``` type CorruptInputError int64 ``` CorruptInputError表示在输入的指定偏移量位置存在损坏。 ### func (CorruptInputError) [Error](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L28 "View Source") ``` func (e CorruptInputError) Error() string ``` ## type [InternalError](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L33 "View Source") ``` type InternalError string ``` InternalError表示flate数据自身的错误。 ### func (InternalError) [Error](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L35 "View Source") ``` func (e InternalError) Error() string ``` ## type [ReadError](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L38 "View Source") ``` type ReadError struct { Offset int64 // 错误出现的位置(字节偏移量) Err error // 下层的读取操作返回的错误 } ``` ReadError代表在读取输入流时遇到的错误。 ### func (\*ReadError) [Error](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L43 "View Source") ``` func (e *ReadError) Error() string ``` ## type [WriteError](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L48 "View Source") ``` type WriteError struct { Offset int64 // 错误出现的位置(字节偏移量) Err error // 下层的写入操作返回的错误 } ``` WriteError代表在写入输出流时遇到的错误。 ### func (\*WriteError) [Error](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L53 "View Source") ``` func (e *WriteError) Error() string ``` ## type [Reader](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L181 "View Source") ``` type Reader interface { io.Reader io.ByteReader } ``` NewReader真正需要的接口。如果提供的Io.Reader没有提供ReadByte方法,NewReader函数会自行添加缓冲。 ### func [NewReader](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L684 "View Source") ``` func NewReader(r io.Reader) io.ReadCloser ``` NewReader返回一个从r读取并解压数据的io.ReadCloser。调用者有责任在读取完毕后调用返回值的Close方法。 ### func [NewReaderDict](https://github.com/golang/go/blob/master/src/compress/flate/inflate.go#L699 "View Source") ``` func NewReaderDict(r io.Reader, dict []byte) io.ReadCloser ``` NewReaderDict类似NewReader,但会使用预设的字典初始化返回的Reader。 返回的Reader表现的好像原始未压缩的数据流以该字典起始(并已经被读取)。NewReaderDict用于读取NewWriterDict压缩的数据。 ## type [Writer](https://github.com/golang/go/blob/master/src/compress/flate/deflate.go#L526 "View Source") ``` type Writer struct { // 内含隐藏或非导出字段 } ``` Writer将提供给它的数据压缩后写入下层的io.Writer接口。 ### func [NewWriter](https://github.com/golang/go/blob/master/src/compress/flate/deflate.go#L485 "View Source") ``` func NewWriter(w io.Writer, level int) (*Writer, error) ``` NewWriter返回一个压缩水平为level的Writer。 和zlib包一样,level的范围是1(BestSpeed)到9 (BestCompression)。值越大,压缩效果越好,但也越慢;level为0表示不尝试做任何压缩,只添加必需的deflate框架;level为-1时会使用默认的压缩水平;如果level在[-1, 9]范围内,error返回值将是nil,否则将返回非nil的错误值。 ### func [NewWriterDict](https://github.com/golang/go/blob/master/src/compress/flate/deflate.go#L499 "View Source") ``` func NewWriterDict(w io.Writer, level int, dict []byte) (*Writer, error) ``` NewWriterDict类似NewWriter,但会使用预设的字典初始化返回的Writer。 返回的Writer表现的好像已经将原始、未压缩数据dict(压缩后未产生任何数据的)写入w了,使用w压缩的数据只能被使用同样的字典初始化生成的Reader接口解压缩。(类似加解密的初始向量/密钥) ### func (\*Writer) [Reset](https://github.com/golang/go/blob/master/src/compress/flate/deflate.go#L558 "View Source") ``` func (w *Writer) Reset(dst io.Writer) ``` Reset将w重置,丢弃当前的写入状态,并将下层输出目标设为dst。效果上等价于将w设为使用dst和w的压缩水平、字典重新调用NewWriter或NewWriterDict返回的\*Writer。 ### func (\*Writer) [Write](https://github.com/golang/go/blob/master/src/compress/flate/deflate.go#L533 "View Source") ``` func (w *Writer) Write(data []byte) (n int, err error) ``` Write向w写入数据,最终会将压缩后的数据写入下层io.Writer接口。 ### func (\*Writer) [Flush](https://github.com/golang/go/blob/master/src/compress/flate/deflate.go#L544 "View Source") ``` func (w *Writer) Flush() error ``` Flush将缓冲中的压缩数据刷新到下层io.Writer接口中。 本方法主要用在传输压缩数据的网络连接中,以保证远端的接收者可以获得足够的数据来重构数据报。Flush会阻塞直到所有缓冲中的数据都写入下层io.Writer接口后才返回。如果下层的io.Writetr接口返回一个错误,Flush也会返回该错误。在zlib包的术语中,Flush方法等价于Z_SYNC_FLUSH。 ### func (\*Writer) [Close](https://github.com/golang/go/blob/master/src/compress/flate/deflate.go#L551 "View Source") ``` func (w *Writer) Close() error ``` Close刷新缓冲并关闭w。