-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstaller.go
72 lines (62 loc) · 1.3 KB
/
installer.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
package main
import (
"archive/zip"
"io"
"os"
"path/filepath"
"strings"
"github.com/cheggaaa/pb/v3"
)
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func UnzipPackage(file, dest string) error {
log.Infof("Extracting %s to %s", file, dest)
zipFile, err := zip.OpenReader(file)
if err != nil {
os.Remove(file)
return err
}
defer zipFile.Close()
bar := pb.StartNew(len(zipFile.File))
os.MkdirAll(dest, 0644)
for _, f := range zipFile.File {
extractFile := func(path string) error {
if fileExists(path) && config.InstallerExcludeSet.Contains(f.Name) {
return nil
}
fileWriter, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, f.Mode())
if err != nil {
return err
}
defer fileWriter.Close()
fileReader, err := f.Open()
if err != nil {
return err
}
defer fileReader.Close()
_, err = io.Copy(fileWriter, fileReader)
if err != nil {
return err
}
return nil
}
bar.Add(1)
realPath := filepath.Join(dest, f.Name)
if strings.Contains(realPath, "..") {
log.Warningf("Skipped invalid path %s", realPath)
continue
}
if f.FileInfo().IsDir() {
os.MkdirAll(realPath, f.Mode())
} else {
extractFile(realPath)
}
}
bar.Finish()
return nil
}