-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipfile.go
More file actions
97 lines (85 loc) · 2.1 KB
/
zipfile.go
File metadata and controls
97 lines (85 loc) · 2.1 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Utility functions for handling and fetching repo archives in zip format
* @Author: TaceyWong
* @Date: 2020-09-23 17:00:26
* @Last Modified by: TaceyWong
* @Last Modified time: 2020-09-23 18:12:04
*/
package starter
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// UnZip Download and unpack a zipfile at a given URI.
//
// This will download the zipfile to the cookiecutter repository,
// and unpack into a temporary directory.
//
// :param zipURI: The URI for the zipfile.
// :param isURL: Is the zip URI a URL or a file?
// :param cloneToDir: The cookiecutter repository directory
// to put the archive into.
// :param noInput: Suppress any prompts
// :param password: The password to use when unpacking the repository.
func UnZipFromURL(zipURI string, isURL bool, cloneToDir string, noInput bool, password string) error {
return nil
}
func UnZip(cloneToDir string, noInput bool, password string) error {
r, err := zip.OpenReader("src")
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
os.MkdirAll("dest", 0755)
// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
path := filepath.Join("dest", f.Name)
// Check for ZipSlip (Directory traversal)
if !strings.HasPrefix(path, filepath.Clean("dest")+string(os.PathSeparator)) {
return fmt.Errorf("illegal file path: %s", path)
}
if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}
return nil
}