forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipper.go
69 lines (57 loc) · 1.36 KB
/
zipper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cf
import (
"archive/zip"
"errors"
"fileutils"
"os"
"path/filepath"
)
type Zipper interface {
Zip(dirToZip string, targetFile *os.File) (err error)
}
type ApplicationZipper struct{}
var doNotZipExtensions = []string{".zip", ".war", ".jar"}
func (zipper ApplicationZipper) Zip(dirOrZipFile string, targetFile *os.File) (err error) {
if shouldNotZip(filepath.Ext(dirOrZipFile)) {
err = fileutils.CopyPathToWriter(dirOrZipFile, targetFile)
} else {
err = writeZipFile(dirOrZipFile, targetFile)
}
targetFile.Seek(0, os.SEEK_SET)
return
}
func shouldNotZip(extension string) (result bool) {
for _, ext := range doNotZipExtensions {
if ext == extension {
return true
}
}
return
}
func writeZipFile(dir string, targetFile *os.File) (err error) {
isEmpty, err := fileutils.IsDirEmpty(dir)
if err != nil {
return
}
if isEmpty {
err = errors.New("Directory is empty")
return
}
writer := zip.NewWriter(targetFile)
defer writer.Close()
err = WalkAppFiles(dir, func(fileName string, fullPath string) (err error) {
fileInfo, err := os.Stat(fullPath)
if err != nil {
return err
}
header, err := zip.FileInfoHeader(fileInfo)
header.Name = filepath.ToSlash(fileName)
if err != nil {
return err
}
zipFilePart, err := writer.CreateHeader(header)
err = fileutils.CopyPathToWriter(fullPath, zipFilePart)
return
})
return
}