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