MCPcopy
hub / github.com/cloudfoundry/cli / Zipit

Function Zipit

actor/v7action/buildpack.go:192–259  ·  view source on GitHub ↗

Zipit zips the source into a .zip file in the target dir

(source, target, prefix string)

Source from the content-addressed store, hash-verified

190
191// Zipit zips the source into a .zip file in the target dir
192func Zipit(source, target, prefix string) error {
193 // Thanks to Svett Ralchev
194 // http://blog.ralch.com/tutorial/golang-working-with-zip/
195
196 zipfile, err := os.Create(target)
197 if err != nil {
198 return err
199 }
200 defer zipfile.Close()
201
202 if prefix != "" {
203 _, err = io.WriteString(zipfile, prefix)
204 if err != nil {
205 return err
206 }
207 }
208
209 archive := zip.NewWriter(zipfile)
210 defer archive.Close()
211
212 err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
213 if err != nil {
214 return err
215 }
216
217 if path == source {
218 return nil
219 }
220
221 header, err := zip.FileInfoHeader(info)
222 if err != nil {
223 return err
224 }
225 header.Name, err = filepath.Rel(source, path)
226 if err != nil {
227 return err
228 }
229
230 header.Name = filepath.ToSlash(header.Name)
231 if info.IsDir() {
232 header.Name += "/"
233 header.SetMode(info.Mode())
234 } else {
235 header.Method = zip.Deflate
236 header.SetMode(fixMode(info.Mode()))
237 }
238
239 writer, err := archive.CreateHeader(header)
240 if err != nil {
241 return err
242 }
243
244 if info.IsDir() {
245 return nil
246 }
247
248 file, err := os.Open(path)
249 if err != nil {

Callers 4

buildpack_test.goFile · 0.70
PrepareBuildpackBitsMethod · 0.70

Calls 5

IsDirMethod · 0.80
ModeMethod · 0.80
fixModeFunction · 0.70
CreateMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected