-
Notifications
You must be signed in to change notification settings - Fork 3
/
tarutil.go
62 lines (49 loc) · 843 Bytes
/
tarutil.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
58
59
60
61
62
package main
import (
"archive/tar"
"io"
"os"
)
func addFile(w *tar.Writer, path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
fi, err := f.Stat()
if err != nil {
return err
}
hdr, err := tar.FileInfoHeader(fi, "")
if err != nil {
return err
}
if err := w.WriteHeader(hdr); err != nil {
return err
}
if fi.Mode().IsRegular() {
if _, err := io.Copy(w, f); err != nil {
return err
}
}
return nil
}
func copyTar(dest *tar.Writer, src *tar.Reader, f func(*tar.Header) bool) error {
for {
hdr, err := src.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
if f != nil && !f(hdr) {
continue
}
if err := dest.WriteHeader(hdr); err != nil {
return err
}
if _, err := io.Copy(dest, src); err != nil {
return err
}
}
return nil
}