1 Star 0 Fork 0

wuzpdev/goworld

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
zlib.go 1.08 KB
Copy Edit Raw Blame History
package compress
import (
"bytes"
"compress/zlib"
"io"
"io/ioutil"
"github.com/xiaonanln/goworld/engine/gwioutil"
)
func NewZlibCompressor() Compressor {
fc := &zlibCompressor{}
var err error
fc.reader = newZlibReader()
if err != nil {
panic(err)
}
fc.writer, err = zlib.NewWriterLevel(ioutil.Discard, zlib.BestSpeed)
if err != nil {
panic(err)
}
return fc
}
func newZlibReader() (reader io.ReadCloser) {
w := bytes.NewBuffer(nil)
cw := zlib.NewWriter(w)
cw.Write(nil)
cw.Flush()
reader, _ = zlib.NewReader(bytes.NewReader(w.Bytes()))
return
}
type zlibCompressor struct {
writer *zlib.Writer
reader io.ReadCloser
}
func (fc *zlibCompressor) Compress(b []byte, c []byte) ([]byte, error) {
wb := bytes.NewBuffer(c)
fc.writer.Reset(wb)
n, err := fc.writer.Write(b)
if err != nil {
return nil, err
}
if n != len(b) {
return nil, errNotFullyCompressed
}
fc.writer.Flush()
return wb.Bytes(), nil
}
func (fc *zlibCompressor) Decompress(c []byte, b []byte) error {
fc.reader.(zlib.Resetter).Reset(bytes.NewReader(c), nil)
return gwioutil.ReadAll(fc.reader, b)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzpdev/goworld.git
git@gitee.com:wuzpdev/goworld.git
wuzpdev
goworld
goworld
v0.1.2

Search

0d507c66 1850385 C8b1a773 1850385