我如何使用golang的“compress / gzip”包到gzip一个文件? [英] How can I use golang's "compress/gzip" package to gzip a file?

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

问题描述

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

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

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