Created
December 15, 2017 13:09
-
-
Save lenstr/a856df8a295228b61936f166523f9a8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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