github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/transport/compress.go (about) 1 package transport 2 3 import ( 4 "bytes" 5 "compress/gzip" 6 "io/ioutil" 7 ) 8 9 // Unzip unzips data. 10 func Unzip(data []byte) ([]byte, error) { 11 gr, err := gzip.NewReader(bytes.NewBuffer(data)) 12 if err != nil { 13 return nil, err 14 } 15 defer gr.Close() 16 data, err = ioutil.ReadAll(gr) 17 if err != nil { 18 return nil, err 19 } 20 return data, err 21 } 22 23 // Zip zips data. 24 func Zip(data []byte) ([]byte, error) { 25 var buf bytes.Buffer 26 w := gzip.NewWriter(&buf) 27 _, err := w.Write(data) 28 if err != nil { 29 return nil, err 30 } 31 err = w.Flush() 32 if err != nil { 33 return nil, err 34 } 35 err = w.Close() 36 if err != nil { 37 return nil, err 38 } 39 40 return buf.Bytes(), nil 41 }