-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompress.go
161 lines (126 loc) · 3.12 KB
/
compress.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
package nbt
/*
nbt
Copyright (c) 2018 beito
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/
import (
"bytes"
"compress/gzip"
"compress/zlib"
"io"
"io/ioutil"
"os"
"github.com/beito123/binary"
)
// DefaultCompressionLevel set the default compression level when it's compressing
const DefaultCompressionLevel = -1
// CompressType is a compression type
type CompressType int
// DefaultCompression returns the default compression level
func (ct CompressType) DefaultCompression() int {
switch ct {
case CompressGZip:
return gzip.DefaultCompression
case CompressZlib:
return zlib.DefaultCompression
}
return 0
}
const (
// CompressGZip compresses with gzip
CompressGZip CompressType = iota
// CompressZlib compresses with zlib
CompressZlib
)
// Compression Detector
var gzipHeader = []byte{0x1f, 0x8b, 0x08}
var zlibHeader = []byte{0x78}
func hasGZipHeader(b []byte) bool {
return bytes.HasPrefix(b, gzipHeader)
}
func hasZlibHeader(b []byte) bool {
return bytes.HasPrefix(b, zlibHeader)
}
//
// FromFile returns new stream from file
// If the bytes is compressed, it will uncompresses
func FromFile(path string, order binary.Order) (*Stream, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return FromReader(file, order)
}
// FromReader returns new stream from Reader
// If the bytes is compressed, it will uncompresses
func FromReader(reader io.Reader, order binary.Order) (*Stream, error) {
b, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
return FromBytes(b, order)
}
// FromBytes returns new stream with bytes
// If the bytes is compressed, it will uncompresses
func FromBytes(b []byte, order binary.Order) (*Stream, error) {
if hasGZipHeader(b) {
read, err := gzip.NewReader(bytes.NewBuffer(b))
if err != nil {
return nil, err
}
defer read.Close()
b, err = ioutil.ReadAll(read)
if err != nil {
return nil, err
}
} else if hasZlibHeader(b) {
read, err := zlib.NewReader(bytes.NewBuffer(b))
if err != nil {
return nil, err
}
defer read.Close()
b, err = ioutil.ReadAll(read)
if err != nil {
return nil, err
}
}
return NewStreamBytes(order, b), nil
}
// Compress compresses stream's bytes
// You can use compression level in "compress/gzip" and "compress/zlib" for level
// If you set the default compression level, you can set DefaultCompressionLevel
// This often is used for player and level data
func Compress(s *Stream, typ CompressType, level int) ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
if level == DefaultCompressionLevel {
level = typ.DefaultCompression()
}
switch typ {
case CompressGZip:
writer, err := gzip.NewWriterLevel(buf, level)
if err != nil {
return nil, err
}
_, err = writer.Write(s.Bytes())
if err != nil {
return nil, err
}
writer.Flush()
writer.Close()
case CompressZlib:
writer, err := zlib.NewWriterLevel(buf, level)
if err != nil {
return nil, err
}
_, err = writer.Write(s.Bytes())
if err != nil {
return nil, err
}
writer.Flush()
writer.Close()
}
return buf.Bytes(), nil
}