1
// Copyright 2012 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
18
func ExampleWriter() {
19
// Create a buffer to write our archive to.
20
buf := new(bytes.Buffer)
22
// Create a new zip archive.
23
w := zip.NewWriter(buf)
25
// Add some files to the archive.
26
var files = []struct {
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."},
33
for _, file := range files {
34
f, err := w.Create(file.Name)
38
_, err = f.Write([]byte(file.Body))
44
// Make sure to check the error on Close.
51
func ExampleReader() {
52
// Open a zip archive for reading.
53
r, err := zip.OpenReader("testdata/readme.zip")
59
// Iterate through the files in the archive,
60
// printing some of their contents.
61
for _, f := range r.File {
62
fmt.Printf("Contents of %s:\n", f.Name)
67
_, err = io.CopyN(os.Stdout, rc, 68)
75
// Contents of README:
76
// This is the source code repository for the Go programming language.
79
func ExampleWriter_RegisterCompressor() {
80
// Override the default Deflate compressor with a higher compression
83
// Create a buffer to write our archive to.
84
buf := new(bytes.Buffer)
86
// Create a new zip archive.
87
w := zip.NewWriter(buf)
91
// Register the deflator.
92
w.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
95
// Creating a flate compressor for every file is
96
// expensive, create one and reuse it.
97
fw, err = flate.NewWriter(out, flate.BestCompression)
104
// Proceed to add files to w.