Standard library / archive/zip

archive/zip.Reader.Open()

Open a slash-separated ZIP entry via fs.FS. Path uses fs.ValidPath conventions. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func (r *Reader) Open(name string) (fs.File, error)

Parameters & Attributes

NameTypeDescription
namestringArchive filename or slash-separated entry name, depending on operation.

Return Value

(fs.File, error)

Opened filesystem entry or error.

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() {  r := reader()  f, e := r.Open("a.txt")  must(e)  p, e := io.ReadAll(f)  must(e)  must(f.Close())  fmt.Println(string(p))}
Go Reference