-
Notifications
You must be signed in to change notification settings - Fork 1
/
zip.go
54 lines (51 loc) · 1009 Bytes
/
zip.go
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
package kitgo
import (
"archive/zip"
"io/ioutil"
"os"
)
//-------------------------------------
//
// ZipDir zip dir files in a zip file
//
//-------------------------------------
func ZipDir(dir string, dest string) error {
f, err := FileListPaths(dir)
if err != nil {
return err
}
return ZipFiles(f, dest)
}
//-------------------------------------
//
// ZipFiles zip provide files in a zip file
//
//-------------------------------------
func ZipFiles(fileNames []string, dest string) error {
fzip, err := os.Create(dest)
if err != nil {
return err
}
w := zip.NewWriter(fzip)
defer w.Close()
for _, file := range fileNames {
header := &zip.FileHeader{
Name: file,
Flags: 1 << 11, // 使用utf8编码
Method: zip.Deflate,
}
fileZip, err := w.CreateHeader(header)
if err != nil {
return err
}
filecontent, err := ioutil.ReadFile(file)
if err != nil {
return err
}
_, err = fileZip.Write(filecontent)
if err != nil {
return err
}
}
return nil
}