问题描述
我是golang的新手,不知道如何使用golang的compress / gzip包来获得我的优势。基本上,我只是想写一个文件,gzip它,并直接从压缩格式通过另一个脚本读取它。
I'm new to golang, and can't figure out how to use golang's "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.
推荐答案
所有的压缩包都实现了相同的接口。你将使用这样的东西来压缩:
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()
并解压缩:
r, err := gzip.NewReader(&b)
io.Copy(os.Stdout, r)
r.Close()