src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

gzip.go (690B)


      1 package requestcompression
      2 
      3 import (
      4 	"bytes"
      5 	"compress/gzip"
      6 	"fmt"
      7 	"io"
      8 )
      9 
     10 func gzipCompress(input io.Reader) ([]byte, error) {
     11 	var b bytes.Buffer
     12 	w, err := gzip.NewWriterLevel(&b, gzip.DefaultCompression)
     13 	if err != nil {
     14 		return nil, fmt.Errorf("failed to create gzip writer, %v", err)
     15 	}
     16 
     17 	inBytes, err := io.ReadAll(input)
     18 	if err != nil {
     19 		return nil, fmt.Errorf("failed read payload to compress, %v", err)
     20 	}
     21 
     22 	if _, err = w.Write(inBytes); err != nil {
     23 		return nil, fmt.Errorf("failed to write payload to be compressed, %v", err)
     24 	}
     25 	if err = w.Close(); err != nil {
     26 		return nil, fmt.Errorf("failed to flush payload being compressed, %v", err)
     27 	}
     28 
     29 	return b.Bytes(), nil
     30 }