如何使用“ compress / gzip”包gzip文件? [英] How can I use the "compress/gzip" package to gzip a file?

查看:66
本文介绍了如何使用“ compress / gzip”包gzip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Go语言的新手,无法弄清楚如何使用 compress / gzip 软件包对我有利。基本上,我只想写一些东西到文件中,将其gzip压缩,然后通过另一个脚本直接从压缩格式中读取。如果有人可以给我一个有关如何执行此操作的示例,我将不胜感激。

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.

推荐答案

所有compress包都实现相同的接口。您可以使用类似的方法进行压缩:

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()

这篇关于如何使用“ compress / gzip”包gzip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆