VictoriaMetrics/vendor/github.com/aws/smithy-go/private/requestcompression/gzip.go

31 lines
690 B
Go
Raw Normal View History

2023-12-11 09:48:36 +01:00
package requestcompression
import (
"bytes"
"compress/gzip"
"fmt"
"io"
)
func gzipCompress(input io.Reader) ([]byte, error) {
var b bytes.Buffer
w, err := gzip.NewWriterLevel(&b, gzip.DefaultCompression)
if err != nil {
return nil, fmt.Errorf("failed to create gzip writer, %v", err)
}
inBytes, err := io.ReadAll(input)
if err != nil {
return nil, fmt.Errorf("failed read payload to compress, %v", err)
}
if _, err = w.Write(inBytes); err != nil {
return nil, fmt.Errorf("failed to write payload to be compressed, %v", err)
}
if err = w.Close(); err != nil {
return nil, fmt.Errorf("failed to flush payload being compressed, %v", err)
}
return b.Bytes(), nil
}