-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
lz4.go
57 lines (45 loc) · 1.13 KB
/
lz4.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
package archives
import (
"bytes"
"context"
"io"
"strings"
"github.com/pierrec/lz4/v4"
)
func init() {
RegisterFormat(Lz4{})
}
// Lz4 facilitates LZ4 compression.
type Lz4 struct {
CompressionLevel int
}
func (Lz4) Extension() string { return ".lz4" }
func (Lz4) MediaType() string { return "application/x-lz4" }
func (lz Lz4) Match(_ context.Context, filename string, stream io.Reader) (MatchResult, error) {
var mr MatchResult
// match filename
if strings.Contains(strings.ToLower(filename), lz.Extension()) {
mr.ByName = true
}
// match file header
buf, err := readAtMost(stream, len(lz4Header))
if err != nil {
return mr, err
}
mr.ByStream = bytes.Equal(buf, lz4Header)
return mr, nil
}
func (lz Lz4) OpenWriter(w io.Writer) (io.WriteCloser, error) {
lzw := lz4.NewWriter(w)
options := []lz4.Option{
lz4.CompressionLevelOption(lz4.CompressionLevel(lz.CompressionLevel)),
}
if err := lzw.Apply(options...); err != nil {
return nil, err
}
return lzw, nil
}
func (Lz4) OpenReader(r io.Reader) (io.ReadCloser, error) {
return io.NopCloser(lz4.NewReader(r)), nil
}
var lz4Header = []byte{0x04, 0x22, 0x4d, 0x18}