目录 搜索
archive archive/tar archive/zip bufio bufio(缓存) builtin builtin(内置包) bytes bytes(包字节) compress compress/bzip2(压缩/bzip2) compress/flate(压缩/flate) compress/gzip(压缩/gzip) compress/lzw(压缩/lzw) compress/zlib(压缩/zlib) container container/heap(容器数据结构heap) container/list(容器数据结构list) container/ring(容器数据结构ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(数据库/sql) database/sql/driver(数据库/sql/driver) debug debug/dwarf(调试/dwarf) debug/elf(调试/elf) debug/gosym(调试/gosym) debug/macho(调试/macho) debug/pe(调试/pe) debug/plan9obj(调试/plan9obj) encoding encoding(编码) encoding/ascii85(编码/ascii85) encoding/asn1(编码/asn1) encoding/base32(编码/base32) encoding/base64(编码/base64) encoding/binary(编码/binary) encoding/csv(编码/csv) encoding/gob(编码/gob) encoding/hex(编码/hex) encoding/json(编码/json) encoding/pem(编码/pem) encoding/xml(编码/xml) errors errors(错误) expvar expvar flag flag(命令行参数解析flag包) fmt fmt go go/ast(抽象语法树) go/build go/constant(常量) go/doc(文档) go/format(格式) go/importer go/parser go/printer go/scanner(扫描仪) go/token(令牌) go/types(类型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(图像) image/color(颜色) image/color/palette(调色板) image/draw(绘图) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系统) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路径) plugin plugin(插件) reflect reflect(反射) regexp regexp(正则表达式) regexp/syntax runtime runtime(运行时) runtime/debug(调试) runtime/internal/sys runtime/pprof runtime/race(竞争) runtime/trace(执行追踪器) sort sort(排序算法) strconv strconv(转换) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系统调用) testing testing(测试) testing/iotest testing/quick text text/scanner(扫描文本) text/tabwriter text/template(定义模板) text/template/parse time time(时间戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • import "compress/zlib"

  • 概述

  • 索引

  • 示例

概述

打包 zlib 实现了读取和写入zlib格式压缩数据,如 RFC 1950中所述。

实现提供了在读取和压缩期间解压缩的过滤器。例如,要将压缩数据写入缓冲区:

var b bytes.Buffer
w := zlib.NewWriter(&b)w.Write([]byte("hello, world\n"))w.Close()

并读回数据:

r, err := zlib.NewReader(&b)io.Copy(os.Stdout, r)r.Close()

参数

  • Constants(常量)

  • Variables(变量)

  • func NewReader(r io.Reader) (io.ReadCloser, error)

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

  • type Resetter

  • type Writer

  • func NewWriter(w io.Writer) *Writer

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

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

  • func (z *Writer) Close() error

  • func (z *Writer) Flush() error

  • func (z *Writer) Reset(w io.Writer)

  • func (z *Writer) Write(p []byte) (n int, err error)

示例

NewReader NewWriter

打包文件

reader.go writer.go

常量

这些常量是从 flate 包中复制的,因此导入“compress/zlib”的代码不需要导入“compress/flate”。

const (
        NoCompression      = flate.NoCompression
        BestSpeed          = flate.BestSpeed
        BestCompression    = flate.BestCompression
        DefaultCompression = flate.DefaultCompression
        HuffmanOnly        = flate.HuffmanOnly)

变量

var (        // 读取具有无效校验和的ZLIB数据时会返回ErrChecksum。
        ErrChecksum = errors.New("zlib: invalid checksum")        // 读取具有无效字典的ZLIB数据时,会返回ErrDictionary。
        ErrDictionary = errors.New("zlib: invalid dictionary")        // 读取包含无效标题的ZLIB数据时,会返回ErrHeader。
        ErrHeader = errors.New("zlib: invalid header"))

func NewReader

func NewReader(r io.Reader) (io.ReadCloser, error)

NewReader 创建一个新的 ReadCloser。从返回的 ReadCloser 读取并解压缩来自r的数据。如果 r 没有实现 io.ByteReader,解压缩程序可能会读取比r更多的数据。调用者有责任在完成时调用ReadCloser 上的 Close。

NewReader 返回的 ReadCloser 也实现了 Resetter。

示例

package mainimport ("bytes""compress/zlib""io""os")func main() {
	buff := []byte{120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207,47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 33, 231, 4, 147}
	b := bytes.NewReader(buff)

	r, err := zlib.NewReader(b)if err != nil {panic(err)}
	io.Copy(os.Stdout, r)

	r.Close()}

func NewReaderDict

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

NewReaderDict 与 NewReader 类似,但使用预设字典。如果压缩数据没有引用它,NewReaderDict 会忽略该字典。如果压缩数据引用不同的字典,则 NewReaderDict 返回 ErrDictionary。

NewReaderDict 返回的 ReadCloser 也实现了 Resetter。

type Resetter

重置器重置由 NewReader 或 NewReaderDict 返回的 ReadCloser 以切换到新的底层 Reader。这允许重新使用 ReadCloser 而不是分配新的。

type Resetter interface {        // 重置会丢弃任何缓冲数据,并将重置器重置为好像它一样        // 最新与给定的读者初始化。        Reset(r io.Reader, dict []byte) error}

type Writer

Writer 将写入数据的数据写入底层写入器(请参阅 NewWriter)。

type Writer struct {        // 包含过滤或未导出的字段}

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter 创建一个新的 Writer。写入返回的 Writer 被压缩并写入 w。

调用者有责任在完成时调用 WriteCloser 上的 Close。写入可能会被缓冲,直到关闭才会被刷新。

示例

package mainimport ("bytes""compress/zlib""fmt")func main() {var b bytes.Buffer

	w := zlib.NewWriter(&b)
	w.Write([]byte("hello, world\n"))
	w.Close()
	fmt.Println(b.Bytes())}

func NewWriterLevel

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

NewWriterLevel 就像 NewWriter,但指定压缩级别而不是假设 DefaultCompression。

压缩级别可以是DefaultCompression,NoCompression, HuffmanOnly 或 BestSpeed 和 BestCompression 之间的任何整数值。如果级别有效,返回的错误将为零。

func NewWriterLevelDict

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

NewWriterLevelDict 与 NewWriterLevel 类似,但指定要压缩的字典。

字典可能是零。如果没有,则在 Writer 关闭之前不应修改其内容。

func (*Writer) Close

func (z *Writer) Close() error

关闭Writer,将任何未写入的数据刷新到基础 io.Writer,但不关闭基础 io.Writer。

func (*Writer) Flush

func (z *Writer) Flush() error

Flush Writer 到其基础 io.Writer。

func (*Writer) Reset

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

重置清除Writer z 的状态,使其等于 NewWriterLevel 或 NewWriterLevelDict 的初始状态,并且写入 w。

func (*Writer) Write

func (z *Writer) Write(p []byte) (n int, err error)

写入一个压缩形式的 p 到底层的 io.Writer 。在写入器关闭或明确刷新之前,压缩的字节不一定会被刷新。

上一篇: 下一篇: