Пример #1
0
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
	}{
		{"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 {
		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)
	}
}
Пример #2
0
func ExampleWriter_RegisterCompressor() {
	// Override the default Deflate compressor with a higher compression
	// level.

	// Create a buffer to write our archive to.
	buf := new(bytes.Buffer)

	// Create a new zip archive.
	w := zip.NewWriter(buf)

	var fw *flate.Writer

	// Register the deflator.
	w.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
		var err error
		if fw == nil {
			// Creating a flate compressor for every file is
			// expensive, create one and reuse it.
			fw, err = flate.NewWriter(out, flate.BestCompression)
		} else {
			fw.Reset(out)
		}
		return fw, err
	})

	// Proceed to add files to w.
}
Пример #3
0
// Zip creates a .zip file in the location zipPath containing
// the contents of files listed in filePaths.
func Zip(zipPath string, filePaths []string) error {
	out, err := os.Create(zipPath)
	if err != nil {
		return err
	}
	defer out.Close()

	w := zip.NewWriter(out)
	for _, fpath := range filePaths {
		infile, err := os.Open(fpath)
		if err != nil {
			return err
		}

		outfile, err := w.Create(filepath.Base(fpath))
		if err != nil {
			w.Close()
			infile.Close()
			return err
		}

		_, err = io.Copy(outfile, infile)
		if err != nil {
			w.Close()
			infile.Close()
			return err
		}

		infile.Close()
	}

	return w.Close()
}
Пример #4
0
func makeZip(source, target string, omitRootFolder bool) {
	zipfile, err := os.Create(target)
	check(err)
	defer zipfile.Close()

	archive := zip.NewWriter(zipfile)
	defer archive.Close()

	info, err := os.Stat(source)
	check(err)

	var baseDir string
	if info.IsDir() {
		baseDir = filepath.Base(source)
	}

	filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
		check(err)

		header, err := zip.FileInfoHeader(info)
		check(err)

		if baseDir != "" {
			if omitRootFolder {
				header.Name = strings.TrimPrefix(path, source+"/")
			} else {
				header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
			}
		}

		if info.IsDir() {
			header.Name += "/"
		} else {
			header.Method = zip.Deflate
		}

		writer, err := archive.CreateHeader(header)
		check(err)

		if info.IsDir() {
			return nil
		}

		file, err := os.Open(path)
		check(err)
		defer file.Close()
		_, err = io.Copy(writer, file)
		return err
	})
}