forked from chen-keinan/go-archive-extractor
-
Notifications
You must be signed in to change notification settings - Fork 8
/
compression.go
215 lines (186 loc) · 4.31 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
package compression
import (
"bytes"
"compress/bzip2"
"compress/flate"
"compress/gzip"
"compress/lzw"
"compress/zlib"
"errors"
"github.com/ulikunitz/xz"
"github.com/ulikunitz/xz/lzma"
"io"
"io/ioutil"
"os"
"path/filepath"
)
const (
bz2Ext = ".bz2"
tbz2Ext = ".tbz2"
gzExt = ".gz"
tgzExt = ".tgz"
lzwExt = ".Z"
inflExt = ".infl"
zlibExt = ".xp3"
xzExt = ".xz"
txzExt = ".txz"
lzmaExt = ".lzma"
tlzmaExt = ".tlzma"
maxMagicBytes = 6 // 6 is the biggest used here (xz)
)
var (
gzipMagic = []byte{0x1F, 0x8B}
bz2Magic = []byte{0x42, 0x5A, 0x68}
xzMagic = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}
lzmaMagic = []byte{0x5D, 0x00, 0x00}
)
func NewReaderSkipBytes(filePath string, skip int64) (io.ReadCloser, error) {
return newReader(&fileArgs{path: filePath, skipBytes: skip})
}
func NewReader(filePath string) (io.ReadCloser, error) {
return newReader(&fileArgs{path: filePath})
}
type fileArgs struct {
path string
skipBytes int64
}
func (fa *fileArgs) open() (*os.File, error) {
f, err := os.Open(fa.path)
if err != nil {
return nil, err
}
if fa.skipBytes > 0 {
_, err := f.Seek(fa.skipBytes, io.SeekStart)
if err != nil {
f.Close()
return nil, err
}
}
return f, nil
}
func newReader(fa *fileArgs) (io.ReadCloser, error) {
ext := filepath.Ext(fa.path)
//these types has no defined magic bytes
switch ext {
case lzwExt:
return initReader(fa, lzwReader)
case inflExt:
return initReader(fa, flateReader)
case zlibExt:
return initReader(fa, zlibReader)
}
// if possible init by magic bytes
if magic, err := getMagicBytes(fa); err == nil {
switch {
case bytes.HasPrefix(magic, bz2Magic):
return initReader(fa, bz2Reader)
case bytes.HasPrefix(magic, gzipMagic):
return initReader(fa, gzipReader)
case bytes.HasPrefix(magic, xzMagic):
return initReader(fa, xzReader)
case bytes.HasPrefix(magic, lzmaMagic):
return initReader(fa, lzmaReader)
}
}
// fallback to init by extension
switch ext {
case bz2Ext, tbz2Ext:
return initReader(fa, bz2Reader)
case gzExt, tgzExt:
return initReader(fa, gzipReader)
case xzExt, txzExt:
return initReader(fa, xzReader)
case lzmaExt, tlzmaExt:
return initReader(fa, lzmaReader)
default:
// no compression format found
return initReader(fa, fileReader)
}
}
func getMagicBytes(fa *fileArgs) ([]byte, error) {
f, err := fa.open()
if err != nil {
return nil, err
}
defer f.Close()
b := make([]byte, maxMagicBytes)
if _, err = f.Read(b); err != nil {
return nil, err
}
return b, nil
}
// compression readers
type cReader struct {
reader io.ReadCloser
file *os.File
}
func (cr *cReader) Read(p []byte) (int, error) {
return cr.reader.Read(p)
}
func (cr *cReader) Close() error {
if err := cr.file.Close(); err != nil {
return err
}
if err := cr.reader.Close(); err != nil {
return err
}
return nil
}
func initReader(fa *fileArgs, getReader func(io.Reader) (io.ReadCloser, error)) (io.ReadCloser, error) {
f, err := fa.open()
if err != nil {
return nil, err
}
r, err := getReader(f)
if err != nil {
f.Close()
return nil, &ErrGetReader{err}
}
return &cReader{reader: r, file: f}, nil
}
type ErrGetReader struct {
err error
}
func (e *ErrGetReader) Error() string {
return e.err.Error()
}
func IsGetReaderError(err error) bool {
for e := err; e != nil; e = errors.Unwrap(e) {
if _, ok := e.(*ErrGetReader); ok {
return true
}
}
return false
}
func bz2Reader(reader io.Reader) (io.ReadCloser, error) {
return ioutil.NopCloser(bzip2.NewReader(reader)), nil
}
func flateReader(reader io.Reader) (io.ReadCloser, error) {
return flate.NewReader(reader), nil
}
func gzipReader(reader io.Reader) (io.ReadCloser, error) {
return gzip.NewReader(reader)
}
func lzwReader(reader io.Reader) (io.ReadCloser, error) {
return lzw.NewReader(reader, lzw.LSB, 100), nil
}
func zlibReader(reader io.Reader) (io.ReadCloser, error) {
return zlib.NewReader(reader)
}
func xzReader(reader io.Reader) (io.ReadCloser, error) {
r, err := xz.NewReader(reader)
if err != nil {
return nil, err
}
return ioutil.NopCloser(r), nil
}
func lzmaReader(reader io.Reader) (io.ReadCloser, error) {
r, err := lzma.NewReader(reader)
if err != nil {
return nil, err
}
return ioutil.NopCloser(r), nil
}
func fileReader(reader io.Reader) (io.ReadCloser, error) {
return ioutil.NopCloser(reader), nil
}