-
Notifications
You must be signed in to change notification settings - Fork 26
/
gzip.go
60 lines (54 loc) · 2.23 KB
/
gzip.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
package core
// (C) Copyright IBM Corp. 2020.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"compress/gzip"
"io"
)
// NewGzipCompressionReader will return an io.Reader instance that will deliver
// the gzip-compressed version of the "uncompressedReader" argument.
// This function was inspired by this github gist:
//
// https://gist.github.com/tomcatzh/cf8040820962e0f8c04700eb3b2f26be
func NewGzipCompressionReader(uncompressedReader io.Reader) (io.Reader, error) {
// Create a pipe whose reader will effectively replace "uncompressedReader"
// to deliver the gzip-compressed byte stream.
pipeReader, pipeWriter := io.Pipe()
go func() {
defer pipeWriter.Close()
// Wrap the pipe's writer with a gzip writer that will
// write the gzip-compressed bytes to the Pipe.
compressedWriter := gzip.NewWriter(pipeWriter)
defer compressedWriter.Close()
// To trigger the operation of the pipe, we'll simply start
// to copy bytes from "uncompressedReader" to "compressedWriter".
// This copy operation will block as needed in order to write bytes
// to the pipe only when the pipe reader is called to retrieve more bytes.
_, err := io.Copy(compressedWriter, uncompressedReader)
if err != nil {
sdkErr := SDKErrorf(err, "", "compression-failed", getComponentInfo())
_ = pipeWriter.CloseWithError(sdkErr)
}
}()
return pipeReader, nil
}
// NewGzipDecompressionReader will return an io.Reader instance that will deliver
// the gzip-decompressed version of the "compressedReader" argument.
func NewGzipDecompressionReader(compressedReader io.Reader) (io.Reader, error) {
res, err := gzip.NewReader(compressedReader)
if err != nil {
err = SDKErrorf(err, "", "decompress-read-error", getComponentInfo())
}
return res, err
}