base-golang/pkg/compress/compress.go

39 lines
706 B
Go
Raw Normal View History

2024-07-23 10:23:43 +08:00
package compress
import (
"bytes"
"compress/zlib"
"io"
)
var _ Compress = (*compress)(nil)
type Compress interface {
DoZlibCompress(src []byte) []byte
DoZlibUnCompress(compressSrc []byte) []byte
}
type compress struct{}
func New() Compress {
return &compress{}
}
// DoZlibCompress 进行zlib压缩
func (c *compress) DoZlibCompress(src []byte) []byte {
var in bytes.Buffer
w := zlib.NewWriter(&in)
_, _ = w.Write(src)
_ = w.Close()
return in.Bytes()
}
// DoZlibUnCompress 进行zlib解压缩
func (c *compress) DoZlibUnCompress(compressSrc []byte) []byte {
b := bytes.NewReader(compressSrc)
var out bytes.Buffer
r, _ := zlib.NewReader(b)
_, _ = io.Copy(&out, r)
return out.Bytes()
}