Standard library / archive/zip

archive/zip.Writer.Copy()

Copy an existing ZIP entry in raw form, bypassing decompression and validation. Only copy trusted or separately verified entries. Expected stdout: 'a.txt\n'.

Formal Syntax & Structure

go syntaxgo
func (w *Writer) Copy(f *File) error

Parameters & Attributes

NameTypeDescription
f*FileSource ZIP entry to copy without decompression.

Return Value

error

Nil on success, otherwise operation error.

Working Example

Example Snippetgo
package main
import (  "archive/zip"  "bytes"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}func archive() []byte {  var b bytes.Buffer  w := zip.NewWriter(&b)  f, e := w.CreateHeader(&zip.FileHeader{Name: "a.txt", Method: zip.Store})  must(e)  _, e = f.Write([]byte("Go"))  must(e)  must(w.Close())  return b.Bytes()}func reader() *zip.Reader {  b := archive()  r, e := zip.NewReader(bytes.NewReader(b), int64(len(b)))  must(e)  return r}func main() {  var b bytes.Buffer  w := zip.NewWriter(&b)  must(w.Copy(reader().File[0]))  must(w.Close())  r, e := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))  must(e)  fmt.Println(r.File[0].Name)}
Go Reference