-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
lzip.go
54 lines (42 loc) · 1.05 KB
/
lzip.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
package archiver
import (
"bytes"
"context"
"io"
"path/filepath"
"strings"
"github.com/sorairolake/lzip-go"
)
func init() {
RegisterFormat(Lzip{})
}
// Lzip facilitates lzip compression.
type Lzip struct{}
func (Lzip) Extension() string { return ".lz" }
func (lz Lzip) Match(_ context.Context, filename string, stream io.Reader) (MatchResult, error) {
var mr MatchResult
// match filename
if filepath.Ext(strings.ToLower(filename)) == lz.Extension() {
mr.ByName = true
}
// match file header
buf, err := readAtMost(stream, len(lzipHeader))
if err != nil {
return mr, err
}
mr.ByStream = bytes.Equal(buf, lzipHeader)
return mr, nil
}
func (Lzip) OpenWriter(w io.Writer) (io.WriteCloser, error) {
return lzip.NewWriter(w), nil
}
func (Lzip) OpenReader(r io.Reader) (io.ReadCloser, error) {
lzr, err := lzip.NewReader(r)
if err != nil {
return nil, err
}
return io.NopCloser(lzr), err
}
// magic number at the beginning of lzip files
// https://datatracker.ietf.org/doc/html/draft-diaz-lzip-09#section-2
var lzipHeader = []byte("LZIP")