about summary refs log tree commit diff
path: root/third_party/gerrit/detzip.go
blob: 511c18ecfe170e30dfa81e6f47f4944cc010492a (plain) (blame)
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
93
94
95
96
97
package main

import (
	"archive/zip"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"sort"
	"strings"
)

var (
	exclude = flag.String("exclude", "", "comma-separated list of filenames to exclude (in any directory)")
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [zip file] [directory]:\n", os.Args[0])
		flag.PrintDefaults()
	}
}

func listToMap(ss []string) map[string]bool {
	m := make(map[string]bool)
	for _, s := range ss {
		m[s] = true
	}
	return m
}

func main() {
	flag.Parse()
	if flag.NArg() != 2 {
		flag.Usage()
		os.Exit(1)
	}

	outPath := flag.Arg(0)
	dirPath := flag.Arg(1)

	excludeFiles := listToMap(strings.Split(*exclude, ","))

	// Aggregate all files first.
	var files []string
	filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}
		if excludeFiles[info.Name()] {
			return nil
		}
		files = append(files, path)
		return nil
	})

	// Create zip
	outW, err := os.Create(outPath)
	if err != nil {
		log.Fatalf("Create(%q): %v", outPath, err)
	}

	zipW := zip.NewWriter(outW)

	// Output files in alphabetical order
	sort.Strings(files)
	for _, f := range files {
		fw, err := zipW.CreateHeader(&zip.FileHeader{
			Name:   f,
			Method: zip.Store,
		})
		if err != nil {
			log.Fatalf("creating %q in zip: %v", f, err)
		}

		ff, err := os.Open(f)
		if err != nil {
			log.Fatalf("opening %q: %v", f, err)
		}
		if _, err := io.Copy(fw, ff); err != nil {
			log.Fatalf("copying %q to zip: %v", f, err)
		}
		ff.Close()
	}

	if err := zipW.Close(); err != nil {
		log.Fatalf("writing ZIP central directory: %v", err)
	}
	if err := outW.Close(); err != nil {
		log.Fatalf("closing ZIP file: %v", err)
	}
}