You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/codec/gzip.go

37 lines
559 B
Go

4 years ago
package codec
import (
"bytes"
"compress/gzip"
"io"
)
const unzipLimit = 100 * 1024 * 1024 // 100MB
// Gzip compresses bs.
4 years ago
func Gzip(bs []byte) []byte {
var b bytes.Buffer
w := gzip.NewWriter(&b)
w.Write(bs)
w.Close()
return b.Bytes()
}
// Gunzip uncompresses bs.
4 years ago
func Gunzip(bs []byte) ([]byte, error) {
r, err := gzip.NewReader(bytes.NewBuffer(bs))
if err != nil {
return nil, err
}
defer r.Close()
var c bytes.Buffer
if _, err = io.Copy(&c, io.LimitReader(r, unzipLimit)); err != nil {
4 years ago
return nil, err
}
return c.Bytes(), nil
}