Standard library / archive/zip

archive/zip.ReadCloser

File-backed ZIP reader embedding Reader. Its Close releases the archive file. Expected stdout: '1\n'.

Formal Syntax & Structure

go syntaxgo
type ReadCloser struct {  Reader  // Has unexported fields.}

Working Example

Example Snippetgo
package main
import (  "archive/zip"  "bytes"  "fmt"  "os")
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 main() {  dir, e := os.MkdirTemp("", "zip-example-")  must(e)  defer os.RemoveAll(dir)  name := dir + "/a.zip"  must(os.WriteFile(name, archive(), 0600))  var r *zip.ReadCloser  r, e = zip.OpenReader(name)  must(e)  fmt.Println(len(r.File))  must(r.Close())}
Go Reference