-
Notifications
You must be signed in to change notification settings - Fork 2
/
extractor.go
156 lines (126 loc) · 4.06 KB
/
extractor.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
package pzip
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/klauspost/compress/zip"
"github.com/ybirader/pzip/pool"
)
type extractor struct {
outputDir string
archiveReader *zip.ReadCloser
fileWorkerPool pool.WorkerPool[zip.File]
concurrency int
}
// NewExtractor returns a new pzip extractor. The extractor can be configured by passing in a number of options.
// Available options include ExtractorConcurrency(n int). It returns an error if the extractor can't be created
// Close() should be called on the returned extractor when done
func NewExtractor(outputDir string, options ...extractorOption) (*extractor, error) {
absOutputDir, err := filepath.Abs(outputDir)
if err != nil {
return nil, fmt.Errorf("absolute path %q: %w", outputDir, err)
}
e := &extractor{outputDir: absOutputDir, concurrency: runtime.GOMAXPROCS(0)}
fileExecutor := func(file *zip.File) error {
if err := e.extractFile(file); err != nil {
return fmt.Errorf("extract file %q: %w", file.Name, err)
}
return nil
}
fileWorkerPool, err := pool.NewFileWorkerPool(fileExecutor, &pool.Config{Concurrency: e.concurrency, Capacity: 10})
if err != nil {
return nil, fmt.Errorf("new file worker pool: %w", err)
}
e.fileWorkerPool = fileWorkerPool
for _, option := range options {
if err = option(e); err != nil {
return nil, err
}
}
return e, nil
}
// Extract extracts the files from the specified archivePath to
// the corresponding outputDir registered with the extractor. Extraction is canceled when the
// associated ctx is canceled. The first error that arises during extraction is returned.
func (e *extractor) Extract(ctx context.Context, archivePath string) (err error) {
e.archiveReader, err = zip.OpenReader(archivePath)
if err != nil {
return fmt.Errorf("open archive %q: %w", archivePath, err)
}
e.fileWorkerPool.Start(ctx)
for _, file := range e.archiveReader.File {
e.fileWorkerPool.Enqueue(file)
}
if err = e.fileWorkerPool.Close(); err != nil {
return fmt.Errorf("close file worker pool: %w", err)
}
return nil
}
func (e *extractor) Close() error {
if err := e.archiveReader.Close(); err != nil {
return fmt.Errorf("close archive reader: %w", err)
}
return nil
}
func (e *extractor) extractFile(file *zip.File) (err error) {
outputPath := e.outputPath(file.Name)
dir := filepath.Dir(outputPath)
if err = os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("create directory %q: %w", dir, err)
}
if e.isDir(file.Name) {
if err = e.writeDir(outputPath, file); err != nil {
return fmt.Errorf("write directory %q: %w", file.Name, err)
}
return nil
}
if err = e.writeFile(outputPath, file); err != nil {
return fmt.Errorf("write file %q: %w", file.Name, err)
}
return nil
}
func (e *extractor) writeDir(outputPath string, file *zip.File) error {
err := os.Mkdir(outputPath, file.Mode())
if os.IsExist(err) {
if err = os.Chmod(outputPath, file.Mode()); err != nil {
return fmt.Errorf("chmod directory %q: %w", outputPath, err)
}
} else if err != nil {
return fmt.Errorf("create directory %q: %w", outputPath, err)
}
return nil
}
func (e *extractor) writeFile(outputPath string, file *zip.File) (err error) {
outputFile, err := os.OpenFile(outputPath, os.O_CREATE|os.O_WRONLY, file.Mode())
if err != nil {
return fmt.Errorf("create file %q: %w", outputPath, err)
}
defer func() {
if cerr := outputFile.Close(); cerr != nil && err == nil {
err = fmt.Errorf("close output file %q: %w", outputPath, cerr)
}
}()
srcFile, err := file.Open()
if err != nil {
return fmt.Errorf("open file %q: %w", file.Name, err)
}
defer func() {
if cerr := srcFile.Close(); cerr != nil && err == nil {
err = fmt.Errorf("close source file %q: %w", file.Name, cerr)
}
}()
if _, err = io.Copy(outputFile, srcFile); err != nil {
return fmt.Errorf("decompress file %q: %w", file.Name, err)
}
return nil
}
func (e *extractor) isDir(name string) bool {
return strings.HasSuffix(filepath.ToSlash(name), "/")
}
func (e *extractor) outputPath(name string) string {
return filepath.Join(e.outputDir, name)
}