
package main import (
"bytes"
"compress/zlib"
"fmt"
"io"
"os"
) //进行zlib压缩
func DoZlibCompress(src []byte) []byte {
var in bytes.Buffer
w := zlib.NewWriter(&in)
w.Write(src)
w.Close()
return in.Bytes()
} //进行zlib解压缩
func DoZlibUnCompress(compressSrc []byte) []byte {
b := bytes.NewReader(compressSrc)
var out bytes.Buffer
r, _ := zlib.NewReader(b)
io.Copy(&out, r)
return out.Bytes()
} func main() {
buff := []byte{, , , , , , , , , , ,
, , , , , , , , , , , , , }
b := bytes.NewReader(buff)
r, err := zlib.NewReader(b)
if err != nil {
panic(err)
}
io.Copy(os.Stdout, r)
r.Close() zip := DoZlibCompress([]byte("hello, world\n"))
fmt.Println(zip)
fmt.Println(string(DoZlibUnCompress(zip)))
}