11 // ZipDecompressor is an implementation of Decompressor that can
12 // decompress tar.gzip files.
13 type ZipDecompressor struct{}
15 func (d *ZipDecompressor) Decompress(dst, src string, dir bool) error {
16 // If we're going into a directory we should make that first
19 mkdir = filepath.Dir(dst)
21 if err := os.MkdirAll(mkdir, 0755); err != nil {
26 zipR, err := zip.OpenReader(src)
32 // Check the zip integrity
33 if len(zipR.File) == 0 {
35 return fmt.Errorf("empty archive: %s", src)
37 if !dir && len(zipR.File) > 1 {
38 return fmt.Errorf("expected a single file: %s", src)
41 // Go through and unarchive
42 for _, f := range zipR.File {
45 path = filepath.Join(path, f.Name)
48 if f.FileInfo().IsDir() {
50 return fmt.Errorf("expected a single file: %s", src)
53 // A directory, just make the directory and continue unarchiving...
54 if err := os.MkdirAll(path, 0755); err != nil {
61 // Create the enclosing directories if we must. ZIP files aren't
62 // required to contain entries for just the directories so this
65 if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
70 // Open the file for reading
76 // Open the file for writing
77 dstF, err := os.Create(path)
82 _, err = io.Copy(dstF, srcF)
90 if err := os.Chmod(path, f.Mode()); err != nil {