github.com/rck/u-root@v0.0.0-20180106144920-7eb602e381bb/pkg/gzip/gzip.go (about)

     1  package gzip
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/klauspost/pgzip"
     7  )
     8  
     9  // Compress takes input from io.Reader and deflates it using pgzip
    10  // to io.Writer. Data is compressed in blocksize (KB) chunks using
    11  // upto the number of CPU cores specified.
    12  func Compress(r io.Reader, w io.Writer, level int, blocksize int, processes int) error {
    13  	zw, err := pgzip.NewWriterLevel(w, level)
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	if err := zw.SetConcurrency(blocksize*1024, processes); err != nil {
    19  		zw.Close()
    20  		return err
    21  	}
    22  
    23  	if _, err := io.Copy(zw, r); err != nil {
    24  		zw.Close()
    25  		return err
    26  	}
    27  
    28  	return zw.Close()
    29  }