在 Go 中压缩一个字节数组
时间:2023-04-30
浏览:305
作者:佚名
有志者,事竟成!如果你在学习Golang,那么本文《在 Go 中压缩一个字节数组》,就很适合你!文章讲解的知识点主要包括golang,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
问题内容
我正在尝试获取一大块字节并使用archive/zipGo
中的包压缩它们。但是,我完全无法理解。有没有什么例子可以说明它是如何完成的,有没有关于那个神秘包的解释?
正确答案
感谢jamessan,我确实找到了这个例子(它并没有引起你的注意)。
这是我想出的结果:
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:\nGeorge\nGeoffrey\nGonzo"},
{"todo.txt", "Get animal handling licence.\nWrite 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)
}
希望对你有帮助 :)
到这里,我们也就讲完了《在 Go 中压缩一个字节数组》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于golang的知识点!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
12.01
12.01
12.01
12.01
热门专题
