gpt4 book ai didi

在 Go 中压缩一个字节数组

转载 作者:IT王子 更新时间:2023-10-29 01:12:31 29 4
gpt4 key购买 nike

我正在尝试获取一大块字节并使用 Go 中的 archive/zip 包压缩它们。然而,我根本看不懂。是否有关于如何完成的示例,是否有关于该神秘包的任何解释?

最佳答案

感谢 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 中压缩一个字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12440387/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com