forked from mholt/archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sz.go
47 lines (35 loc) · 968 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
package archiver
import (
"bytes"
"io"
"strings"
"github.com/golang/snappy"
)
func init() {
RegisterFormat(Sz{})
}
// Sz facilitates Snappy compression.
type Sz struct{}
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
}
// https://github.com/google/snappy/blob/master/framing_format.txt
var snappyHeader = []byte{0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59}