-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZipFile.go
66 lines (53 loc) · 1.2 KB
/
ZipFile.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
package o365Api
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type ZipRequest interface {
Unzip(Zip) ([]string, error)
}
type Zip struct {
Source string
Destination string
}
func (zipRequest Zip) Unzip() ([]string, error) {
var filenames []string
reader, err := zip.OpenReader(zipRequest.Source)
if err != nil {
return []string{}, err
}
defer reader.Close()
for _, file := range reader.File {
path := filepath.Join(zipRequest.Destination, file.Name)
if !strings.HasPrefix(path, filepath.Clean(zipRequest.Destination)+string(os.PathSeparator)) {
return filenames, fmt.Errorf("%s: illegal file path", path)
}
filenames = append(filenames, path)
if file.FileInfo().IsDir() {
os.MkdirAll(path, os.ModePerm)
continue
}
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
return filenames, err
}
outFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
return filenames, err
}
rc, err := file.Open()
if err != nil {
return filenames, err
}
_, err = io.Copy(outFile, rc)
outFile.Close()
rc.Close()
if err != nil {
return filenames, err
}
}
return filenames, nil
}