zip_create.go 2.59 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
	fh.Name = fileName
	fh.Extra = createZipExtra(fi)
67
68
	// Set EFS flag to indicate that filenames and comments are UTF-8 encoded
	fh.Flags = 0x800
69
70
71

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

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

	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
80
		return nil
81
82

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

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

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

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

98
99
100
101
102
103
104
105
106
107
108
		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
109
110
111
112
	err := os.MkdirAll(filepath.Dir(fileName), 0700)
	if err != nil {
		return err
	}
113
114
115
116
117

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

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

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

	return nil
}