MCPcopy Index your code
hub / github.com/cloudfoundry/cli / zipit

Function zipit

actor/sharedaction/sharedaction_suite_test.go:27–94  ·  view source on GitHub ↗

Thanks to Svett Ralchev http://blog.ralch.com/tutorial/golang-working-with-zip/

(source, target, prefix string)

Source from the content-addressed store, hash-verified

25// Thanks to Svett Ralchev
26// http://blog.ralch.com/tutorial/golang-working-with-zip/
27func zipit(source, target, prefix string) error {
28 zipfile, err := os.Create(target)
29 if err != nil {
30 return err
31 }
32 defer zipfile.Close()
33
34 if prefix != "" {
35 _, err = io.WriteString(zipfile, prefix)
36 if err != nil {
37 return err
38 }
39 }
40
41 archive := zip.NewWriter(zipfile)
42 defer archive.Close()
43
44 err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
45 if err != nil {
46 return err
47 }
48
49 header, err := zip.FileInfoHeader(info)
50 if err != nil {
51 return err
52 }
53
54 header.Name = strings.TrimPrefix(path, source)
55
56 if info.IsDir() {
57 header.Name += string(os.PathSeparator)
58 } else {
59 header.Method = zip.Deflate
60 }
61
62 writer, err := archive.CreateHeader(header)
63 if err != nil {
64 return err
65 }
66
67 if info.IsDir() {
68 return nil
69 }
70
71 if info.Mode().IsRegular() {
72 file, err := os.Open(path)
73 if err != nil {
74 return err
75 }
76 defer file.Close()
77
78 _, err = io.Copy(writer, file)
79 return err
80 } else if info.Mode()&os.ModeSymlink != 0 {
81 pathInSymlink, err := os.Readlink(path)
82 if err != nil {
83 return err
84 }

Calls 4

IsDirMethod · 0.80
ModeMethod · 0.80
CreateMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected