-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (75 loc) · 2.1 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"archive/zip"
"bufio"
"bytes"
"io"
"log"
"os"
"path/filepath"
"strings"
)
const ignorefile string = ".archiveignore"
func main() {
ignore := make(map[string]bool) // Setup files to ignore, set defaults
ignore[ignorefile] = true // Ignore the config file
ignore[filepath.Base(os.Args[0])] = true // Ignore this executable
// Read in .archiveignore file if present
f, err := os.Open(ignorefile)
if err != nil && !os.IsNotExist(err) {
log.Fatalf("Error with %s: %s", ignorefile, err)
}
defer f.Close()
if f != nil {
// Store all Globs in a map from the file
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// // Get all files that match the ignore pattern
filepath.Walk(strings.TrimSpace(scanner.Text()), func(path string, info os.FileInfo, err error) error {
if path != "" {
ignore[path] = true
}
return nil
})
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
buf := new(bytes.Buffer) // Create Byte buffer to hold archive
w := zip.NewWriter(buf) // Create Zip Writer on Buffer
defer w.Close() // Defer close
// Walk all files in this folder and sub-folders
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if ignore[path] != true && !info.IsDir() {
// Open the file
fileToZip, err := os.Open(path)
if err != nil {
return err
}
defer fileToZip.Close()
// Get the file information
info, err := fileToZip.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
// Using FileInfoHeader() above only uses the basename of the file. If we want
// to preserve the folder structure we can overwrite this with the full path.
header.Name = filepath.ToSlash(path)
// Change to deflate to gain better compression
// see http://golang.org/pkg/archive/zip/#pkg-constants
header.Method = zip.Deflate
writer, err := w.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, fileToZip)
}
return nil
})
// Push archive to Server
}