-
Notifications
You must be signed in to change notification settings - Fork 0
/
ziplib.go
158 lines (129 loc) · 2.82 KB
/
ziplib.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
package ziplib
import (
"archive/zip"
"bytes"
"compress/flate"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// ZipFolder source path -> target.zip
func ZipFolder(source, target string) error {
if strings.ToLower(filepath.Ext(target)) != ".zip" {
return fmt.Errorf("target must be end with .zip")
}
if _, err := os.Stat(source); err != nil {
return err
}
zipfile, err := os.Create(target)
if err != nil {
return err
}
defer zipfile.Close()
archive := zip.NewWriter(zipfile)
defer archive.Close()
return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = strings.TrimPrefix(path, source)
if info.IsDir() {
header.Name += string(os.PathSeparator)
} else {
header.Method = zip.Deflate
}
writer, err := archive.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
return err
})
}
// Unzip zipfile -> target folder
func Unzip(archive, target string) error {
reader, err := zip.OpenReader(archive)
if err != nil {
return err
}
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
for _, file := range reader.File {
path := filepath.Join(target, file.Name)
if file.FileInfo().IsDir() {
os.MkdirAll(path, file.Mode())
continue
}
fileReader, err := file.Open()
if err != nil {
return err
}
defer fileReader.Close()
targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
return err
}
defer targetFile.Close()
if _, err := io.Copy(targetFile, fileReader); err != nil {
return err
}
}
return nil
}
// Compress zip
func Compress(data []byte) ([]byte, error) {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
w.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
return flate.NewWriter(out, flate.BestCompression)
})
f, err := w.Create("filename.ext")
if err != nil {
return nil, err
}
if _, err := f.Write(data); err != nil {
return nil, err
}
if err := w.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Decompress zip
func Decompress(data []byte) ([]byte, error) {
r, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return nil, err
}
r.RegisterDecompressor(zip.Deflate, func(r io.Reader) io.ReadCloser {
return flate.NewReader(r)
})
if len(r.File) == 0 {
return nil, fmt.Errorf("zip empty")
}
f, err := r.File[0].Open()
if err != nil {
return nil, err
}
defer f.Close()
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, f); err != nil {
return nil, err
}
return buf.Bytes(), nil
}