This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
zip.go
174 lines (147 loc) · 4.59 KB
/
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2018 the Service Broker Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ziputil
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"text/tabwriter"
"github.com/GoogleCloudPlatform/gcp-service-broker/utils/stream"
)
// List writes a ls -la style listing of the zipfile to the given writer.
func List(z *zip.Reader, w io.Writer) {
sw := tabwriter.NewWriter(w, 0, 0, 2, ' ', tabwriter.StripEscape)
fmt.Fprintln(sw, "MODE\tSIZE\tNAME")
for _, fd := range z.File {
fmt.Fprintf(sw, "%s\t%d\t%s\n", fd.Mode().String(), fd.UncompressedSize, fd.Name)
}
sw.Flush()
}
// Joins a path for use in a zip file.
func Join(path ...string) string {
return strings.Join(path, "/")
}
// Clean converts a path into a zip style path by replacing backslashes with
// forward-slashes and making the paths relative by removing leading "/" and "./".
func Clean(path ...string) string {
joined := filepath.ToSlash(Join(path...))
slashStrip := strings.TrimPrefix(joined, "/")
dotStrip := strings.TrimPrefix(slashStrip, "./")
return dotStrip
}
// Find returns a pointer to the file at the given path if it exists, otherwise
// nil.
func Find(z *zip.Reader, path ...string) *zip.File {
name := Join(path...)
for _, f := range z.File {
if f.Name == name {
return f
}
}
return nil
}
// Opens the file at the given path if possible, otherwise returns an error.
func Open(z *zip.Reader, path ...string) (io.ReadCloser, error) {
f := Find(z, path...)
if f == nil {
fmt.Errorf("no such file: %q", Join(path...))
}
return f.Open()
}
// Extracts the contents of the zipDirectory to the given OS osDirectory.
// This routine is overly strict and doesn't allow extracting _any_ files that
// contain "..".
func Extract(z *zip.Reader, zipDirectory, osDirectory string) error {
for _, fd := range z.File {
if fd.UncompressedSize == 0 { // skip directories
continue
}
if !strings.HasPrefix(fd.Name, zipDirectory) {
continue
}
if containsDotDot(fd.Name) {
return fmt.Errorf("potential zip slip extracting %q", fd.Name)
}
src := stream.FromReadCloserError(fd.Open())
newName := strings.TrimPrefix(fd.Name, zipDirectory)
destPath := filepath.Join(osDirectory, filepath.FromSlash(newName))
dest := stream.ToModeFile(fd.Mode(), destPath)
if err := stream.Copy(src, dest); err != nil {
return fmt.Errorf("couldn't extract file %q: %v", fd.Name, err)
}
}
return nil
}
// containsDotDot checks if the filepath value v contains a ".." entry.
// This will check filepath components by splitting along / or \. This
// function is copied directly from the Go net/http implementation.
func containsDotDot(v string) bool {
if !strings.Contains(v, "..") {
return false
}
for _, ent := range strings.FieldsFunc(v, isSlashRune) {
if ent == ".." {
return true
}
}
return false
}
func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
// Unarchive opens the specified file and extracts all of its contents to the
// destination.
func Unarchive(zipFile, destination string) error {
rdc, err := zip.OpenReader(zipFile)
if err != nil {
return fmt.Errorf("couldn't open archive %q: %v", zipFile, err)
}
defer rdc.Close()
return Extract(&rdc.Reader, "", destination)
}
// Archive creates a zip from the contents of the sourceFolder at the
// destinationZip location.
func Archive(sourceFolder, destinationZip string) error {
fd, err := os.Create(destinationZip)
if err != nil {
return fmt.Errorf("couldn't create archive %q: %v", destinationZip, err)
}
defer fd.Close()
w := zip.NewWriter(fd)
defer w.Close()
return filepath.Walk(sourceFolder, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = Clean(strings.TrimPrefix(path, sourceFolder))
header.Method = zip.Deflate
if info.IsDir() {
w.CreateHeader(header)
} else {
fd, err := w.CreateHeader(header)
if err != nil {
return err
}
if err := stream.Copy(stream.FromFile(path), stream.ToWriter(fd)); err != nil {
return err
}
}
return nil
})
}