-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompression.go
81 lines (61 loc) · 2 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
package feedx
import (
"compress/flate"
"compress/gzip"
"io"
"path"
)
// Compression represents the data compression.
type Compression interface {
// NewReader wraps a reader.
NewReader(io.Reader) (io.ReadCloser, error)
// NewWriter wraps a writer.
NewWriter(io.Writer) (io.WriteCloser, error)
}
// DetectCompression detects the compression type from a URL path or file name.
func DetectCompression(name string) Compression {
if name != "" {
ext := path.Ext(path.Base(name))
if ext != "" && ext[0] == '.' && ext[len(ext)-1] == 'z' {
return GZipCompression
} else if ext == ".flate" {
return FlateCompression
}
}
return NoCompression
}
// --------------------------------------------------------------------
// NoCompression is just a pass-through without compression.
var NoCompression = noCompression{}
type noCompression struct{}
func (noCompression) NewReader(r io.Reader) (io.ReadCloser, error) {
return noCompressionWrapper{Reader: r}, nil
}
func (noCompression) NewWriter(w io.Writer) (io.WriteCloser, error) {
return noCompressionWrapper{Writer: w}, nil
}
type noCompressionWrapper struct {
io.Reader
io.Writer
}
func (noCompressionWrapper) Close() error { return nil }
// --------------------------------------------------------------------
// GZipCompression supports gzip compression format.
var GZipCompression = gzipCompression{}
type gzipCompression struct{}
func (gzipCompression) NewReader(r io.Reader) (io.ReadCloser, error) {
return gzip.NewReader(r)
}
func (gzipCompression) NewWriter(w io.Writer) (io.WriteCloser, error) {
return gzip.NewWriter(w), nil
}
// --------------------------------------------------------------------
// FlateCompression supports flate compression format.
var FlateCompression = flateCompression{}
type flateCompression struct{}
func (flateCompression) NewReader(r io.Reader) (io.ReadCloser, error) {
return flate.NewReader(r), nil
}
func (flateCompression) NewWriter(w io.Writer) (io.WriteCloser, error) {
return flate.NewWriter(w, flate.BestSpeed)
}