-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.go
127 lines (111 loc) · 2.42 KB
/
extract.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
package helper
import (
"archive/tar"
"archive/zip"
"io"
"os"
"path/filepath"
"github.com/tiramiseb/quickonf/internal/output"
"github.com/ulikunitz/xz"
)
// ExtractTarxz extracts a tar.xz archive.
//
// In dry-run mode, only return ResultDryrun.
func ExtractTarxz(tarxzfilepath, dest string) (ResultStatus, error) {
if Dryrun {
return ResultDryrun, nil
}
fread, err := os.Open(tarxzfilepath)
if err != nil {
return ResultError, err
}
defer fread.Close()
xread, err := xz.NewReader(fread)
if err != nil {
return ResultError, err
}
tread := tar.NewReader(xread)
for {
header, err := tread.Next()
if err != nil {
if err == io.EOF {
break
}
return ResultError, err
}
target := filepath.Join(dest, header.Name)
switch header.Typeflag {
case tar.TypeDir:
if _, err := os.Stat(target); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(target, header.FileInfo().Mode()); err != nil {
return ResultError, err
}
continue
}
return ResultError, err
}
case tar.TypeReg:
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, header.FileInfo().Mode())
if err != nil {
return ResultError, err
}
_, err = io.Copy(f, tread)
f.Close()
if err != nil {
return ResultError, err
}
}
}
return ResultSuccess, nil
}
// ExtractZip unzips the given file into the given destination.
//
// In dry-run mode, only return ResultDryrun.
//
// If out is provided, a "X on Y" loader is provided to the user.
func ExtractZip(zipfilepath, dest string, out output.Output) (ResultStatus, error) {
if Dryrun {
return ResultDryrun, nil
}
if out != nil {
defer out.HideXonY()
}
r, err := zip.OpenReader(zipfilepath)
if err != nil {
return ResultError, err
}
defer r.Close()
totalfiles := len(r.File)
for i, f := range r.File {
if out != nil {
out.ShowXonY(i, totalfiles)
}
fpath := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
if err := os.MkdirAll(fpath, 0755); err != nil {
return ResultError, err
}
continue
}
if err := os.MkdirAll(filepath.Dir(fpath), 0755); err != nil {
return ResultError, err
}
outfile, err := os.Create(fpath)
if err != nil {
return ResultError, err
}
rc, err := f.Open()
if err != nil {
outfile.Close()
return ResultError, err
}
_, err = io.Copy(outfile, rc)
outfile.Close()
rc.Close()
if err != nil {
return ResultError, err
}
}
return ResultSuccess, nil
}