-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
gz.go
89 lines (73 loc) · 2.04 KB
/
gz.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package archiver
import (
"bytes"
"context"
"io"
"strings"
"github.com/klauspost/compress/gzip"
"github.com/klauspost/pgzip"
)
func init() {
RegisterFormat(Gz{})
}
// Gz facilitates gzip compression.
type Gz struct {
// Gzip compression level. See https://pkg.go.dev/compress/flate#pkg-constants
// for some predefined constants. If 0, DefaultCompression is assumed rather
// than no compression.
CompressionLevel int
// DisableMultistream controls whether the reader supports multistream files.
// See https://pkg.go.dev/compress/gzip#example-Reader.Multistream
DisableMultistream bool
// Use a fast parallel Gzip implementation. This is only
// effective for large streams (about 1 MB or greater).
Multithreaded bool
}
func (Gz) Extension() string { return ".gz" }
func (gz Gz) Match(_ context.Context, filename string, stream io.Reader) (MatchResult, error) {
var mr MatchResult
// match filename
if strings.Contains(strings.ToLower(filename), gz.Extension()) {
mr.ByName = true
}
// match file header
buf, err := readAtMost(stream, len(gzHeader))
if err != nil {
return mr, err
}
mr.ByStream = bytes.Equal(buf, gzHeader)
return mr, nil
}
func (gz Gz) OpenWriter(w io.Writer) (io.WriteCloser, error) {
// assume default compression level if 0, rather than no
// compression, since no compression on a gzipped file
// doesn't make any sense in our use cases
level := gz.CompressionLevel
if level == 0 {
level = gzip.DefaultCompression
}
var wc io.WriteCloser
var err error
if gz.Multithreaded {
wc, err = pgzip.NewWriterLevel(w, level)
} else {
wc, err = gzip.NewWriterLevel(w, level)
}
return wc, err
}
func (gz Gz) OpenReader(r io.Reader) (io.ReadCloser, error) {
if gz.Multithreaded {
gzR, err := pgzip.NewReader(r)
if gzR != nil && gz.DisableMultistream {
gzR.Multistream(false)
}
return gzR, err
}
gzR, err := gzip.NewReader(r)
if gzR != nil && gz.DisableMultistream {
gzR.Multistream(false)
}
return gzR, err
}
// magic number at the beginning of gzip files
var gzHeader = []byte{0x1f, 0x8b}