-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.go
More file actions
42 lines (34 loc) · 784 Bytes
/
Copy pathzip.go
File metadata and controls
42 lines (34 loc) · 784 Bytes
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
package main
import (
"archive/zip"
"io/ioutil"
"log"
"os"
)
func zipUp(filesToArchive []string) {
// Create a file to write the archive buffer to
outFile, err := os.Create("files.zip")
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
// Create a zip writer on top of the file writer
zipWriter := zip.NewWriter(outFile)
// Create and write files to the archive
for i := 1; i < len(filesToArchive); i++ {
fileWriter, err := zipWriter.Create(filesToArchive[i])
if err != nil {
log.Fatal(err)
}
data, err := ioutil.ReadFile(filesToArchive[i])
_, err = fileWriter.Write([]byte(data))
if err != nil {
log.Fatal(err)
}
}
// Clean up
err = zipWriter.Close()
if err != nil {
log.Fatal(err)
}
}