-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip.go
127 lines (105 loc) · 2.3 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
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"archive/zip"
"bytes"
"crypto/sha256"
"encoding/hex"
"io"
"io/ioutil"
"os"
"path/filepath"
)
func HashFiles(files ...string) (string, error) {
h := sha256.New()
for _, file := range files {
data, err := ioutil.ReadFile(file)
if os.IsNotExist(err) {
continue
}
if err != nil {
return "", err
}
h.Write(data)
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func Zip(root string) ([]byte, error) {
var b bytes.Buffer
archive := zip.NewWriter(&b)
archiveErr := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
// figure out path with forward slashes
rpath, relErr := filepath.Rel(root, path)
if relErr != nil {
return relErr
}
cpath := filepath.ToSlash(rpath)
// create a zipped file
zipFile, createErr := archive.Create(cpath)
if createErr != nil {
return createErr
}
// open source file
dataFile, openErr := os.Open(path)
if openErr != nil {
return openErr
}
// copy data
_, copyErr := io.Copy(zipFile, dataFile)
if copyErr != nil && copyErr != io.EOF {
return copyErr
}
// close source file
if closeErr := dataFile.Close(); closeErr != nil {
return closeErr
}
return nil
})
if archiveErr != nil {
return nil, archiveErr
}
if err := archive.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func Unzip(data []byte, root string) error {
b := bytes.NewReader(data)
archive, err := zip.NewReader(b, b.Size())
if err != nil {
return err
}
for _, zipFile := range archive.File {
if zipFile.FileInfo().IsDir() {
continue
}
zipReader, openErr := zipFile.Open()
if openErr != nil {
return openErr
}
path := filepath.Join(root, filepath.FromSlash(zipFile.Name))
mkdirErr := os.MkdirAll(filepath.Dir(path), 0700)
if mkdirErr != nil {
return mkdirErr
}
dataFile, createErr := os.Create(path)
if createErr != nil {
return createErr
}
_, copyErr := io.Copy(dataFile, zipReader)
if copyErr != nil && copyErr != io.EOF {
return copyErr
}
if closeErr := dataFile.Close(); closeErr != nil {
return closeErr
}
if closeErr := zipReader.Close(); closeErr != nil {
return closeErr
}
}
return nil
}