-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.go
More file actions
300 lines (260 loc) · 7.71 KB
/
extract.go
File metadata and controls
300 lines (260 loc) · 7.71 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package gnsys
import (
"archive/tar"
"archive/zip"
"compress/bzip2"
"compress/gzip"
"errors"
"io"
"os"
"path/filepath"
"github.com/ulikunitz/xz"
)
type Extractor func(src, dst string) error
// ExtractZip extracts a zip archive located at srcPath to the destination
// directory dstDir.
func ExtractZip(srcPath, dstDir string) error {
exists, _ := FileExists(srcPath)
if !exists {
return &ErrFileMissing{Path: srcPath}
}
// Open the zip file for reading.
r, err := zip.OpenReader(srcPath)
if err != nil {
return &ErrExtract{Path: srcPath, Err: err}
}
defer r.Close()
for _, f := range r.File {
fpath := filepath.Join(dstDir, f.Name)
if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return &ErrExtract{Path: fpath, Err: err}
}
// If it's a directory, move on to the next entry.
if f.FileInfo().IsDir() {
if err := os.MkdirAll(fpath, os.ModePerm); err != nil {
return &ErrExtract{Path: fpath, Err: err}
}
continue
}
// Open the file within the zip.
rc, err := f.Open()
if err != nil {
return &ErrExtract{Path: fpath, Err: err}
}
defer rc.Close()
// Create a file in the filesystem.
outFile, err := os.OpenFile(
fpath,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
f.Mode(),
)
if err != nil {
return &ErrExtract{Path: fpath, Err: err}
}
defer outFile.Close()
// Copy the contents of the file from the zip to the new file.
_, err = io.Copy(outFile, rc)
if err != nil {
return &ErrExtract{Path: fpath, Err: err}
}
}
return nil
}
// ExtractGz extracts a gz compressed file located at srcPath to the
// destination directory dstDir.
func ExtractGz(srcPath, dstDir string) error {
gzReader, cleanup, err := newGzReader(srcPath)
if err != nil {
return err
}
defer cleanup()
// Determine the destination file name.
dstFileName := filepath.Base(srcPath)
dstFileName = dstFileName[:len(dstFileName)-3] // Remove ".gz" extension
dstPath := filepath.Join(dstDir, dstFileName)
// Create the destination file.
dstFile, err := os.OpenFile(dstPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return &ErrExtract{Path: dstPath, Err: err}
}
defer dstFile.Close()
// Copy the file contents from the gzip reader to the destination file.
if _, err := io.Copy(dstFile, gzReader); err != nil {
return &ErrExtract{Path: dstPath, Err: err}
}
return nil
}
// ExtractBz2 extracts a bz2 compressed file located at srcPath to the
// destination directory dstDir.
func ExtractBz2(srcPath, dstDir string) error {
bzReader, cleanup, err := newBz2Reader(srcPath)
if err != nil {
return err
}
defer cleanup()
// Determine the destination file name.
dstFileName := filepath.Base(srcPath)
dstFileName = dstFileName[:len(dstFileName)-4] // Remove ".bz2" extension
dstPath := filepath.Join(dstDir, dstFileName)
// Create the destination file.
dstFile, err := os.OpenFile(dstPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return &ErrExtract{Path: dstPath, Err: err}
}
defer dstFile.Close()
// Copy the file contents from the bzip2 reader to the destination file.
if _, err := io.Copy(dstFile, bzReader); err != nil {
return &ErrExtract{Path: dstPath, Err: err}
}
return nil
}
// ExtractXz extracts an xz compressed file located at srcPath to the
// destination directory dstDir.
func ExtractXz(srcPath, dstDir string) error {
xzReader, cleanup, err := newXzReader(srcPath)
if err != nil {
return err
}
defer cleanup()
// Determine the destination file name.
dstFileName := filepath.Base(srcPath)
dstFileName = dstFileName[:len(dstFileName)-3] // Remove ".xz" extension
dstPath := filepath.Join(dstDir, dstFileName)
// Create the destination file.
dstFile, err := os.OpenFile(dstPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return &ErrExtract{Path: dstPath, Err: err}
}
defer dstFile.Close()
// Copy the file contents from the xz reader to the destination file.
if _, err := io.Copy(dstFile, xzReader); err != nil {
return &ErrExtract{Path: dstPath, Err: err}
}
return nil
}
// ExtractTar extracts a tar archive located at srcPath to the destination
// directory dstDir.
func ExtractTar(srcPath, dstDir string) error {
// Open the tar archive for reading.
file, err := os.Open(srcPath)
if err != nil {
return &ErrExtract{Path: srcPath, Err: err}
}
defer file.Close()
tr := tar.NewReader(file)
return untar(tr, srcPath, dstDir)
}
// ExtractTarGz extracts a tar.gz archive located at srcPath to the destination
// directory dstDir.
func ExtractTarGz(srcPath, dstDir string) error {
gzReader, cleanup, err := newGzReader(srcPath)
if err != nil {
return err
}
defer cleanup()
tr := tar.NewReader(gzReader)
return untar(tr, srcPath, dstDir)
}
// ExtractTarBz2 extracts a tar.bz2 archive located at srcPath to the destination
// directory dstDir.
func ExtractTarBz2(srcPath, dstDir string) error {
bzReader, cleanup, err := newBz2Reader(srcPath)
if err != nil {
return err
}
defer cleanup()
tr := tar.NewReader(bzReader)
return untar(tr, srcPath, dstDir)
}
// ExtractTarXz extracts a tar.xz archive located at srcPath to the destination
// directory dstDir.
func ExtractTarXz(srcPath, dstDir string) error {
xzReader, cleanup, err := newXzReader(srcPath)
if err != nil {
return err
}
defer cleanup()
tr := tar.NewReader(xzReader)
return untar(tr, srcPath, dstDir)
}
// newBz2Reader opens a bz2 file and returns a reader for its decompressed content.
// The caller must call the returned cleanup function to close the file.
func newBz2Reader(srcPath string) (io.Reader, func(), error) {
file, err := os.Open(srcPath)
if err != nil {
return nil, nil, &ErrExtract{Path: srcPath, Err: err}
}
bzReader := bzip2.NewReader(file)
return bzReader, func() { file.Close() }, nil
}
// newXzReader opens an xz file and returns a reader for its decompressed content.
// The caller must call the returned cleanup function to close the file.
func newXzReader(srcPath string) (io.Reader, func(), error) {
file, err := os.Open(srcPath)
if err != nil {
return nil, nil, &ErrExtract{Path: srcPath, Err: err}
}
xzReader, err := xz.NewReader(file)
if err != nil {
file.Close()
return nil, nil, &ErrExtract{Path: srcPath, Err: err}
}
return xzReader, func() { file.Close() }, nil
}
// newGzReader opens a gz file and returns a reader for its decompressed content.
// The caller must call the returned cleanup function to close resources.
func newGzReader(srcPath string) (io.Reader, func(), error) {
file, err := os.Open(srcPath)
if err != nil {
return nil, nil, &ErrExtract{Path: srcPath, Err: err}
}
gzReader, err := gzip.NewReader(file)
if err != nil {
file.Close()
return nil, nil, &ErrExtract{Path: srcPath, Err: err}
}
return gzReader, func() { gzReader.Close(); file.Close() }, nil
}
func untar(tarReader *tar.Reader, srcPath, dstDir string) error {
var writer *os.File
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return &ErrExtract{Path: srcPath, Err: err}
}
// Get the individual filepath from the header.
filepath := filepath.Join(dstDir, header.Name)
switch header.Typeflag {
case tar.TypeDir:
// Handle directory.
err = os.MkdirAll(filepath, os.FileMode(header.Mode))
if err != nil {
return &ErrExtract{Path: srcPath, Err: err}
}
case tar.TypeReg:
// Handle regular file.
writer, err = os.Create(filepath)
if err != nil {
return &ErrExtract{Path: srcPath, Err: err}
}
if _, err := io.Copy(writer, tarReader); err != nil {
writer.Close()
return &ErrExtract{Path: srcPath, Err: err}
}
writer.Close()
default:
return &ErrExtract{Path: srcPath, Err: err}
}
}
state := GetDirState(dstDir)
if state == DirEmpty {
return &ErrExtract{
Path: dstDir,
Err: errors.New("bad tar file"),
}
}
return nil
}