-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.go
More file actions
52 lines (48 loc) · 1.19 KB
/
zip.go
File metadata and controls
52 lines (48 loc) · 1.19 KB
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
package main
import (
"archive/zip"
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/apex/log"
)
func readZip(r io.Reader) (*zip.Reader, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("failed to retrieve file: %s", err)
}
z, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
if err != nil {
return nil, fmt.Errorf("failed to read file: %s", err)
}
return z, nil
}
func parseZipFile(r *zip.Reader) (string, map[string][]byte) {
fileMap := map[string][]byte{}
htmlFile := ""
for i, f := range r.File {
if strings.HasSuffix(f.Name, ".html") {
if htmlFile != "" {
log.WithFields(log.Fields{
"first": htmlFile,
"second": f.Name,
}).Fatal("Multiple HTML files in export, not supported")
}
htmlFile = f.Name
}
rc, err := f.Open()
if err != nil {
log.WithError(err).WithField("filename", f.Name).Fatal("Failed to open zipped file")
}
b, err := ioutil.ReadAll(rc)
if err != nil {
log.WithError(err).WithField("filename", f.Name).Fatal("Failed to read zipped file")
}
fileMap[f.Name] = b
log.WithField("filename", f.Name).WithField("size", len(b)).Infof("File %d:", i)
rc.Close()
}
return htmlFile, fileMap
}