zip_create.go 2.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
package archives

import (
	"archive/zip"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"

10
	"github.com/sirupsen/logrus"
11
12
)

Kamil Trzcinski's avatar
Kamil Trzcinski committed
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
func createZipDirectoryEntry(archive *zip.Writer, fh *zip.FileHeader) error {
	fh.Name += "/"
	_, err := archive.CreateHeader(fh)
	return err
}

func createZipSymlinkEntry(archive *zip.Writer, fh *zip.FileHeader) error {
	fw, err := archive.CreateHeader(fh)
	if err != nil {
		return err
	}

	link, err := os.Readlink(fh.Name)
	if err != nil {
		return err
	}

	_, err = io.WriteString(fw, link)
	return err
}

func createZipFileEntry(archive *zip.Writer, fh *zip.FileHeader) error {
	fh.Method = zip.Deflate
	fw, err := archive.CreateHeader(fh)
	if err != nil {
		return err
	}

	file, err := os.Open(fh.Name)
	if err != nil {
		return err
	}

	_, err = io.Copy(fw, file)
47
	_ = file.Close()
Kamil Trzcinski's avatar
Kamil Trzcinski committed
48
49
50
51
52
53
	if err != nil {
		return err
	}
	return nil
}

54
55
56
57
58
59
60
61
func createZipEntry(archive *zip.Writer, fileName string) error {
	fi, err := os.Lstat(fileName)
	if err != nil {
		logrus.Warningln("File ignored:", err)
		return nil
	}

	fh, err := zip.FileInfoHeader(fi)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
62
63
64
	if err != nil {
		return err
	}
65
66
67
68
69
	fh.Name = fileName
	fh.Extra = createZipExtra(fi)

	switch fi.Mode() & os.ModeType {
	case os.ModeDir:
Kamil Trzcinski's avatar
Kamil Trzcinski committed
70
		return createZipDirectoryEntry(archive, fh)
71
72

	case os.ModeSymlink:
Kamil Trzcinski's avatar
Kamil Trzcinski committed
73
		return createZipSymlinkEntry(archive, fh)
74
75
76
77

	case os.ModeNamedPipe, os.ModeSocket, os.ModeDevice:
		// Ignore the files that of these types
		logrus.Warningln("File ignored:", fileName)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
78
		return nil
79
80

	default:
Kamil Trzcinski's avatar
Kamil Trzcinski committed
81
		return createZipFileEntry(archive, fh)
82
83
84
85
	}
}

func CreateZipArchive(w io.Writer, fileNames []string) error {
86
	tracker := newPathErrorTracker()
87

88
	archive := zip.NewWriter(w)
89
	defer func() { _ = archive.Close() }()
90
91

	for _, fileName := range fileNames {
92
93
94
95
		if err := errorIfGitDirectory(fileName); tracker.actionable(err) {
			printGitArchiveWarning("archive")
		}

96
97
98
99
100
101
102
103
104
105
106
		err := createZipEntry(archive, fileName)
		if err != nil {
			return err
		}
	}

	return nil
}

func CreateZipFile(fileName string, fileNames []string) error {
	// create directories to store archive
Simon Jürgensmeyer's avatar
Simon Jürgensmeyer committed
107
108
109
110
	err := os.MkdirAll(filepath.Dir(fileName), 0700)
	if err != nil {
		return err
	}
111
112
113
114
115

	tempFile, err := ioutil.TempFile(filepath.Dir(fileName), "archive_")
	if err != nil {
		return err
	}
116
117
118
119
	defer func() {
		_ = tempFile.Close()
		_ = os.Remove(tempFile.Name())
	}()
120
121
122
123
124
125

	logrus.Debugln("Temporary file:", tempFile.Name())
	err = CreateZipArchive(tempFile, fileNames)
	if err != nil {
		return err
	}
126
	_ = tempFile.Close()
127
128
129
130
131
132
133
134

	err = os.Rename(tempFile.Name(), fileName)
	if err != nil {
		return err
	}

	return nil
}