I am trying to take a chunk of bytes and compress them using the archive/zip
package in Go. However, I can't understand it at all. Are there any examples of how it could be done and is there any explanation of that cryptic package?
在Go中压缩一个字节数组
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- duanjihe5180 2012-09-16 11:28关注
Thanks to jamessan I did find the example (which doesn't exactly catch your eye).
Here is what I come up with as the result:
func (this *Zipnik) zipData() { // Create a buffer to write our archive to. fmt.Println("we are in the zipData function") buf := new(bytes.Buffer) // Create a new zip archive. zipWriter := zip.NewWriter(buf) // Add some files to the archive. var files = []struct { Name, Body string }{ {"readme.txt", "This archive contains some text files."}, {"gopher.txt", "Gopher names: George Geoffrey Gonzo"}, {"todo.txt", "Get animal handling licence. Write more examples."}, } for _, file := range files { zipFile, err := zipWriter.Create(file.Name) if err != nil { fmt.Println(err) } _, err = zipFile.Write([]byte(file.Body)) if err != nil { fmt.Println(err) } } // Make sure to check the error on Close. err := zipWriter.Close() if err != nil { fmt.Println(err) } //write the zipped file to the disk ioutil.WriteFile("Hello.zip", buf.Bytes(), 0777) }
I hope you find it useful :)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 求帮我调试一下freefem代码
- ¥15 R语言Rstudio突然无法启动
- ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
- ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
- ¥15 用windows做服务的同志有吗
- ¥60 求一个简单的网页(标签-安全|关键词-上传)
- ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
- ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
- ¥100 为什么这个恒流源电路不能恒流?
- ¥15 有偿求跨组件数据流路径图