-
Notifications
You must be signed in to change notification settings - Fork 0
/
sz.go
49 lines (36 loc) · 917 Bytes
/
sz.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
package compressor
import (
"bytes"
"io"
"strings"
"github.com/klauspost/compress/snappy"
)
// Sz facilitates Snappy compression.
type Sz struct{}
var snappyHeader = []byte{0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59}
func init() {
RegisterFormat(Sz{})
}
func (sz Sz) Name() string {
return ".sz"
}
func (sz Sz) Match(filename string, stream io.Reader) (MatchResult, error) {
var mr MatchResult
// match filename
if strings.Contains(strings.ToLower(filename), sz.Name()) {
mr.ByName = true
}
// match file header
buf, err := readAtMost(stream, len(snappyHeader))
if err != nil {
return mr, err
}
mr.ByStream = bytes.Equal(buf, snappyHeader)
return mr, nil
}
func (Sz) OpenWriter(w io.Writer) (io.WriteCloser, error) {
return snappy.NewBufferedWriter(w), nil
}
func (Sz) OpenReader(r io.Reader) (io.ReadCloser, error) {
return io.NopCloser(snappy.NewReader(r)), nil
}