forked from OneOfOne/otk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.go
191 lines (157 loc) · 3.58 KB
/
compress.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package otk
import (
"archive/tar"
"archive/zip"
"bufio"
"io"
"os"
"path/filepath"
"strings"
"golang.org/x/xerrors"
)
type TarOptions struct {
CompressFn func(w io.Writer) io.WriteCloser
FilterFn func(path string, fi os.FileInfo) bool
BufSize int
DeleteOnError bool
}
func TarFolder(folder, fp string, topts *TarOptions) (err error) {
const defBufSize = 2 * 1024 * 1024
if topts == nil {
topts = &TarOptions{}
}
if err = os.MkdirAll(filepath.Dir(fp), 0o755); err != nil {
return
}
var f *os.File
if f, err = os.Create(fp + ".tmp"); err != nil {
return
}
bsz := topts.BufSize
if bsz < 1 {
bsz = defBufSize
}
bw := bufio.NewWriterSize(f, bsz)
defer func() {
if err = MergeErrors(", ", err, bw.Flush(), f.Close()); err != nil && topts.DeleteOnError {
err = MergeErrors(", ", err, os.Remove(fp))
}
if err == nil {
err = os.Rename(fp+".tmp", fp)
}
}()
var wc io.WriteCloser
if topts.CompressFn != nil {
wc = topts.CompressFn(bw)
defer func() { err = MergeErrors(", ", err, wc.Close()) }()
} else {
wc = NopWriteCloser(bw)
}
tw := tar.NewWriter(wc)
defer func() { err = MergeErrors(", ", err, tw.Close()) }()
ffn := topts.FilterFn
if ffn == nil {
ffn = func(_ string, fi os.FileInfo) bool { return fi.IsDir() || fi.Mode().IsRegular() }
}
err = filepath.Walk(folder, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
p, _ := filepath.Rel(folder, path)
if !ffn(p, fi) {
if fi.IsDir() {
return filepath.SkipDir
}
return nil
}
if !fi.Mode().IsRegular() {
return nil
}
if err = AppendToTar(tw, path, p); err != nil {
err = xerrors.Errorf("tar error (%s): %w", path, err)
}
return err
})
return
}
// AppendToTar is a helper function for add a physical file to tar
func AppendToTar(tw *tar.Writer, fullPath, tarPath string) (err error) {
var (
f *os.File
st os.FileInfo
hdr *tar.Header
)
if f, err = os.Open(fullPath); err != nil {
return err
}
defer f.Close()
if st, err = f.Stat(); err != nil {
return
}
if hdr, err = tar.FileInfoHeader(st, tarPath); err != nil {
return
}
hdr.Name = tarPath
if err = tw.WriteHeader(hdr); err != nil {
return
}
_, err = io.Copy(tw, io.LimitReader(f, st.Size()))
return
}
func Unzip(rt io.ReaderAt, dst string, filter func(path string, f *zip.File) bool) (err error) {
var (
zr *zip.Reader
size int64
)
switch rt := rt.(type) {
case interface{ Len() int }:
size = int64(rt.Len())
case interface{ Size() int64 }:
size = rt.Size()
case interface{ Stat() (os.FileInfo, error) }:
fi, err := rt.Stat()
if err != nil {
return err
}
size = fi.Size()
default:
err = xerrors.Errorf("%T doesn't provide a way to get the file size", rt)
return
}
if zr, err = zip.NewReader(rt, size); err != nil {
return
}
for _, zf := range zr.File {
fpath := filepath.Join(dst, zf.Name)
if !strings.HasPrefix(fpath, filepath.Clean(dst)+string(os.PathSeparator)) {
err = xerrors.Errorf("%s: illegal file path", fpath)
return
}
if filter != nil && !filter(fpath, zf) {
continue
}
if zf.FileInfo().IsDir() {
continue
}
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return
}
func() {
var f *os.File
if f, err = os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, zf.Mode()); err != nil {
return
}
defer f.Close()
var rc io.ReadCloser
if rc, err = zf.Open(); err != nil {
return
}
defer rc.Close()
_, err = io.Copy(f, rc)
}()
if err != nil {
return xerrors.Errorf("%s: unzip error: %w", zf.Name, err)
}
}
return
}