/
zip.go
164 lines (130 loc) · 2.93 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import "os"
import "github.com/hopkinsth/lambda-phage/Godeps/_workspace/src/github.com/tj/go-debug"
import "archive/zip"
import "io"
import "bytes"
import "strings"
import "fmt"
type zipFile struct {
f *os.File
fName string
*zip.Writer
}
// creates a new zip file
func newZipFile(fName string) (*zipFile, error) {
f, err := os.Create(fName)
if err != nil {
return nil, err
}
return &zipFile{
f,
fName,
zip.NewWriter(f),
}, nil
}
// adds a file to this archive
func (z *zipFile) AddFile(fName string) (int64, error) {
debug := debug.Debug("zipFile.AddFile")
debug("opening source file")
f, err := os.Open(fName)
if err != nil {
return 0, err
}
debug("source file opened")
// want to make sure we get the base name for a file,
// so fstat it, which is able to do that
// debug("fstat source file")
// s, err := f.Stat()
// if err != nil {
// return 0, err
// }
// bName := s.Name()
// add file to archive
debug("adding file to archive")
wr, err := z.Create(f.Name())
if err != nil {
return 0, err
}
n, err := io.Copy(wr, f)
if err != nil {
return 0, err
}
err = f.Close()
if err != nil {
return 0, err
}
return n, nil
}
// adds a file from string data to the archive
func (z *zipFile) AddString(fName string, str []byte) (int64, error) {
debug := debug.Debug("zipFile.AddString")
buf := bytes.NewBuffer(str)
// add file to archive
debug("adding file to archive")
wr, err := z.Create(fName)
if err != nil {
return 0, err
}
debug("copying data for file")
n, err := io.Copy(wr, buf)
if err != nil {
return 0, err
}
return n, nil
}
// adds a directory recursively
func (z *zipFile) AddDirectory(f *os.File, infoCh chan<- string) error {
debug := debug.Debug("zipFile.AddDirectory")
dirName := f.Name()
debug("adding directory %s", dirName)
fi, err := f.Readdir(-1)
if err != nil {
return err
}
for _, info := range fi {
debug("zname is...", z.f.Name())
fName := info.Name()
fullName := dirName + "/" + fName
if info.IsDir() {
if strings.Index(fName, ".") != 0 {
debug("found directory, adding file %s", dirName+"/"+fName)
if infoCh != nil {
infoCh <- fmt.Sprintf("adding %s", fullName)
}
f, err := os.Open(fullName)
if err != nil {
// TODO: prob don't want to do this ALWAYS
return err
}
err = z.AddDirectory(f, infoCh)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
}
} else if info.Name() != z.fName {
debug("have file %s in directory %s, adding now", info.Name(), dirName)
if infoCh != nil {
infoCh <- fmt.Sprintf("adding %s", fullName)
}
_, err := z.AddFile(dirName + "/" + info.Name())
if err != nil {
// TODO: prob don't want to do this ALWAYS
return err
}
}
}
return nil
}
// closes the writer and the file
func (z *zipFile) Close() error {
err := z.Writer.Close()
if err != nil {
return err
}
return z.f.Close()
}