我试图获取一大块字节并使用archive/zipGo中的包压缩它们.但是,我根本无法理解.有没有关于如何做到的例子,是否有任何关于这个神秘包装的解释?
Ibo*_*lit 14
感谢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)
}
Run Code Online (Sandbox Code Playgroud)
希望对你有帮助 :)
| 归档时间: |
|
| 查看次数: |
3703 次 |
| 最近记录: |