-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.go
84 lines (76 loc) · 2.36 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
// Apla Software includes an integrated development
// environment with a multi-level system for the management
// of access rights to data, interfaces, and Smart contracts. The
// technical characteristics of the Apla Software are indicated in
// Apla Technical Paper.
// Apla Users are granted a permission to deal in the Apla
// Software without restrictions, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of Apla Software, and to permit persons
// to whom Apla Software is furnished to do so, subject to the
// following conditions:
// * the copyright notice of GenesisKernel and EGAAS S.A.
// and this permission notice shall be included in all copies or
// substantial portions of the software;
// * a result of the dealing in Apla Software cannot be
// implemented outside of the Apla Platform environment.
// THE APLA SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY
// OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, ERROR FREE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
// THE USE OR OTHER DEALINGS IN THE APLA SOFTWARE.
package main
import (
"bytes"
"compress/gzip"
"io"
"time"
"github.com/google/logger"
)
const (
COMPRESS_NONE = iota
COMPRESS_GZIP
)
func Compression(in []byte) []byte {
var buf bytes.Buffer
switch cfg.Settings.Compression {
case COMPRESS_GZIP:
zw := gzip.NewWriter(&buf)
zw.Name = "block"
zw.ModTime = time.Now()
_, err := zw.Write(in)
if err != nil {
logger.Fatal(err)
}
if err := zw.Close(); err != nil {
logger.Fatal(err)
}
default:
logger.Fatal(`Unknown compression: `, cfg.Settings.Compression)
}
return buf.Bytes()
}
func Decompression(method int64, in []byte) []byte {
var bufout bytes.Buffer
buf := bytes.NewBuffer(in)
switch method {
case COMPRESS_GZIP:
zr, err := gzip.NewReader(buf)
if err != nil {
logger.Fatal(err)
}
if _, err := io.Copy(&bufout, zr); err != nil {
logger.Fatal(err)
}
if err := zr.Close(); err != nil {
logger.Fatal(err)
}
default:
logger.Fatal(`Unknown compression: `, method)
}
return bufout.Bytes()
}