forked from mmadfox/go-crx3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip.go
57 lines (51 loc) · 1017 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
55
56
57
package crx
import (
"archive/zip"
"io"
"os"
"path/filepath"
)
// Zip creates a *.zip archive and adds all the files to it.
func Zip(w io.Writer, unpacked string) error {
if !isDir(unpacked) {
return ErrPathNotFound
}
wz := zip.NewWriter(w)
defer wz.Close()
return filepath.Walk(unpacked, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
relpath, err := filepath.Rel(unpacked, path)
if err != nil {
return err
}
return writeToZip(wz, path, relpath)
})
}
func writeToZip(w *zip.Writer, filename string, metaname string) error {
fd, err := os.Open(filename)
if err != nil {
return err
}
defer fd.Close()
info, err := fd.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = metaname
header.Method = zip.Deflate
writer, err := w.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, fd)
return err
}