-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzip.go
50 lines (41 loc) · 1.01 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
package pkg
import (
"io"
"os"
"path/filepath"
"github.com/mholt/archives"
)
// FROM https://golangcode.com/unzip-files-in-go/
// Unzip will decompress a zip archive, moving all files and folders
// within the zip file (parameter 1) to an output directory (parameter 2).
func Unzip(f archives.FileInfo, dest string) error {
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()
// Store filename/path for returning and using later on
fpath := filepath.Join(dest, f.NameInArchive)
// filenames = append(filenames, fpath)
if f.IsDir() {
// Make Folder
os.MkdirAll(fpath, os.ModePerm)
} else {
// Make File
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
_, err = io.Copy(outFile, rc)
// Close the file without defer to close before next iteration of loop
outFile.Close()
if err != nil {
return err
}
}
// }
return nil
}