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.
|
|
package util
import ( "compress/gzip" "fmt" "io" "sync" )
var ( gzipReaderPool = sync.Pool{ New: func() interface{} { return new(gzip.Reader) //return gzip.NewReader()
}, }
gzipWriterPool = sync.Pool{ New: func() interface{} { w, _ := gzip.NewWriterLevel(nil, gzip.BestSpeed) return w }, } )
func GzipStream(w io.Writer, r io.Reader) (int64, error) { gw, ok := gzipWriterPool.Get().(*gzip.Writer) if !ok { return 0, fmt.Errorf("gzip: new writer error") } gw.Reset(w) defer func() { gw.Close() gzipWriterPool.Put(gw) }() return io.Copy(gw, r) }
func GunzipStream(w io.Writer, r io.Reader) (int64, error) { gr, ok := gzipReaderPool.Get().(*gzip.Reader) if !ok { return 0, fmt.Errorf("gzip: new reader error") }
if err := gr.Reset(r); err != nil { return 0, err } defer func() { gr.Close() gzipReaderPool.Put(gr) }() return io.Copy(w, gr) }
|