-
Notifications
You must be signed in to change notification settings - Fork 3
/
tar.go
53 lines (46 loc) · 880 Bytes
/
tar.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
package main
import (
"archive/tar"
"errors"
"io"
"os"
)
type handler func(n string, r io.Reader) error
type matcher func(string) bool
var errStop = errors.New("stop")
func forEachMatchingEntry(p string, m matcher, h handler) error {
f, err := os.Open(p)
if err != nil {
return err
}
defer f.Close()
r := tar.NewReader(f)
for {
hdr, err := r.Next()
if hdr == nil {
break
}
if err != nil {
return err
}
if m(hdr.Name) {
if err := h(hdr.Name, r); err == errStop {
return nil
} else if err != nil {
return err
}
}
}
return nil
}
func forEachEntry(p string, h handler) error {
return forEachMatchingEntry(p, any, h)
}
func onMatchingEntry(p string, m matcher, h handler) error {
return forEachMatchingEntry(p, m, func(n string, r io.Reader) error {
if err := h(n, r); err != nil {
return err
}
return errStop
})
}