Skip to content

Instantly share code, notes, and snippets.

@lenstr
Created December 15, 2017 13:10
Show Gist options
  • Save lenstr/7eaa147865d9ceafb84aa8b62fe3195b to your computer and use it in GitHub Desktop.
Save lenstr/7eaa147865d9ceafb84aa8b62fe3195b to your computer and use it in GitHub Desktop.
package main
import (
"archive/zip"
"bytes"
"io/ioutil"
"log"
)
func main() {
ExampleWriter()
}
func ExampleWriter() {
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
// Create a new zip archive.
w := zip.NewWriter(buf)
// Add some files to the archive.
var files = []struct {
Name, Body string
}{
{"deploy/readme.txt", "This archive contains some text files."},
{"deploy/gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
{"deploy/todo.txt", "Get animal handling licence.\nWrite more examples."},
}
for _, file := range files {
f, err := w.Create(file.Name)
if err != nil {
log.Fatal(err)
}
_, err = f.Write([]byte(file.Body))
if err != nil {
log.Fatal(err)
}
}
// Make sure to check the error on Close.
err := w.Close()
if err != nil {
log.Fatal(err)
}
ioutil.WriteFile("./data.zip", buf.Bytes(), 0644)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment