-
Notifications
You must be signed in to change notification settings - Fork 1
/
unzip.go
52 lines (46 loc) · 864 Bytes
/
unzip.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
package util
import (
"archive/zip"
"io"
"os"
"path/filepath"
"strings"
)
func Unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()
name := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(name, os.ModePerm)
} else {
var fdir string
if lastIndex := strings.LastIndex(name, string(os.PathSeparator)); lastIndex > -1 {
fdir = name[:lastIndex]
}
err = os.MkdirAll(fdir, os.ModePerm)
if err != nil {
return err
}
f, err := os.OpenFile(
name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
}
return nil
}