-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress.go
More file actions
411 lines (341 loc) · 11 KB
/
compress.go
File metadata and controls
411 lines (341 loc) · 11 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package pathlib
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/spf13/afero"
)
const (
maxSize = 1 * 1024 * 1024 * 1024 // 1GB max size
)
var (
ErrIllegalFilePath = errors.New("illegal file path")
ErrFileTooLarge = errors.New("file exceeded maximum allowed size")
ErrIncompleteWrite = errors.New("incomplete write: consider increasing the maxSize parameter or checking for disk space issues")
)
// CompressOptions holds the options for compression and decompression operations
type CompressOptions struct {
MaxSize int64
}
// defaultCompressOptions returns the default options for compression and decompression
func defaultCompressOptions() CompressOptions {
return CompressOptions{
MaxSize: maxSize,
}
}
// CompressOption defines the method to modify CompressOptions
type CompressOption func(*CompressOptions)
// WithMaxSize sets the MaxSize option
func WithMaxSize(maxSize int64) CompressOption {
return func(o *CompressOptions) {
o.MaxSize = maxSize
}
}
func applyCompressOptions(opts ...CompressOption) CompressOptions {
options := defaultCompressOptions()
for _, opt := range opts {
opt(&options)
}
return options
}
// ZipDir compresses the directory represented by this FsPath into a zip file.
// The zip file is created in the same folder as the directory.
//
// Parameters:
// - zipFileName: The name of the zip file to be created. If it doesn't end with ".zip",
// the ".zip" extension will be automatically added.
//
// Returns:
// - *FsPath: A new FsPath representing the created zip file.
// - int: The total number of files compressed into the zip file.
// - error: An error if any step of the compression process fails.
//
// The function performs the following steps:
// 1. Checks if the current FsPath represents a directory.
// 2. Prepares the compression by creating the zip file.
// 3. Creates a new zip writer.
// 4. Walks through the directory, adding each file to the zip archive.
// 5. Closes the zip writer and the file.
//
// Possible errors include:
// - ErrNotDirectory if the current FsPath is not a directory.
// - I/O errors during file creation or writing.
// - Errors encountered while walking the directory structure.
//
// Example usage:
//
// dirPath := Path("/path/to/directory")
// zipPath, fileCount, err := dirPath.ZipDir("archive.zip")
// if err != nil {
// log.Fatalf("Failed to create zip: %v", err)
// }
// fmt.Printf("Created zip at %s with %d files\n", zipPath, fileCount)
func (p *FsPath) ZipDir(zipFileName string) (*FsPath, int, error) {
zipPath, zipFile, err := p.prepareCompression(zipFileName, ".zip")
if err != nil {
return nil, 0, err
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
totalFiles, err := p.compressDirectoryToWriter(
func(name string, info os.FileInfo) (io.Writer, error) {
return zipWriter.Create(name)
},
)
if err != nil {
return nil, 0, err
}
return zipPath, totalFiles, nil
}
// TarGzDir compresses the directory represented by this FsPath into a tar.gz file.
// The tar.gz file is created in the same folder as the directory.
//
// Parameters:
// - tarGzFileName: The name of the tar.gz file to be created. If it doesn't end with ".tar.gz",
// the ".tar.gz" extension will be automatically added.
//
// Returns:
// - *FsPath: A new FsPath representing the created tar.gz file.
// - int: The total number of files compressed into the tar.gz file.
// - error: An error if any step of the compression process fails.
//
// The function performs the following steps:
// 1. Checks if the current FsPath represents a directory.
// 2. Prepares the compression by creating the tar.gz file.
// 3. Creates a new gzip writer.
// 4. Creates a new tar writer.
// 5. Walks through the directory, adding each file to the tar archive.
// 6. Closes the tar writer, gzip writer, and the file.
//
// Possible errors include:
// - ErrNotDirectory if the current FsPath is not a directory.
// - I/O errors during file creation or writing.
// - Errors encountered while walking the directory structure.
// - Errors in creating tar headers or writing tar entries.
//
// Example usage:
//
// dirPath := Path("/path/to/directory")
// tarGzPath, fileCount, err := dirPath.TarGzDir("archive.tar.gz")
// if err != nil {
// log.Fatalf("Failed to create tar.gz: %v", err)
// }
// fmt.Printf("Created tar.gz at %s with %d files\n", tarGzPath, fileCount)
//
// Note: This function compresses the entire directory structure, including subdirectories.
// Empty directories are included in the archive.
func (p *FsPath) TarGzDir(tarGzFileName string) (*FsPath, int, error) {
tarGzPath, file, err := p.prepareCompression(tarGzFileName, ".tar.gz")
if err != nil {
return nil, 0, err
}
defer file.Close()
gzipWriter := gzip.NewWriter(file)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
totalFiles, err := p.compressDirectoryToWriter(
func(name string, info os.FileInfo) (io.Writer, error) {
header, err := tar.FileInfoHeader(info, name)
if err != nil {
return nil, fmt.Errorf("failed to create tar header: %w", err)
}
header.Name = name
if err := tarWriter.WriteHeader(header); err != nil {
return nil, fmt.Errorf("failed to write tar header: %w", err)
}
return tarWriter, nil
},
)
if err != nil {
return nil, 0, err
}
return tarGzPath, totalFiles, nil
}
// Untar extracts the contents of the tar file to the specified destination directory.
func (p *FsPath) Untar(destDir string, opts ...CompressOption) error {
options := applyCompressOptions(opts...)
// Create the subdirectory for extraction
subDir := Path(destDir).Join(strings.TrimSuffix(p.Name, ".tar.gz"))
if err := subDir.MkdirAll(_mode755); err != nil {
return fmt.Errorf("failed to create subdirectory: %w", err)
}
// Prepare the tar reader
tarReader, cleanup, err := p.prepareUntarEnvironment()
if err != nil {
return err
}
defer cleanup()
for {
header, err := tarReader.Next()
switch {
case errors.Is(err, io.EOF):
return nil
case err != nil:
return err
case header == nil:
continue
}
target := subDir.Join(header.Name)
switch header.Typeflag {
case tar.TypeDir:
if err := target.MkdirAll(os.FileMode(header.Mode & _mode777)); err != nil {
return err
}
case tar.TypeReg:
if err := target.MkParentDir(); err != nil {
return err
}
if err := p.extractTarFile(target, header, tarReader, options.MaxSize); err != nil {
return err
}
}
}
}
// Unzip extracts the contents of the zip file to the specified destination directory.
func (p *FsPath) Unzip(destDir string, opts ...CompressOption) error {
options := applyCompressOptions(opts...)
subDir := Path(destDir).Join(strings.TrimSuffix(p.Name, ".zip"))
if err := subDir.MkdirAll(_mode755); err != nil {
return fmt.Errorf("failed to create subdirectory: %w", err)
}
reader, err := zip.OpenReader(p.absPath)
if err != nil {
return fmt.Errorf("failed to open zip file: %w", err)
}
defer reader.Close()
for _, file := range reader.File {
err := p.extractZipFile(file, subDir, options.MaxSize)
if err != nil {
return err
}
}
return nil
}
func (p *FsPath) extractTarFile(target *FsPath, header *tar.Header, tarReader *tar.Reader, maxSize int64) error {
if header.Size > maxSize {
return fmt.Errorf("%w: %s (size: %d bytes, max allowed: %d bytes)", ErrFileTooLarge, header.Name, header.Size, maxSize)
}
file, err := target.fs.OpenFile(target.absPath, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode&_mode777))
if err != nil {
return err
}
defer file.Close()
written, err := io.Copy(file, io.LimitReader(tarReader, header.Size))
if err != nil {
return err
}
if written != header.Size {
return fmt.Errorf("%w: %s (wrote %d of %d bytes)", ErrIncompleteWrite, header.Name, written, header.Size)
}
return nil
}
func (p *FsPath) prepareUntarEnvironment() (*tar.Reader, func(), error) {
file, err := p.fs.Open(p.absPath)
if err != nil {
return nil, nil, err
}
gzr, err := gzip.NewReader(file)
if err != nil {
file.Close()
return nil, nil, err
}
tarReader := tar.NewReader(gzr)
cleanup := func() {
gzr.Close()
file.Close()
}
return tarReader, cleanup, nil
}
func (p *FsPath) extractZipFile(file *zip.File, destDir *FsPath, maxSize int64) error {
// Use FsPath.Join instead of filepath.Join
filePath := destDir.Join(file.Name)
// Check for path traversal
if !strings.HasPrefix(filePath.absPath, destDir.absPath) {
return fmt.Errorf("%w: illegal file path %s", ErrIllegalFilePath, file.Name)
}
if file.FileInfo().IsDir() {
return filePath.MkdirAll(_mode755)
}
if file.UncompressedSize64 > uint64(maxSize) {
return fmt.Errorf("%w: %s (size: %d bytes, max allowed: %d bytes)", ErrFileTooLarge, file.Name, file.UncompressedSize64, maxSize)
}
srcFile, err := file.Open()
if err != nil {
return fmt.Errorf("failed to open file in zip: %w", err)
}
defer srcFile.Close()
if err := filePath.MkParentDir(); err != nil {
return err
}
destFile, err := filePath.fs.OpenFile(filePath.absPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
return fmt.Errorf("failed to create destination file: %w", err)
}
defer destFile.Close()
written, err := io.Copy(destFile, io.LimitReader(srcFile, maxSize))
if err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
if written != int64(file.UncompressedSize64) {
return fmt.Errorf("%w: %s (wrote %d of %d bytes)", ErrIncompleteWrite, file.Name, written, file.UncompressedSize64)
}
return nil
}
func (p *FsPath) prepareCompression(fileName, extension string) (*FsPath, afero.File, error) {
if !p.IsDir() {
return nil, nil, fmt.Errorf("%w: %s", ErrNotDirectory, p.absPath)
}
// Ensure the fileName has the correct extension
if !strings.HasSuffix(fileName, extension) {
fileName += extension
}
// Create the path for the compressed file
compressedPath := p.Parent().Join(fileName)
// Create the file
file, err := p.fs.Create(compressedPath.absPath)
if err != nil {
return nil, nil, fmt.Errorf("failed to create %s file: %w", extension, err)
}
return compressedPath, file, nil
}
// Update the type definition for writerFactory
type (
writerFactory func(name string, info os.FileInfo) (io.Writer, error)
)
func (p *FsPath) compressDirectoryToWriter(createWriter writerFactory) (int, error) {
totalFiles := 0
err := p.Walk(func(relPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := p.fs.Open(p.Join(relPath).absPath)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
writer, err := createWriter(relPath, info)
if err != nil {
return fmt.Errorf("failed to create writer for %s: %w", relPath, err)
}
_, err = io.Copy(writer, file)
if err != nil {
return fmt.Errorf("failed to write file %s: %w", relPath, err)
}
totalFiles++
return nil
})
if err != nil {
return 0, fmt.Errorf("failed to compress directory: %w", err)
}
return totalFiles, nil
}