zip_create.go 2.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
package archives

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

	"github.com/Sirupsen/logrus"
)

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
47
48
49
50
51
52
53
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)
	file.Close()
	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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	}
}

func CreateZipArchive(w io.Writer, fileNames []string) error {
	archive := zip.NewWriter(w)
	defer archive.Close()

	for _, fileName := range fileNames {
		err := createZipEntry(archive, fileName)
		if err != nil {
			return err
		}
	}

	return nil
}

func CreateZipFile(fileName string, fileNames []string) error {
	// create directories to store archive
	os.MkdirAll(filepath.Dir(fileName), 0700)

	tempFile, err := ioutil.TempFile(filepath.Dir(fileName), "archive_")
	if err != nil {
		return err
	}
	defer tempFile.Close()
	defer os.Remove(tempFile.Name())

	logrus.Debugln("Temporary file:", tempFile.Name())
	err = CreateZipArchive(tempFile, fileNames)
	if err != nil {
		return err
	}
	tempFile.Close()

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

	return nil
}