7 Star 27 Fork 2

Gitee 极速下载/goworld

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/xiaonanln/goworld
克隆/下载
zlib.go 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
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)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/goworld.git
git@gitee.com:mirrors/goworld.git
mirrors
goworld
goworld
v0.1.6

搜索帮助