Standard library / archive/zip
archive/zip.OpenReader()
Open a ZIP on disk; Close releases the underlying file. Use a disposable fixture. Expected stdout: 'a.txt\n'.
Formal Syntax & Structure
go syntaxgo
func OpenReader(name string) (*ReadCloser, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Archive filename or slash-separated entry name, depending on operation. |
Return Value
(*ReadCloser, error)
File-backed archive reader and error.
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)) r, e := zip.OpenReader(name) must(e) defer r.Close() fmt.Println(r.File[0].Name)}