This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compression.go
executable file
·283 lines (233 loc) · 6.64 KB
/
compression.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
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
package iop
import (
"archive/zip"
"bufio"
"compress/gzip"
"io"
"os"
"path/filepath"
"strings"
"github.com/flarco/g"
"github.com/klauspost/compress/s2"
"github.com/klauspost/compress/zstd"
)
// Compressor implements differnt kind of compression
type Compressor interface {
Self() Compressor
Compress(io.Reader) io.Reader
Decompress(io.Reader) (io.Reader, error)
Suffix() string
}
// CompressorType is an int type for enum for the Compressor Type
type CompressorType string
const (
// AutoCompressorType is for auto compression
AutoCompressorType CompressorType = "AUTO"
// NoneCompressorType is for no compression
NoneCompressorType CompressorType = "NONE"
// ZipCompressorType is for Zip compression
ZipCompressorType CompressorType = "ZIP"
// GzipCompressorType is for Gzip compression
GzipCompressorType CompressorType = "GZIP"
// SnappyCompressorType is for Snappy compression
SnappyCompressorType CompressorType = "SNAPPY"
// ZStandardCompressorType is for ZStandard
ZStandardCompressorType CompressorType = "ZSTD"
)
// CompressorTypePtr returns a pointer to the CompressorType value passed in.
func CompressorTypePtr(v CompressorType) *CompressorType {
return &v
}
// Unzip will decompress a zip archive, moving all files and folders
// within the zip file (parameter 1) to an output directory (parameter 2).
func Unzip(src string, dest string) ([]string, error) {
var filenames []string
r, err := zip.OpenReader(src)
if err != nil {
return filenames, g.Error(err)
}
defer r.Close()
for _, f := range r.File {
// Store filename/path for returning and using later on
fpath := filepath.Join(dest, f.Name)
g.Trace("unzipping to: " + fpath)
// Check for ZipSlip. More Info: http://bit.ly/2MsjAWE
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
return filenames, g.Error("%s: illegal file path", fpath)
}
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
// Make Folder
os.MkdirAll(fpath, os.ModePerm)
continue
}
// Make File
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return filenames, g.Error(err)
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, g.Error(err)
}
rc, err := f.Open()
if err != nil {
return filenames, g.Error(err)
}
_, err = io.Copy(outFile, rc)
// Close the file without defer to close before next iteration of loop
outFile.Close()
rc.Close()
if err != nil {
return filenames, g.Error(err)
}
}
return filenames, nil
}
func NewCompressor(cpType CompressorType) Compressor {
var compressor Compressor
switch cpType {
// case ZipCompressorType:
case GzipCompressorType:
compressor = &GzipCompressor{cpType: cpType, suffix: ".gz"}
case SnappyCompressorType:
compressor = &SnappyCompressor{cpType: cpType, suffix: ".snappy"}
case ZStandardCompressorType:
compressor = &ZStandardCompressor{cpType: cpType, suffix: ".zst"}
default:
compressor = &NoneCompressor{cpType: NoneCompressorType, suffix: ""}
}
return compressor
}
type NoneCompressor struct {
Compressor
cpType CompressorType
suffix string
}
func (cp *NoneCompressor) Compress(reader io.Reader) io.Reader {
return reader
}
func (cp *NoneCompressor) Decompress(reader io.Reader) (gReader io.Reader, err error) {
return reader, err
}
func (cp *NoneCompressor) Suffix() string {
return cp.suffix
}
type GzipCompressor struct {
Compressor
cpType CompressorType
suffix string
}
// Compress uses gzip to compress
func (cp *GzipCompressor) Compress(reader io.Reader) io.Reader {
pr, pw := io.Pipe()
gw, _ := gzip.NewWriterLevel(pw, gzip.BestSpeed)
go func() {
_, err := io.Copy(gw, reader)
if err != nil {
g.LogError(g.Error(err, "could not compress stream with gzip"))
}
gw.Close()
pw.Close()
}()
return pr
}
// Decompress uses gzip to decompress if it is gzip. Otherwise return same reader
func (cp *GzipCompressor) Decompress(reader io.Reader) (gReader io.Reader, err error) {
gReader, err = gzip.NewReader(reader)
if err != nil {
return reader, g.Error(err, "Error using gzip decompressor")
}
return gReader, nil
}
func (cp *GzipCompressor) Suffix() string {
return cp.suffix
}
type SnappyCompressor struct {
Compressor
cpType CompressorType
suffix string
}
// Compress uses gzip to compress
func (cp *SnappyCompressor) Compress(reader io.Reader) io.Reader {
pr, pw := io.Pipe()
w := s2.NewWriter(pw)
go func() {
_, err := io.Copy(w, reader)
if err != nil {
g.LogError(g.Error(err, "could not compress stream with snappy"))
}
w.Close()
pw.Close()
}()
return pr
}
// Decompress uses gzip to decompress if it is gzip. Otherwise return same reader
func (cp *SnappyCompressor) Decompress(reader io.Reader) (sReader io.Reader, err error) {
bReader := bufio.NewReader(reader)
testBytes, err := bReader.Peek(6)
if err != nil {
return bReader, g.Error(err, "Error Peeking")
}
// https://github.com/google/snappy/blob/master/framing_format.txt
// if string(testBytes) == "sNaPpY" {
_ = testBytes
sReader = s2.NewReader(bReader)
return sReader, err
}
func (cp *SnappyCompressor) Suffix() string {
return cp.suffix
}
type ZStandardCompressor struct {
Compressor
cpType CompressorType
suffix string
}
// Compress uses gzip to compress
func (cp *ZStandardCompressor) Compress(reader io.Reader) io.Reader {
pr, pw := io.Pipe()
w, err := zstd.NewWriter(pw, zstd.WithEncoderLevel(zstd.SpeedFastest))
if err != nil {
g.Warn("Could not compress using ZStandard")
return reader
}
go func() {
_, err := io.Copy(w, reader)
if err != nil {
g.LogError(g.Error(err, "could not compress stream with zstandard"))
}
w.Close()
pw.Close()
}()
return pr
}
// Decompress uses gzip to decompress if it is gzip. Otherwise return same reader
func (cp *ZStandardCompressor) Decompress(reader io.Reader) (sReader io.Reader, err error) {
sReader, err = zstd.NewReader(reader)
if err != nil {
return nil, g.Error(err, "Error decompressing with Zstandard")
}
return sReader, err
}
func (cp *ZStandardCompressor) Suffix() string {
return cp.suffix
}
// AutoDecompress auto detexts compression to decompress. Otherwise return same reader
func AutoDecompress(reader io.Reader) (gReader io.Reader, err error) {
bReader := bufio.NewReader(reader)
testBytes, err := bReader.Peek(2)
if err != nil {
// return bReader, g.Error(err, "Error Peeking")
return bReader, nil
}
// https://stackoverflow.com/a/28332019
if testBytes[0] == 31 && testBytes[1] == 139 {
// is gzip
gReader, err = gzip.NewReader(bReader)
if err != nil {
return bReader, g.Error(err, "Error using gzip.NewReader")
}
} else {
gReader = bReader
}
return gReader, err
}