@@ -53,16 +53,16 @@ func Zip(srcDirPath string, destFilePath string, args ...*regexp.Regexp) (n int6
53
53
relativePath := strings .TrimPrefix (path , root )
54
54
relativePath = strings .Replace (relativePath , `\` , `/` , - 1 )
55
55
relativePath = strings .TrimPrefix (relativePath , `/` )
56
- f , err := w .Create (relativePath )
56
+ fw , err := w .Create (relativePath )
57
57
if err != nil {
58
58
return err
59
59
}
60
60
sf , err := os .Open (path )
61
61
if err != nil {
62
62
return err
63
63
}
64
- defer sf . Close ( )
65
- _ , err = io . Copy ( f , sf )
64
+ _ , err = io . Copy ( fw , sf )
65
+ sf . Close ( )
66
66
return err
67
67
})
68
68
@@ -78,6 +78,25 @@ func Zip(srcDirPath string, destFilePath string, args ...*regexp.Regexp) (n int6
78
78
return
79
79
}
80
80
81
+ func IllegalFilePath (path string ) bool {
82
+ var dots int
83
+ for _ , c := range path {
84
+ switch c {
85
+ case '.' :
86
+ dots ++
87
+ case '/' :
88
+ fallthrough
89
+ case '\\' :
90
+ if dots > 1 {
91
+ return true
92
+ }
93
+ default :
94
+ dots = 0
95
+ }
96
+ }
97
+ return false
98
+ }
99
+
81
100
// Unzip unzips .zip file to 'destPath'.
82
101
// It returns error when fail to finish operation.
83
102
func Unzip (srcPath , destPath string ) error {
@@ -90,24 +109,34 @@ func Unzip(srcPath, destPath string) error {
90
109
91
110
// Iterate through the files in the archive
92
111
for _ , f := range r .File {
93
- // Get files from archive
94
- rc , err := f .Open ()
95
- if err != nil {
96
- return err
112
+ if IllegalFilePath (f .Name ) {
113
+ return fmt .Errorf ("illegal file path in %s: %v" , filepath .Base (srcPath ), f .Name )
97
114
}
98
115
99
- dir := filepath .Dir (f .Name )
100
- // Create directory before create file
101
- os .MkdirAll (destPath + "/" + dir , os .ModePerm )
102
-
116
+ fullPath := filepath .Join (destPath , f .Name )
103
117
if f .FileInfo ().IsDir () {
118
+ if err = os .MkdirAll (fullPath , f .Mode ()); err != nil {
119
+ return err
120
+ }
104
121
continue
105
122
}
106
123
124
+ dir := filepath .Dir (f .Name )
125
+ // Create directory before create file
126
+ if err = os .MkdirAll (filepath .Join (destPath , dir ), os .ModePerm ); err != nil {
127
+ return err
128
+ }
129
+
130
+ // Get files from archive
131
+ rc , err := f .Open ()
132
+ if err != nil {
133
+ return err
134
+ }
107
135
// Write data to file
108
136
var fw * os.File
109
- fw , err = os .Create ( filepath . Join ( destPath , f .Name ))
137
+ fw , err = os .OpenFile ( fullPath , os . O_WRONLY | os . O_CREATE | os . O_TRUNC , f .Mode ( ))
110
138
if err != nil {
139
+ rc .Close ()
111
140
return err
112
141
}
113
142
_ , err = io .Copy (fw , rc )
@@ -163,6 +192,7 @@ func tarGz(gw *gzip.Writer, srcDirPath string, args ...*regexp.Regexp) error {
163
192
return err
164
193
}
165
194
fi , err := f .Stat ()
195
+ f .Close ()
166
196
if err != nil {
167
197
return err
168
198
}
@@ -177,7 +207,7 @@ func tarGz(gw *gzip.Writer, srcDirPath string, args ...*regexp.Regexp) error {
177
207
}
178
208
// handle source directory
179
209
fmt .Println ("Cerating tar.gz from directory..." )
180
- if err := tarGzDir (srcDirPath , filepath . Base ( srcDirPath ) , tw , regexpFileName , regexpIgnoreFile ); err != nil {
210
+ if err := tarGzDir (srcDirPath , `.` , tw , regexpFileName , regexpIgnoreFile ); err != nil {
181
211
return err
182
212
}
183
213
} else {
@@ -286,7 +316,9 @@ func tarGzFile(srcFile string, recPath string, tw *tar.Writer, fi os.FileInfo) e
286
316
// It returns error when fail to finish operation.
287
317
func UnTarGz (srcFilePath string , destDirPath string ) ([]string , error ) {
288
318
// Create destination directory
289
- os .Mkdir (destDirPath , os .ModePerm )
319
+ if err := os .MkdirAll (destDirPath , os .ModePerm ); err != nil {
320
+ return nil , err
321
+ }
290
322
291
323
fr , err := os .Open (srcFilePath )
292
324
if err != nil {
@@ -312,23 +344,35 @@ func UnTarGz(srcFilePath string, destDirPath string) ([]string, error) {
312
344
break
313
345
}
314
346
347
+ if IllegalFilePath (hdr .Name ) {
348
+ return nil , fmt .Errorf ("illegal file path in %s: %v" , filepath .Base (srcFilePath ), hdr .Name )
349
+ }
350
+ fullPath := filepath .Join (destDirPath , hdr .Name )
351
+ mode := hdr .FileInfo ().Mode ()
352
+
315
353
// Check if it is directory or file
316
354
if hdr .Typeflag != tar .TypeDir {
317
355
// Get files from archive
318
356
// Create directory before create file
319
357
dir := filepath .Dir (hdr .Name )
320
- os .MkdirAll (destDirPath + "/" + dir , os .ModePerm )
321
- dirs = AppendStr (dirs , dir )
358
+ if err = os .MkdirAll (filepath .Join (destDirPath , dir ), os .ModePerm ); err != nil {
359
+ return nil , err
360
+ }
322
361
323
362
// Write data to file
324
- fw , _ := os .Create (destDirPath + "/" + hdr .Name )
363
+ var fw * os.File
364
+ fw , err = os .OpenFile (fullPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , mode )
325
365
if err != nil {
326
366
return nil , err
327
367
}
328
368
_ , err = io .Copy (fw , tr )
329
- if err != nil {
330
- return nil , err
331
- }
369
+ fw .Close ()
370
+ } else {
371
+ dirs = AppendStr (dirs , fullPath )
372
+ err = os .MkdirAll (fullPath , mode )
373
+ }
374
+ if err != nil {
375
+ return nil , err
332
376
}
333
377
}
334
378
return dirs , nil
0 commit comments