I'm new to Go, and can't figure out how to use the compress/gzip
package to my advantage. Basically, I just want to write something to a file, gzip it and read it directly from the zipped format through another script. I would really appreciate if someone could give me an example on how to do this.
The easiest way to enable GZIP compression on your WordPress site is by using a caching or performance optimization plugin. For example, if you host your WordPress site on Apache web server, W3 Total Cache includes an option to enable GZIP compression under its Browser Cache settings panel.
GZIP compression is a data-compressing process through which the size of a file is reduced before it is transferred from the server to the browser. So, a GZIP compressed file is smaller in size when compared to the original, thus the browser renders its contents faster.
Copy all read bytes into the new file and close the file By using gzip. NewWriter() from the compress/gzip package, we create a new gzip writer. Then we can use Write to write the compressed bytes in the file. After finishing, we need to close the file.
All the compress packages implement the same interface. You would use something like this to compress:
var b bytes.Buffer
w := gzip.NewWriter(&b)
w.Write([]byte("hello, world\n"))
w.Close()
And this to unpack:
r, err := gzip.NewReader(&b)
io.Copy(os.Stdout, r)
r.Close()
Pretty much the same answer as Laurent, but with the file io:
import (
"bytes"
"compress/gzip"
"io/ioutil"
)
// ...
var b bytes.Buffer
w := gzip.NewWriter(&b)
w.Write([]byte("hello, world\n"))
w.Close() // You must close this first to flush the bytes to the buffer.
err := ioutil.WriteFile("hello_world.txt.gz", b.Bytes(), 0666)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With