-
Notifications
You must be signed in to change notification settings - Fork 238
/
decompress_zip.go
121 lines (100 loc) · 2.57 KB
/
decompress_zip.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package getter
import (
"archive/zip"
"fmt"
"os"
"path/filepath"
)
// ZipDecompressor is an implementation of Decompressor that can
// decompress zip files.
type ZipDecompressor struct {
// FileSizeLimit limits the total size of all
// decompressed files.
//
// The zero value means no limit.
FileSizeLimit int64
// FilesLimit limits the number of files that are
// allowed to be decompressed.
//
// The zero value means no limit.
FilesLimit int
}
func (d *ZipDecompressor) Decompress(dst, src string, dir bool, umask os.FileMode) error {
// If we're going into a directory we should make that first
mkdir := dst
if !dir {
mkdir = filepath.Dir(dst)
}
if err := os.MkdirAll(mkdir, mode(0755, umask)); err != nil {
return err
}
// Open the zip
zipR, err := zip.OpenReader(src)
if err != nil {
return err
}
defer zipR.Close()
// Check the zip integrity
if len(zipR.File) == 0 {
// Empty archive
return fmt.Errorf("empty archive: %s", src)
}
if !dir && len(zipR.File) > 1 {
return fmt.Errorf("expected a single file: %s", src)
}
if d.FilesLimit > 0 && len(zipR.File) > d.FilesLimit {
return fmt.Errorf("zip archive contains too many files: %d > %d", len(zipR.File), d.FilesLimit)
}
var fileSizeTotal int64
// Go through and unarchive
for _, f := range zipR.File {
path := dst
if dir {
// Disallow parent traversal
if containsDotDot(f.Name) {
return fmt.Errorf("entry contains '..': %s", f.Name)
}
path = filepath.Join(path, f.Name)
}
fileInfo := f.FileInfo()
fileSizeTotal += fileInfo.Size()
if d.FileSizeLimit > 0 && fileSizeTotal > d.FileSizeLimit {
return fmt.Errorf("zip archive larger than limit: %d", d.FileSizeLimit)
}
if fileInfo.IsDir() {
if !dir {
return fmt.Errorf("expected a single file: %s", src)
}
// A directory, just make the directory and continue unarchiving...
if err := os.MkdirAll(path, mode(0755, umask)); err != nil {
return err
}
continue
}
// Create the enclosing directories if we must. ZIP files aren't
// required to contain entries for just the directories so this
// can happen.
if dir {
if err := os.MkdirAll(filepath.Dir(path), mode(0755, umask)); err != nil {
return err
}
}
// Open the file for reading
srcF, err := f.Open()
if err != nil {
if srcF != nil {
srcF.Close()
}
return err
}
// Size limit is tracked using the returned file info.
err = copyReader(path, srcF, f.Mode(), umask, 0)
srcF.Close()
if err != nil {
return err
}
}
return nil
}