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())
}
@Shailesh1994
Copy link

If you to compress an xml file which is in the same directory where your program is saved how can we save the gzip file that you created.

@xyproto
Copy link
Author

xyproto commented Jan 30, 2020

@Shailesh1994 Here's one way:

data, err := ioutil.ReadFile("test.xml")
if err != nil {
    log.Fatal(err)
}

file, err := os.Create("test.xml.gz")
if err != nil {
    log.Fatal(err)
}

err = gzipWrite(file, data)
if err != nil {
    log.Fatal(err)
}

@Shailesh1994
Copy link

Shailesh1994 commented Jan 30, 2020 via email

@Shailesh1994
Copy link

Do you have a Read Me file of following code and I want to integrate this code for the sitemap .Can you help me out if possible

@xyproto
Copy link
Author

xyproto commented Feb 5, 2020

I don't understand the question. Please try Stack Overflow.

@leobrines
Copy link

leobrines commented Feb 20, 2020

Thanks for the examples!!

Maybe it might be better to use io.Copy when you write?

func gunzipWrite(w io.Writer, data []byte) error {
	// Write gzipped data to the client
	gr, err := gzip.NewReader(bytes.NewBuffer(data))
	defer gr.Close()

	_, err := io.Copy(w, gr)
        return err
}

@xyproto
Copy link
Author

xyproto commented Feb 20, 2020

Yes, io.Copy is probably better.

@Tanmay451
Copy link

can we read gz file in chunks?

@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