-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoderutils.go
175 lines (146 loc) · 3.36 KB
/
encoderutils.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
175
package encoderutils
import (
"archive/zip"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// Unzip unzips the specified zip file and extracts the contents of the
// zip file to the specified destination.
//
// **Parameters:**
//
// src: A string representing the path to the zip file.
//
// dest: A string representing the path to the destination directory.
//
// **Returns:**
//
// error: An error if any issue occurs while trying to unzip the file.
func Unzip(src, dest string) error {
// This rule appears to be broken - false positive
// nosemgrep:go.lang.security.zip.path-traversal-inside-zip-extraction
reader, err := zip.OpenReader(src)
if err != nil {
return err
}
defer reader.Close()
dest = filepath.Clean(dest) + string(os.PathSeparator)
for _, file := range reader.File {
rc, err := file.Open()
if err != nil {
return err
}
var buf bytes.Buffer
tee := io.TeeReader(rc, &buf)
if _, err := io.CopyN(&buf, tee, 1024*1024*256); err != nil && err != io.EOF { // Limit to 256MB
rc.Close()
return err
}
fmt.Println()
path := filepath.Join(dest, file.Name)
if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) {
return fmt.Errorf("illegal file path: %s", path)
}
relPath, err := filepath.Rel(dest, path)
if err != nil {
rc.Close()
return err
}
if strings.HasPrefix(relPath, "..") {
rc.Close()
return fmt.Errorf("illegal file path: %s", path)
}
if file.FileInfo().IsDir() {
if err := os.MkdirAll(path, os.ModePerm); err != nil {
rc.Close()
return err
}
rc.Close()
continue
}
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
rc.Close()
return err
}
destFile, err := os.Create(path)
if err != nil {
rc.Close()
return err
}
if _, err = io.CopyN(destFile, rc, file.FileInfo().Size()); err != nil && err != io.EOF {
destFile.Close()
rc.Close()
return err
}
destFile.Close()
rc.Close()
}
return nil
}
// Zip creates a zip file from the specified source directory and saves it to the
// specified destination path.
//
// **Parameters:**
//
// srcDir: A string representing the path to the source directory.
//
// destFile: A string representing the path to the destination zip file.
//
// **Returns:**
//
// error: An error if any issue occurs while trying to zip the file.
func Zip(srcDir, destFile string) error {
zipFile, err := os.Create(destFile)
if err != nil {
return err
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(srcDir, path)
if err != nil {
return err
}
// Skip the root directory
if relPath == "." {
return nil
}
// Create a new zip file entry
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = filepath.ToSlash(relPath)
if info.IsDir() {
header.Name += "/"
} else {
header.Method = zip.Deflate
}
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
// Write the file content to the zip
if !info.IsDir() {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
if err != nil {
return err
}
}
return nil
})
return err
}