Last active
April 3, 2024 09:30
-
-
Save tomcatzh/cf8040820962e0f8c04700eb3b2f26be to your computer and use it in GitHub Desktop.
Wrap a reader to a gzip compress reader using gzip writer :-P
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func NewGzipReader(source io.Reader) io.Reader { | |
r, w := io.Pipe() | |
go func() { | |
defer w.Close() | |
zip, err := gzip.NewWriterLevel(w, gzip.BestSpeed) | |
defer zip.Close() | |
if err != nil { | |
w.CloseWithError(err) | |
} | |
io.Copy(zip, source) | |
}() | |
return r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better add err handling also around the
io.Copy
: