-
Notifications
You must be signed in to change notification settings - Fork 1
/
upzip.go
70 lines (56 loc) · 1.27 KB
/
upzip.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
// Copyright 2019 The vogo Authors. All rights reserved.
// author: wongoo
package gracego
import (
"archive/zip"
"io"
"os"
"path/filepath"
"strings"
)
// unzip will decompress a zip archive, moving all files and folders
// within the zip file (parameter 1) to an output directory (parameter 2).
func unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()
// Make File
if err := os.MkdirAll(dest, os.ModePerm); err != nil {
return err
}
for _, zipFile := range r.File {
// Check for ZipSlip
fileName := strings.ReplaceAll(zipFile.Name, "..", "")
if fileName != zipFile.Name {
graceLog("ignore zip file: %s", zipFile.Name)
continue
}
targetPath := filepath.Join(dest, fileName)
if err := writeZipFile(targetPath, zipFile); err != nil {
return err
}
}
return nil
}
func writeZipFile(targetPath string, f *zip.File) error {
if f.FileInfo().IsDir() {
if err := os.MkdirAll(targetPath, os.ModePerm); err != nil {
return err
}
return nil
}
outFile, err := os.OpenFile(targetPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
rc, err := f.Open()
if err != nil {
return err
}
_, err = io.Copy(outFile, rc)
outFile.Close()
_ = rc.Close()
return err
}