-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlib.go
56 lines (42 loc) · 931 Bytes
/
zlib.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
package compressor
import (
"bytes"
"compress/zlib"
"io"
"strings"
)
// Zlib facilitates zlib compression.
type Zlib struct {
CompressionLevel int
}
var ZlibHeader = []byte{0x78}
func init() {
RegisterFormat(Zlib{})
}
func (Zlib) Name() string {
return ".zz"
}
func (zz Zlib) Match(filename string, stream io.Reader) (MatchResult, error) {
var mr MatchResult
// match filename
if strings.Contains(strings.ToLower(filename), zz.Name()) {
mr.ByName = true
}
// match file header
buf, err := readAtMost(stream, len(ZlibHeader))
if err != nil {
return mr, err
}
mr.ByStream = bytes.Equal(buf, ZlibHeader)
return mr, nil
}
func (zz Zlib) OpenWriter(w io.Writer) (io.WriteCloser, error) {
level := zz.CompressionLevel
if level == 0 {
level = zlib.DefaultCompression
}
return zlib.NewWriterLevel(w, level)
}
func (Zlib) OpenReader(r io.Reader) (io.ReadCloser, error) {
return zlib.NewReader(r)
}