forked from mholt/archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brotli.go
49 lines (38 loc) · 1.12 KB
/
brotli.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 archiver
import (
"context"
"io"
"strings"
"github.com/andybalholm/brotli"
)
func init() {
RegisterFormat(Brotli{})
}
// Brotli facilitates brotli compression.
type Brotli struct {
Quality int
}
func (Brotli) Extension() string { return ".br" }
func (br Brotli) Match(_ context.Context, filename string, stream io.Reader) (MatchResult, error) {
var mr MatchResult
// match filename
if strings.Contains(strings.ToLower(filename), br.Extension()) {
mr.ByName = true
}
// brotli does not have well-defined file headers or a magic number;
// the best way to match the stream is probably to try decoding part
// of it, but we'll just have to guess a large-enough size that is
// still small enough for the smallest streams we'll encounter
r := brotli.NewReader(stream)
buf := make([]byte, 16)
if _, err := io.ReadFull(r, buf); err == nil {
mr.ByStream = true
}
return mr, nil
}
func (br Brotli) OpenWriter(w io.Writer) (io.WriteCloser, error) {
return brotli.NewWriterLevel(w, br.Quality), nil
}
func (Brotli) OpenReader(r io.Reader) (io.ReadCloser, error) {
return io.NopCloser(brotli.NewReader(r)), nil
}