forked from rande/gitlab-ci-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip.go
66 lines (52 loc) · 1.23 KB
/
zip.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
// Copyright © 2016 Thomas Rabaix <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gitlab_ci_helper
import (
"errors"
"github.com/rande/garchive"
"os"
"path/filepath"
"regexp"
"strings"
)
func Unzip(archive, target string) error {
return garchive.ExtractZipFile(archive, target)
}
func Zip(includePaths, excludePaths Paths, target string) error {
var excludes []*regexp.Regexp
for _, path := range excludePaths {
excludes = append(excludes, regexp.MustCompile(path))
}
files := []string{}
for _, source := range includePaths {
info, err := os.Stat(source)
if err != nil {
return nil
}
var baseDir string
if info.IsDir() {
baseDir = source
}
filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
for _, exclude := range excludes {
if exclude.Match([]byte(path)) {
return nil
}
}
if baseDir != "" {
path = filepath.Join(baseDir, strings.TrimPrefix(path, source))
}
files = append(files, path)
return err
})
}
if len(files) == 0 {
return errors.New("No file to zip")
}
return garchive.CreateZipFile(target, files)
}