archive/zip
Goのパッケージを使用して、バイトのチャンクを取得して圧縮しようとしています。しかし、全然理解できません。それがどのように行われるかについての例はありますか、そしてその不可解なパッケージの説明はありますか?
8588 次
1 に答える
25
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:\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)
}
I hope you find it useful :)
于 2012-09-16T11:28:40.100 に答える