Standard library / archive/zip
archive/zip.NewReader()
Read a ZIP using random access and the complete archive size. Check errors before using entries. Expected stdout: '1\n'.
Formal Syntax & Structure
go syntaxgo
func NewReader(r io.ReaderAt, size int64) (*Reader, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| r | io.ReaderAt | Input stream or random-access archive reader. |
| size | int64 | Total archive length in bytes. |
Return Value
(*Reader, error)
ZIP reader and error; check both, including insecure-path warnings.
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 main() { b := archive() r, e := zip.NewReader(bytes.NewReader(b), int64(len(b))) must(e) fmt.Println(len(r.File))}