You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.0 KiB
45 lines
1.0 KiB
package new
|
|
|
|
import (
|
|
"OpenIM/pkg/utils"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"io/ioutil"
|
|
)
|
|
|
|
type Compressor interface {
|
|
Compress(rawData []byte) ([]byte, error)
|
|
DeCompress(compressedData []byte) ([]byte, error)
|
|
}
|
|
type GzipCompressor struct {
|
|
compressProtocol string
|
|
}
|
|
|
|
func NewGzipCompressor() *GzipCompressor {
|
|
return &GzipCompressor{compressProtocol: "gzip"}
|
|
}
|
|
func (g *GzipCompressor) Compress(rawData []byte) ([]byte, error) {
|
|
gzipBuffer := bytes.Buffer{}
|
|
gz := gzip.NewWriter(&gzipBuffer)
|
|
if _, err := gz.Write(rawData); err != nil {
|
|
return nil, utils.Wrap(err, "")
|
|
}
|
|
if err := gz.Close(); err != nil {
|
|
return nil, utils.Wrap(err, "")
|
|
}
|
|
return gzipBuffer.Bytes(), nil
|
|
}
|
|
func (g *GzipCompressor) DeCompress(compressedData []byte) ([]byte, error) {
|
|
buff := bytes.NewBuffer(compressedData)
|
|
reader, err := gzip.NewReader(buff)
|
|
if err != nil {
|
|
return nil, utils.Wrap(err, "NewReader failed")
|
|
}
|
|
compressedData, err = ioutil.ReadAll(reader)
|
|
if err != nil {
|
|
return nil, utils.Wrap(err, "ReadAll failed")
|
|
}
|
|
_ = reader.Close()
|
|
return compressedData, nil
|
|
}
|