MCPcopy Create free account
hub / github.com/LanceLRQ/deer-executor / packZipFile

Function packZipFile

common/persistence/problems/pack_zip.go:14–70  ·  view source on GitHub ↗
(rootPath string, targetPath string)

Source from the content-addressed store, hash-verified

12)
13
14func packZipFile(rootPath string, targetPath string) error {
15 fout, err := os.Create(targetPath)
16 if err != nil {
17 return errors.Errorf("create problem package file error: %s", err.Error())
18 }
19 defer fout.Close()
20
21 zipWriter := zip.NewWriter(fout)
22 defer zipWriter.Close()
23
24 err = filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
25 if err != nil {
26 return err
27 }
28
29 header, err := zip.FileInfoHeader(info)
30 if err != nil {
31 return err
32 }
33
34 header.Name = strings.TrimPrefix(path, rootPath+"/")
35
36 // 排除根目录
37 if header.Name == rootPath {
38 return nil
39 }
40 if info.IsDir() {
41 header.Name += "/"
42 } else {
43 header.Method = zip.Deflate
44 }
45 // 排除bin目录
46 if strings.HasPrefix(header.Name, "bin/") {
47 return nil
48 }
49
50 writer, err := zipWriter.CreateHeader(header)
51 if err != nil {
52 return err
53 }
54
55 if !info.IsDir() {
56 file, err := os.Open(path)
57 if err != nil {
58 return err
59 }
60 defer file.Close()
61 _, err = io.Copy(writer, file)
62 if err != nil {
63 return err
64 }
65 }
66 return err
67 })
68
69 return err
70}
71

Callers 1

PackProblemsAsZipFunction · 0.85

Calls 2

ErrorfMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected