-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgzip.go
More file actions
154 lines (144 loc) · 4.54 KB
/
gzip.go
File metadata and controls
154 lines (144 loc) · 4.54 KB
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
package compressor
import (
"bytes"
"compress/gzip"
"encoding/base64"
)
// TODO: Adicionar exemplo
// ToGzip converts any given value into a byte array using GZIP compression. It first
// converts the given value into a byte array using the toBytes function and then applies
// compression using gzip. If the conversion to byte array fails, it will cause a panic.
//
// Parameters:
// - a: Any value to be converted into a GZIP compressed byte array.
//
// Returns:
// - []byte: The GZIP compressed byte array representation of the given value.
//
// Panic:
// - If the conversion to byte array fails, it will cause a panic.
//
// Example:
//
// var x string = "Hello, world!"
// var y int = 12345
// fmt.Println(ToGzip(x)) // returns gzip compressed byte array of the string
// fmt.Println(ToGzip(y)) // returns gzip compressed byte array of the integer
//
// Note:
//
// This function is a variation of ToGzipWithErr where it suppresses any occurring error by causing a panic.
func ToGzip(a any) []byte {
bs, err := ToGzipWithErr(a)
if err != nil {
panic(err)
}
return bs
}
// ToGzipWithErr converts any given value into a GZIP compressed byte array. It first
// converts the given value into a byte array using the toBytes function and then applies
// GZIP compression. Any errors that occur during the conversion or compression are
// returned alongside the byte array.
//
// Parameters:
// - a: Any value to be converted into a GZIP compressed byte array.
//
// Returns:
// - []byte: The GZIP compressed byte array representation of the given value.
// - error: Any errors that might occur during the conversion or compression.
//
// Example:
//
// var x string = "Hello, world!"
// var y map[string]int = map[string]int{"one": 1, "two": 2}
// bs, err := ToGzipWithErr(x)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(bs) // Prints the gzip compressed byte array of the string
//
// bs, err = ToGzipWithErr(y)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(bs) // Prints the gzip compressed byte array of the map
func ToGzipWithErr(a any) ([]byte, error) {
bs, err := toBytes(a)
if err != nil {
return nil, err
}
var gzipBuffer bytes.Buffer
gz := gzip.NewWriter(&gzipBuffer)
_, err = gz.Write(bs)
if err != nil {
return nil, err
}
err = gz.Close()
if err != nil {
return nil, err
}
return gzipBuffer.Bytes(), nil
}
// ToGzipBase64 converts any given value into a GZIP compressed Base64 string. It first
// compresses the given value into a GZIP byte array using the ToGzipBase64WithErr function
// and then converts it to a Base64 string. If any error occurs during the conversion
// or compression, it would panic and throw the error.
//
// Parameters:
// - a: Any value to be converted into a GZIP compressed Base64 string.
//
// Returns:
// - string: The GZIP compressed Base64 string representation of the given value.
//
// Panics:
// - error: Any errors that might occur during the conversion or compression.
//
// Example:
//
// var x string = "Hello, world!"
// b64 := ToGzipBase64(x)
// fmt.Println(b64) // Prints the gzip compressed Base64 string of the string
//
// var y map[string]int = map[string]int{"one": 1, "two": 2}
// b64 = ToGzipBase64(y)
// fmt.Println(b64) // Prints the gzip compressed Base64 string of the map
func ToGzipBase64(a any) string {
b64, err := ToGzipBase64WithErr(a)
if err != nil {
panic(err)
}
return b64
}
// ToGzipBase64WithErr converts any given value into a GZIP compressed Base64 string. It first compresses the given
// value into a GZIP byte array using the ToGzipWithErr function and then converts it into a Base64 string. Any errors
// that occur during the conversion or compression are returned alongside with the Base64 string.
//
// Parameters:
// - a: Any value to be converted into a GZIP compressed Base64 string.
//
// Returns:
// - string: The GZIP compressed Base64 string representation of the given value.
// - error: Any errors that occur during the conversion or compression.
//
// Example:
//
// var x string = "Hello, world!"
// var y map[string]int = map[string]int{"one": 1, "two": 2}
// b64, err := ToGzipBase64WithErr(x)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(b64) // Prints the gzip compressed Base64 string of the string
//
// b64, err = ToGzipBase64WithErr(y)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(b64) // Prints the gzip compressed Base64 string of the map
func ToGzipBase64WithErr(a any) (string, error) {
bs, err := ToGzipWithErr(a)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(bs), nil
}