# package zlib
`import "compress/zlib"`
zlib包实现了对zlib格式压缩数据的读写,参见[RFC 1950](http://tools.ietf.org/html/rfc1950)。
本包的实现提供了在读取时解压和写入时压缩的滤镜。例如,将压缩数据写入一个bytes.Buffer:
```
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()
```
## Index
* [Constants](#pkg-constants)
* [Variables](#pkg-variables)
* [func NewReader(r io.Reader) (io.ReadCloser, error)](#NewReader)
* [func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, error)](#NewReaderDict)
* [type Writer](#Writer)
* [func NewWriter(w io.Writer) \*Writer](#NewWriter)
* [func NewWriterLevel(w io.Writer, level int) (\*Writer, error)](#NewWriterLevel)
* [func NewWriterLevelDict(w io.Writer, level int, dict []byte) (\*Writer, error)](#NewWriterLevelDict)
* [func (z \*Writer) Close() error](#Writer.Close)
* [func (z \*Writer) Flush() error](#Writer.Flush)
* [func (z \*Writer) Reset(w io.Writer)](#Writer.Reset)
* [func (z \*Writer) Write(p []byte) (n int, err error)](#Writer.Write)
### Examples
* [NewReader](#example-NewReader)
* [NewWriter](#example-NewWriter)
## Constants
```
const (
NoCompression = flate.NoCompression
BestSpeed = flate.BestSpeed
BestCompression = flate.BestCompression
DefaultCompression = flate.DefaultCompression
)
```
这些常量都是拷贝自flate包,因此导入"compress/zlib"后,就不必再导入"compress/flate"了。
## Variables
```
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](https://github.com/golang/go/blob/master/src/compress/zlib/reader.go#L58 "View Source")
```
func NewReader(r io.Reader) (io.ReadCloser, error)
```
NewReader返回一个从r读取并解压数据的io.ReadCloser。其实现会缓冲输入流的数据,并可能从r中读取比需要的更多的数据。调用者有责任在读取完毕后调用返回值的Close方法。
Example
```
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)
```
Output:
```
hello, world
```
## func [NewReaderDict](https://github.com/golang/go/blob/master/src/compress/zlib/reader.go#L64 "View Source")
```
func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, error)
```
NewReaderDict类似NewReader,但会使用预设的字典初始化返回的Reader。
如果压缩数据没有采用字典,本函数会忽略该参数。
## type [Writer](https://github.com/golang/go/blob/master/src/compress/zlib/writer.go#L26 "View Source")
```
type Writer struct {
// 内含隐藏或非导出字段
}
```
Writer将提供给它的数据压缩后写入下层io.Writer接口。
### func [NewWriter](https://github.com/golang/go/blob/master/src/compress/zlib/writer.go#L42 "View Source")
```
func NewWriter(w io.Writer) *Writer
```
NewWriter创建并返回一个Writer。写入返回值的数据都会在压缩后写入w。
调用者有责任在结束写入后调用返回值的Close方法。因为写入的数据可能保存在缓冲中没有刷新入下层。
Example
```
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write([]byte("hello, world\n"))
w.Close()
fmt.Println(b.Bytes())
```
Output:
```
[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]
```
### func [NewWriterLevel](https://github.com/golang/go/blob/master/src/compress/zlib/writer.go#L53 "View Source")
```
func NewWriterLevel(w io.Writer, level int) (*Writer, error)
```
NewWriterLevel类似NewWriter但指定了压缩水平而不是采用默认的DefaultCompression。
参数level可以是DefaultCompression、NoCompression或BestSpeed与BestCompression之间包括二者的任何整数。如果level合法,返回的错误值为nil。
### func [NewWriterLevelDict](https://github.com/golang/go/blob/master/src/compress/zlib/writer.go#L62 "View Source")
```
func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error)
```
NewWriterLevelDict类似NewWriterLevel但还指定了用于压缩的字典。dict参数可以为nil;否则,在返回的Writer关闭之前,其内容不可被修改。
### func (\*Writer) [Reset](https://github.com/golang/go/blob/master/src/compress/zlib/writer.go#L76 "View Source")
```
func (z *Writer) Reset(w io.Writer)
```
Reset将w重置,丢弃当前的写入状态,并将下层输出目标设为dst。效果上等价于将w设为使用dst和w的压缩水平、字典重新调用NewWriterLevel或NewWriterLevelDict返回的\*Writer。
### func (\*Writer) [Write](https://github.com/golang/go/blob/master/src/compress/zlib/writer.go#L146 "View Source")
```
func (z *Writer) Write(p []byte) (n int, err error)
```
Write将p压缩后写入下层io.Writer接口。压缩后的数据不一定会立刻刷新,除非Writer被关闭或者显式的刷新。
### func (\*Writer) [Flush](https://github.com/golang/go/blob/master/src/compress/zlib/writer.go#L166 "View Source")
```
func (z *Writer) Flush() error
```
Flush将缓冲中的压缩数据刷新到下层io.Writer接口中。
### func (\*Writer) [Close](https://github.com/golang/go/blob/master/src/compress/zlib/writer.go#L179 "View Source")
```
func (z *Writer) Close() error
```
调用Close会刷新缓冲并关闭w,但不会关闭下层io.Writer接口。
- 库
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe