-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathzip.go
More file actions
44 lines (39 loc) · 711 Bytes
/
zip.go
File metadata and controls
44 lines (39 loc) · 711 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
43
44
package main
import (
"archive/zip"
"io"
"os"
)
type Zip struct {
*zip.Writer
}
func CreateZip(filename string) (*Zip, error) {
fd, err := os.Create(filename)
if err != nil {
return nil, err
}
zipper := zip.NewWriter(fd)
return &Zip{Writer: zipper}, nil
}
func (z *Zip) Add(filename string) error {
info, rdc, err := statFile(filename)
if err != nil {
return err
}
defer rdc.Close()
hdr, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
hdr.Name = sanitizedName(filename)
if info.IsDir() {
hdr.Name += "/"
}
hdr.Method = zip.Deflate // compress method
writer, err := z.CreateHeader(hdr)
if err != nil {
return err
}
_, err = io.Copy(writer, rdc)
return err
}