Skip to content

Instantly share code, notes, and snippets.

@xyproto
Created June 24, 2015 10:14
Show Gist options
  • Save xyproto/f4915d7e208771f3adc4 to your computer and use it in GitHub Desktop.
Save xyproto/f4915d7e208771f3adc4 to your computer and use it in GitHub Desktop.
gzip compression/decompression example
package main
import (
"fmt"
"compress/gzip"
"io"
"io/ioutil"
"bytes"
"log"
)
// Write gzipped data to a Writer
func gzipWrite(w io.Writer, data []byte) error {
// Write gzipped data to the client
gw, err := gzip.NewWriterLevel(w, gzip.BestSpeed)
defer gw.Close()
gw.Write(data)
return err
}
// Write gunzipped data to a Writer
func gunzipWrite(w io.Writer, data []byte) error {
// Write gzipped data to the client
gr, err := gzip.NewReader(bytes.NewBuffer(data))
defer gr.Close()
data, err = ioutil.ReadAll(gr)
if err != nil {
return err
}
w.Write(data)
return nil
}
func main() {
s := "some data"
fmt.Println("original:\t", s)
var buf bytes.Buffer
err := gzipWrite(&buf, []byte(s))
if err != nil {
log.Fatal(err)
}
fmt.Println("compressed:\t", buf.String())
var buf2 bytes.Buffer
err = gunzipWrite(&buf2, buf.Bytes())
if err != nil {
log.Fatal(err)
}
fmt.Println("decompressed:\t", buf2.String())
}
@xyproto
Copy link
Author

xyproto commented Aug 4, 2020

Yes, just use io.CopyN instead of io.Copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment