forked from ovh/cds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
79 lines (66 loc) · 1.65 KB
/
Copy pathfile.go
File metadata and controls
79 lines (66 loc) · 1.65 KB
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
package sdk
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
"github.com/spf13/afero"
)
// IsTar returns true if the content is a tar
func IsTar(buf []byte) bool {
return len(buf) > 261 &&
buf[257] == 0x75 && buf[258] == 0x73 &&
buf[259] == 0x74 && buf[260] == 0x61 &&
buf[261] == 0x72
}
// IsGz returns true if the content is gzipped
func IsGz(buf []byte) bool {
return len(buf) > 2 &&
buf[0] == 0x1F && buf[1] == 0x8B && buf[2] == 0x8
}
// UntarGz takes a destination path and a reader; a tar.gz reader loops over the tarfile
// creating the file structure at 'dst' along the way, and writing any files
func UntarGz(fs afero.Fs, dst string, r io.Reader) error {
gzr, err := gzip.NewReader(r)
if err != nil {
return err
}
defer gzr.Close()
return Untar(fs, dst, gzr)
}
// Untar takes a destination path and a reader; a tar reader loops over the tarfile
// creating the file structure at 'dst' along the way, and writing any files
func Untar(fs afero.Fs, dst string, r io.Reader) error {
tr := tar.NewReader(r)
for {
header, err := tr.Next()
switch {
case err == io.EOF:
return nil
case err != nil:
return err
case header == nil:
continue
}
target := filepath.Join(dst, header.Name)
// check the file type
switch header.Typeflag {
case tar.TypeDir:
if _, err := fs.Stat(target); err != nil {
if err := fs.MkdirAll(target, 0755); err != nil {
return err
}
}
case tar.TypeReg:
f, err := fs.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
if _, err := io.Copy(f, tr); err != nil {
return err
}
f.Close()
}
}
}