Exemplo n.º 1
0
// WriteFile writes a complete file to the writer.
func WriteFile(w *zip.Writer, filename string, data []byte) error {
	if fw, err := w.Create(filename); err != nil {
		return err
	} else if _, err := fw.Write(data); err != nil {
		return err
	}
	addExistingFile(w, filename, filename, 0, 0, 0)
	return nil
}
Exemplo n.º 2
0
// Writes a file to the new writer.
func addFile(w *zip.Writer, fh *zip.FileHeader, r io.Reader, crc uint32) error {
	fh.Flags = 0 // we're not writing a data descriptor after the file
	comp := func(w io.Writer) (io.WriteCloser, error) { return nopCloser{w}, nil }
	fw, err := w.CreateHeaderWithCompressor(fh, comp, fixedCrc32{value: crc})
	if err == nil {
		_, err = io.CopyN(fw, r, int64(fh.CompressedSize64))
	}
	return err
}
Exemplo n.º 3
0
// WriteDir writes a directory entry to the writer.
func WriteDir(w *zip.Writer, filename string) error {
	filename += "/" // Must have trailing slash to tell it it's a directory.
	fh := zip.FileHeader{
		Name:   filename,
		Method: zip.Store,
	}
	if _, err := w.CreateHeader(&fh); err != nil {
		return err
	}
	addExistingFile(w, filename, filename, 0, 0, 0)
	return nil
}