MCPcopy
hub / github.com/flipped-aurora/gin-vue-admin / Unzip

Function Unzip

server/utils/zip.go:13–53  ·  view source on GitHub ↗

解压

(zipFile string, destDir string)

Source from the content-addressed store, hash-verified

11
12// 解压
13func Unzip(zipFile string, destDir string) ([]string, error) {
14 zipReader, err := zip.OpenReader(zipFile)
15 var paths []string
16 if err != nil {
17 return []string{}, err
18 }
19 defer zipReader.Close()
20
21 for _, f := range zipReader.File {
22 if strings.Contains(f.Name, "..") {
23 return []string{}, fmt.Errorf("%s 文件名不合法", f.Name)
24 }
25 fpath := filepath.Join(destDir, f.Name)
26 paths = append(paths, fpath)
27 if f.FileInfo().IsDir() {
28 os.MkdirAll(fpath, os.ModePerm)
29 } else {
30 if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
31 return []string{}, err
32 }
33
34 inFile, err := f.Open()
35 if err != nil {
36 return []string{}, err
37 }
38 defer inFile.Close()
39
40 outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
41 if err != nil {
42 return []string{}, err
43 }
44 defer outFile.Close()
45
46 _, err = io.Copy(outFile, inFile)
47 if err != nil {
48 return []string{}, err
49 }
50 }
51 }
52 return paths, nil
53}

Callers

nothing calls this directly

Calls 2

OpenMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected