github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/gzip/gzip.go (about)

     1  // Copyright 2017-2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gzip
     6  
     7  import (
     8  	"io"
     9  
    10  	"github.com/klauspost/pgzip"
    11  )
    12  
    13  // Compress takes input from io.Reader and deflates it using pgzip
    14  // to io.Writer. Data is compressed in blocksize (KB) chunks using
    15  // upto the number of CPU cores specified.
    16  func Compress(r io.Reader, w io.Writer, level int, blocksize int, processes int) error {
    17  	zw, err := pgzip.NewWriterLevel(w, level)
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	if err := zw.SetConcurrency(blocksize*1024, processes); err != nil {
    23  		zw.Close()
    24  		return err
    25  	}
    26  
    27  	if _, err := io.Copy(zw, r); err != nil {
    28  		zw.Close()
    29  		return err
    30  	}
    31  
    32  	return zw.Close()
    33  }