| 16 | ) |
| 17 | |
| 18 | func ExampleWriter() { |
| 19 | // Create a buffer to write our archive to. |
| 20 | buf := new(bytes.Buffer) |
| 21 | |
| 22 | // Create a new zip archive. |
| 23 | w := zip.NewWriter(buf) |
| 24 | |
| 25 | // Add some files to the archive. |
| 26 | var files = []struct { |
| 27 | Name, Body string |
| 28 | }{ |
| 29 | {"readme.txt", "This archive contains some text files."}, |
| 30 | {"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"}, |
| 31 | {"todo.txt", "Get animal handling licence.\nWrite more examples."}, |
| 32 | } |
| 33 | for _, file := range files { |
| 34 | f, err := w.Create(file.Name) |
| 35 | if err != nil { |
| 36 | log.Fatal(err) |
| 37 | } |
| 38 | _, err = f.Write([]byte(file.Body)) |
| 39 | if err != nil { |
| 40 | log.Fatal(err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Make sure to check the error on Close. |
| 45 | err := w.Close() |
| 46 | if err != nil { |
| 47 | log.Fatal(err) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func ExampleReader() { |
| 52 | // Open a zip archive for reading. |