-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.go
122 lines (102 loc) · 2.55 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
package main
import (
"archive/zip"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
log "github.com/sirupsen/logrus"
)
var appPackagesBasePath = "/home/ubuntu/go-extract-test/packages"
func main() {
packageId := "f59620d2-5f89-4776-97aa-01f21a191a49"
hostIp := "192.168.2.1"
packagePath := appPackagesBasePath + "/" + packageId + hostIp
packageFilePath := packagePath + "/" + packageId + ".csar"
packagePath, err := extractCsarPackage(packageFilePath)
if err != nil {
log.Error(err)
return
}
fmt.Println(packagePath)
}
// Create directory
func CreateDir(path string) bool {
_, err := os.Stat(path)
if os.IsNotExist(err) {
errDir := os.MkdirAll(path, 488)
if errDir != nil {
return false
}
}
return true
}
// extract CSAR package
func extractCsarPackage(packagePath string) (string, error) {
zipReader, _ := zip.OpenReader(packagePath)
if len(zipReader.File) > 1024 {
return "", errors.New("Too many files contains in zip file")
}
defer zipReader.Close()
var totalWrote int64
packageDir := path.Dir(packagePath)
err := os.MkdirAll(packageDir, 0750)
if err != nil {
log.Error("Failed to make directory")
return "" ,errors.New("Failed to make directory")
}
for _, file := range zipReader.Reader.File {
zippedFile, err := file.Open()
if err != nil || zippedFile == nil {
log.Error("Failed to open zip file")
continue
}
if file.UncompressedSize64 > 104857600 || totalWrote > 104857600 {
log.Error("File size limit is exceeded")
}
defer zippedFile.Close()
isContinue, wrote := extractFiles(file, zippedFile, totalWrote, packageDir)
if isContinue {
continue
}
totalWrote = wrote
}
return packageDir, nil
}
// Extract files
func extractFiles(file *zip.File, zippedFile io.ReadCloser, totalWrote int64, dirName string) (bool, int64) {
targetDir := dirName
extractedFilePath := filepath.Join(
targetDir,
file.Name,
)
if file.FileInfo().IsDir() {
err := os.MkdirAll(extractedFilePath, 0750)
if err != nil {
log.Error("Failed to create directory")
}
} else {
parent := filepath.Dir(extractedFilePath)
if _, err := os.Stat(parent); os.IsNotExist(err) {
_ = os.MkdirAll(parent, 0750)
}
outputFile, err := os.OpenFile(
extractedFilePath,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0750,
)
if err != nil || outputFile == nil {
log.Error("The output file is nil")
return true, totalWrote
}
defer outputFile.Close()
wt, err := io.Copy(outputFile, zippedFile)
if err != nil {
log.Error("Failed to copy zipped file")
}
totalWrote += wt
}
return false, totalWrote
}