zhangqian
2023-08-29 6843bdb44b8d5294a21f2ee30886e0c5ad07a150
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
package utils
 
import (
    "archive/zip"
    "fmt"
    "io"
    "os"
    "path/filepath"
    "strings"
)
 
// 解压
func Unzip(zipFile string, destDir string) ([]string, error) {
    zipReader, err := zip.OpenReader(zipFile)
    var paths []string
    if err != nil {
        return []string{}, err
    }
    defer zipReader.Close()
 
    for _, f := range zipReader.File {
        if strings.Index(f.Name, "..") > -1 {
            return []string{}, fmt.Errorf("%s 文件名不合法", f.Name)
        }
        fpath := filepath.Join(destDir, f.Name)
        paths = append(paths, fpath)
        if f.FileInfo().IsDir() {
            os.MkdirAll(fpath, os.ModePerm)
        } else {
            if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
                return []string{}, err
            }
 
            inFile, err := f.Open()
            if err != nil {
                return []string{}, err
            }
            defer inFile.Close()
 
            outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
            if err != nil {
                return []string{}, err
            }
            defer outFile.Close()
 
            _, err = io.Copy(outFile, inFile)
            if err != nil {
                return []string{}, err
            }
        }
    }
    return paths, nil
}