forked from mholt/archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bz2.go
50 lines (38 loc) · 909 Bytes
/
bz2.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
package archiver
import (
"bytes"
"io"
"strings"
"github.com/dsnet/compress/bzip2"
)
func init() {
RegisterFormat(Bz2{})
}
// Bz2 facilitates bzip2 compression.
type Bz2 struct {
CompressionLevel int
}
func (Bz2) Name() string { return ".bz2" }
func (bz Bz2) Match(filename string, stream io.Reader) (MatchResult, error) {
var mr MatchResult
// match filename
if strings.Contains(strings.ToLower(filename), bz.Name()) {
mr.ByName = true
}
// match file header
buf, err := readAtMost(stream, len(bzip2Header))
if err != nil {
return mr, err
}
mr.ByStream = bytes.Equal(buf, bzip2Header)
return mr, nil
}
func (bz Bz2) OpenWriter(w io.Writer) (io.WriteCloser, error) {
return bzip2.NewWriter(w, &bzip2.WriterConfig{
Level: bz.CompressionLevel,
})
}
func (Bz2) OpenReader(r io.Reader) (io.ReadCloser, error) {
return bzip2.NewReader(r, nil)
}
var bzip2Header = []byte("BZh")