Standard library / archive/zip

archive/zip.File.Open()

Read decompressed entry content and check integrity when read to EOF. Close the entry reader after use. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func (f *File) Open() (io.ReadCloser, error)

Return Value

(io.ReadCloser, error)

Entry stream and error; read through EOF to verify CRC.

Working Example

Example Snippetgo
package main
import (  "archive/zip"  "bytes"  "fmt"  "io")
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() {  f, e := reader().File[0].Open()  must(e)  p, e := io.ReadAll(f)  must(e)  must(f.Close())  fmt.Println(string(p))}
Go Reference