MCPcopy Create free account
hub / github.com/cloudfoundry/multiapps-cli-plugin / CreateMtaArchive

Function CreateMtaArchive

util/archive_handler.go:62–127  ·  view source on GitHub ↗
(source, target string)

Source from the content-addressed store, hash-verified

60}
61
62func CreateMtaArchive(source, target string) error {
63 zipfile, err := os.Create(target)
64 if err != nil {
65 return err
66 }
67 defer zipfile.Close()
68
69 archive := zip.NewWriter(zipfile)
70 defer archive.Close()
71
72 info, err := os.Stat(source)
73 if err != nil {
74 return nil
75 }
76
77 var baseDir string
78 if info.IsDir() {
79 baseDir = "."
80 }
81
82 return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
83 if err != nil {
84 return err
85 }
86
87 if info.Name() == filepath.Base(source) {
88 return nil
89 }
90 header, err := zip.FileInfoHeader(info)
91 if err != nil {
92 return err
93 }
94
95 if baseDir != "" {
96 pathWithoutSourceDirPrefix := strings.TrimPrefix(path, source)
97 pathWithoutSourceDirPrefix = strings.TrimPrefix(pathWithoutSourceDirPrefix, string(os.PathSeparator))
98 if pathWithoutSourceDirPrefix != "" {
99 header.Name = filepath.Join(baseDir, pathWithoutSourceDirPrefix)
100 }
101 }
102
103 if info.IsDir() {
104 header.Name += string(os.PathSeparator)
105 }
106 header.Method = zip.Deflate
107
108 header.Name = filepath.ToSlash(header.Name)
109
110 writer, err := archive.CreateHeader(header)
111 if err != nil {
112 return err
113 }
114
115 if info.IsDir() {
116 return nil
117 }
118
119 file, err := os.Open(path)

Callers 1

BuildMethod · 0.85

Calls 4

StatMethod · 0.80
IsDirMethod · 0.80
NameMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected