https://github.com/kataras/compress
HTTP Compression for Go
https://github.com/kataras/compress
brotli compression deflate go golang gzip http iris snappy
Last synced: about 1 month ago
JSON representation
HTTP Compression for Go
- Host: GitHub
- URL: https://github.com/kataras/compress
- Owner: kataras
- License: mit
- Created: 2020-07-11T00:39:49.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-09T13:08:13.000Z (over 1 year ago)
- Last Synced: 2025-04-04T03:33:20.517Z (3 months ago)
- Topics: brotli, compression, deflate, go, golang, gzip, http, iris, snappy
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 17
- Watchers: 2
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Compress
[](https://github.com/kataras/compress/actions) [](https://goreportcard.com/report/github.com/kataras/compress) [](https://pkg.go.dev/github.com/kataras/compress)
Fast and easy-to-use compression package for Go applications.
## Installation
The only requirement is the [Go Programming Language](https://go.dev/dl/).
```sh
$ go get github.com/kataras/compress
```## Getting Started
Import the package:
```go
package mainimport "github.com/kataras/compress"
```Wrap a handler to enable writing and reading using the best offered compression:
```go
import "net/http"mux := http.NewServeMux()
// [...]
http.ListenAndServe(":8080", compress.Handler(mux))
```Wrap any `io.Writer` for writing data using compression with `NewWriter`:
```go
import "bytes"
import "encoding/json"buf := new(bytes.Buffer)
w, err := compress.NewWriter(buf, compress.GZIP, -1)
if err != nil {
panic(err)
}json.NewEncoder(w).Encode(payload{Data: "my data"})
w.Close()
```Wrap any `io.Reader` for reading compressed data with `NewReader`:
```go
// Where resp.Body is an io.Reader.
r, err := compress.NewReader(resp.Body, compress.GZIP)
if err != nil {
panic(err)
}
defer r.Close()body, err := ioutil.ReadAll(r)
```To retrieve the underline `http.ResponseWriter` please use `w.(*compress.ResponseWriter).ResponseWriter`.
Example Code:
```go
import "net/http"func handler(w http.ResponseWriter, r *http.Request) {
target := "/your/asset.js"if pusher, ok := w.(*compress.ResponseWriter).ResponseWriter.(http.Pusher); ok {
err := pusher.Push(target, &http.PushOptions{
Header: http.Header{
"Accept-Encoding": r.Header["Accept-Encoding"],
}})
if err != nil {
if err == http.ErrNotSupported {
http.Error(w, "HTTP/2 push not supported", http.StatusHTTPVersionNotSupported)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
}
// [...]
}
```> The `http.CloseNotifier` is obselete by Go authors, please use `Request.Context().Done()` instead.
Supported compression algorithms:
- gzip
- deflate
- brotli
- snappyPlease navigate through [_examples](_examples) directory for more.
## License
This software is licensed under the [MIT License](LICENSE).