-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathzip.go
202 lines (166 loc) · 4.08 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Gulu - Golang common utilities for everyone.
// Copyright (c) 2019-present, b3log.org
//
// Gulu is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package gulu
import (
"archive/zip"
"bytes"
"io"
"os"
"path/filepath"
"unicode/utf8"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
// ZipFile represents a zip file.
type ZipFile struct {
zipFile *os.File
writer *zip.Writer
}
// Create creates a zip file with the specified filename.
func (*GuluZip) Create(filename string) (*ZipFile, error) {
file, err := os.Create(filename)
if nil != err {
return nil, err
}
return &ZipFile{zipFile: file, writer: zip.NewWriter(file)}, nil
}
// Close closes the zip file writer.
func (z *ZipFile) Close() error {
if err := z.writer.Close(); nil != err {
return err
}
return z.zipFile.Close() // close the underlying writer
}
// AddEntry adds an entry.
func (z *ZipFile) AddEntry(path, name string, callback ...func(filename string)) error {
fi, err := os.Stat(name)
if nil != err {
return err
}
fh, err := zip.FileInfoHeader(fi)
if nil != err {
return err
}
fh.Name = filepath.ToSlash(filepath.Clean(path))
fh.Method = zip.Deflate // data compression algorithm
if fi.IsDir() {
fh.Name = fh.Name + "/" // be care the ending separator
}
entry, err := z.writer.CreateHeader(fh)
if nil != err {
return err
}
if fi.IsDir() {
return nil
}
file, err := os.Open(name)
if nil != err {
return err
}
defer file.Close()
_, err = io.Copy(entry, file)
if 0 < len(callback) {
callback[0](name)
}
return err
}
// AddDirectory adds a directory.
func (z *ZipFile) AddDirectory(path, dirName string, callback ...func(string)) error {
files, err := os.ReadDir(dirName)
if nil != err {
return err
}
if 0 == len(files) {
err = z.AddEntry(path, dirName, callback...)
if nil != err {
return err
}
return nil
}
for _, file := range files {
localPath := filepath.Join(dirName, file.Name())
zipPath := filepath.Join(path, file.Name())
err = nil
if file.IsDir() {
err = z.AddDirectory(zipPath, localPath, callback...)
} else {
err = z.AddEntry(zipPath, localPath, callback...)
}
if nil != err {
return err
}
}
return nil
}
// Unzip extracts a zip file specified by the zipFilePath to the destination.
func (*GuluZip) Unzip(zipFilePath, destination string, callback ...func(filename string)) error {
r, err := zip.OpenReader(zipFilePath)
if nil != err {
return err
}
defer r.Close()
var cb func(string)
if 0 < len(callback) {
cb = callback[0]
}
for _, f := range r.File {
err = cloneZipItem(f, destination, cb)
if nil != err {
return err
}
}
return nil
}
func cloneZipItem(f *zip.File, dest string, callback func(filename string)) error {
// create full directory path
fileName := f.Name
if !utf8.ValidString(fileName) {
data, err := io.ReadAll(transform.NewReader(bytes.NewReader([]byte(fileName)), simplifiedchinese.GB18030.NewDecoder()))
if nil == err {
fileName = string(data)
} else {
logger.Error(err)
}
}
path := filepath.Join(dest, fileName)
err := os.MkdirAll(filepath.Dir(path), 0755)
if nil != err {
return err
}
if f.FileInfo().IsDir() {
if err = os.MkdirAll(path, 0755); nil != err {
return err
}
return nil
}
// clone if item is a file
rc, err := f.Open()
if nil != err {
return err
}
defer rc.Close()
// use os.Create() since Zip don't store file permissions
fileCopy, err := os.Create(path)
if nil != err {
return err
}
defer fileCopy.Close()
_, err = io.Copy(fileCopy, rc)
if nil != err {
return err
}
if err = os.Chtimes(path, f.Modified, f.Modified); nil != err {
return err
}
if nil != callback {
callback(path)
}
return nil
}