-
Notifications
You must be signed in to change notification settings - Fork 11
/
decompress.go
235 lines (188 loc) · 6.33 KB
/
decompress.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
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
package xtractr
import (
"compress/bzip2"
"compress/gzip"
"fmt"
"os"
"github.com/andybalholm/brotli"
"github.com/klauspost/compress/s2"
"github.com/klauspost/compress/snappy"
"github.com/klauspost/compress/zlib"
"github.com/klauspost/compress/zstd"
"github.com/pierrec/lz4/v4"
lzw "github.com/sshaman1101/dcompress"
"github.com/therootcompany/xz"
)
// ExtractXZ extracts an XZ-compressed file. A single file.
func ExtractXZ(xFile *XFile) (int64, []string, error) {
compressedFile, err := os.Open(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer compressedFile.Close()
zipReader, err := xz.NewReader(compressedFile, 0)
if err != nil {
return 0, nil, fmt.Errorf("xz.NewReader: %w", err)
}
// Get the absolute path of the file being written.
wfile := xFile.clean(xFile.FilePath, ".xz")
size, err := writeFile(wfile, zipReader, xFile.FileMode, xFile.DirMode)
if err != nil {
return size, nil, err
}
return size, []string{wfile}, nil
}
// ExtractZlib extracts a zlib-compressed file. A single file.
func ExtractZlib(xFile *XFile) (int64, []string, error) {
compressedFile, err := os.Open(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer compressedFile.Close()
zipReader, err := zlib.NewReader(compressedFile)
if err != nil {
return 0, nil, fmt.Errorf("zlib.NewReader: %w", err)
}
defer zipReader.Close()
// Get the absolute path of the file being written.
wfile := xFile.clean(xFile.FilePath, ".zz", ".zlib")
size, err := writeFile(wfile, zipReader, xFile.FileMode, xFile.DirMode)
if err != nil {
return size, nil, err
}
return size, []string{wfile}, nil
}
// ExtractZstandard extracts a Zstandard-compressed file. A single file.
func ExtractZstandard(xFile *XFile) (int64, []string, error) {
compressedFile, err := os.Open(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer compressedFile.Close()
zipReader, err := zstd.NewReader(compressedFile)
if err != nil {
return 0, nil, fmt.Errorf("zstd.NewReader: %w", err)
}
defer zipReader.Close()
// Get the absolute path of the file being written.
wfile := xFile.clean(xFile.FilePath, ".zstd", ".zst")
size, err := writeFile(wfile, zipReader, xFile.FileMode, xFile.DirMode)
if err != nil {
return size, nil, err
}
return size, []string{wfile}, nil
}
// ExtractLZW extracts an LZW-compressed file. A single file.
func ExtractLZW(xFile *XFile) (int64, []string, error) {
compressedFile, err := os.Open(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer compressedFile.Close()
zipReader, err := lzw.NewReader(compressedFile)
if err != nil {
return 0, nil, fmt.Errorf("lzw.NewReader: %w", err)
}
// Get the absolute path of the file being written.
wfile := xFile.clean(xFile.FilePath, ".Z")
size, err := writeFile(wfile, zipReader, xFile.FileMode, xFile.DirMode)
if err != nil {
return size, nil, err
}
return size, []string{wfile}, nil
}
// ExtractLZ4 extracts an LZ4-compressed file. A single file.
func ExtractLZ4(xFile *XFile) (int64, []string, error) {
compressedFile, err := os.Open(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer compressedFile.Close()
// Get the absolute path of the file being written.
wfile := xFile.clean(xFile.FilePath, ".lz4")
size, err := writeFile(wfile, lz4.NewReader(compressedFile), xFile.FileMode, xFile.DirMode)
if err != nil {
return size, nil, err
}
return size, []string{wfile}, nil
}
// ExtractSnappy extracts a snappy-compressed file. A single file.
func ExtractSnappy(xFile *XFile) (int64, []string, error) {
compressedFile, err := os.Open(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer compressedFile.Close()
// Get the absolute path of the file being written.
wfile := xFile.clean(xFile.FilePath, ".snappy", ".sz")
size, err := writeFile(wfile, snappy.NewReader(compressedFile), xFile.FileMode, xFile.DirMode)
if err != nil {
return size, nil, err
}
return size, []string{wfile}, nil
}
// ExtractS2 extracts a Snappy2-compressed file. A single file.
func ExtractS2(xFile *XFile) (int64, []string, error) {
compressedFile, err := os.Open(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer compressedFile.Close()
// Get the absolute path of the file being written.
wfile := xFile.clean(xFile.FilePath, ".s2")
size, err := writeFile(wfile, s2.NewReader(compressedFile), xFile.FileMode, xFile.DirMode)
if err != nil {
return size, nil, err
}
return size, []string{wfile}, nil
}
// ExtractBrotli extracts a Brotli-compressed file. A single file.
func ExtractBrotli(xFile *XFile) (int64, []string, error) {
compressedFile, err := os.Open(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer compressedFile.Close()
// Get the absolute path of the file being written.
wfile := xFile.clean(xFile.FilePath, ".brotli", ".br")
size, err := writeFile(wfile, brotli.NewReader(compressedFile), xFile.FileMode, xFile.DirMode)
if err != nil {
return size, nil, err
}
return size, []string{wfile}, nil
}
// ExtractBzip extracts a bzip2-compressed file. That is, a single file.
func ExtractBzip(xFile *XFile) (int64, []string, error) {
compressedFile, err := os.Open(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer compressedFile.Close()
// Get the absolute path of the file being written.
wfile := xFile.clean(xFile.FilePath, ".bz", ".bz2")
size, err := writeFile(wfile, bzip2.NewReader(compressedFile), xFile.FileMode, xFile.DirMode)
if err != nil {
return size, nil, err
}
return size, []string{wfile}, nil
}
// ExtractGzip extracts a gzip-compressed file. That is, a single file.
func ExtractGzip(xFile *XFile) (int64, []string, error) {
compressedFile, err := os.Open(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer compressedFile.Close()
zipReader, err := gzip.NewReader(compressedFile)
if err != nil {
return 0, nil, fmt.Errorf("gzip.NewReader: %w", err)
}
defer zipReader.Close()
// Get the absolute path of the file being written.
wfile := xFile.clean(xFile.FilePath, ".gz")
size, err := writeFile(wfile, zipReader, xFile.FileMode, xFile.DirMode)
if err != nil {
return size, nil, err
}
return size, []string{wfile}, nil
}