-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompress.go
243 lines (202 loc) · 4.57 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
UncompressFn func(r io.Reader) io.Reader
FilterFn func(path string, fi os.FileInfo) bool
BufSize int
DeleteOnError bool
}
func TarFolder(folder, fp string, opts *TarOptions) (err error) {
if opts == nil {
opts = &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
}
defer func() {
if err = MergeErrors(", ", err, f.Close()); err != nil && opts.DeleteOnError {
err = MergeErrors(", ", err, os.Remove(fp))
}
if err == nil {
err = os.Rename(fp+".tmp", fp)
}
}()
return Tar(folder, f, opts)
}
func Tar(folder string, w io.Writer, opts *TarOptions) (err error) {
const defBufSize = 4 * 1024 * 1024
if opts == nil {
opts = &TarOptions{}
}
bsz := opts.BufSize
if bsz < 1 {
bsz = defBufSize
}
bw := bufio.NewWriterSize(w, bsz)
defer func() { err = MergeErrors(", ", err, bw.Flush()) }()
var wc io.WriteCloser
if opts.CompressFn != nil {
wc = opts.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 := opts.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 UntarFolder(fp, folder string, opts *TarOptions) error {
f, err := os.Open(fp)
if err != nil {
return err
}
defer f.Close()
return Untar(f, folder, opts)
}
func Untar(r io.Reader, folder string, opts *TarOptions) error {
r = bufio.NewReader(r)
if opts != nil && opts.UncompressFn != nil {
r = opts.UncompressFn(r)
}
rd := tar.NewReader(r)
for {
hdr, err := rd.Next()
if err != nil {
if err == io.EOF {
err = nil
}
return err
}
if hdr.Typeflag != tar.TypeReg {
continue
}
p := filepath.Join(folder, hdr.Name)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
return err
}
if err = CopyOnWriteFile(p, func(w io.Writer) error {
_, err := io.Copy(w, rd)
return err
}); err != nil {
return err
}
}
}
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
}