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

Function Zipit

integration/helpers/app.go:284–352  ·  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

282
283// Zipit zips the source into a .zip file in the target dir.
284func Zipit(source, target, prefix string) error {
285 // Thanks to Svett Ralchev
286 // http://blog.ralch.com/tutorial/golang-working-with-zip/
287
288 zipfile, err := os.Create(target)
289 if err != nil {
290 return err
291 }
292 defer zipfile.Close()
293
294 if prefix != "" {
295 _, err = io.WriteString(zipfile, prefix)
296 if err != nil {
297 return err
298 }
299 }
300
301 archive := zip.NewWriter(zipfile)
302 defer archive.Close()
303
304 err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
305 if err != nil {
306 return err
307 }
308
309 if path == source {
310 return nil
311 }
312
313 header, err := zip.FileInfoHeader(info)
314 if err != nil {
315 return err
316 }
317 header.Name, err = filepath.Rel(source, path)
318 if err != nil {
319 return err
320 }
321
322 header.Name = filepath.ToSlash(header.Name)
323
324 if info.IsDir() {
325 header.Name += "/"
326 header.SetMode(0755)
327 } else {
328 header.Method = zip.Deflate
329 header.SetMode(0744)
330 }
331
332 writer, err := archive.CreateHeader(header)
333 if err != nil {
334 return err
335 }
336
337 if info.IsDir() {
338 return nil
339 }
340
341 file, err := os.Open(path)

Calls 3

IsDirMethod · 0.80
CreateMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected