Standard library / archive/zip
archive/zip.NewWriter()
Create a ZIP writer. Close writes the central directory; underlying output remains open. Expected stdout: 'a.txt\n'.
Formal Syntax & Structure
go syntaxgo
func NewWriter(w io.Writer) *WriterParameters & Attributes
| Name | Type | Description |
|---|---|---|
| w | io.Writer | Destination stream; close the archive writer separately. |
Return Value
*Writer
Writer for constructing an archive.
Working Example
Example Snippetgo
package main
import ( "archive/zip" "bytes" "fmt")
func must(e error) { if e != nil { panic(e) }}
func main() { var b bytes.Buffer w := zip.NewWriter(&b) f, e := w.Create("a.txt") must(e) _, e = f.Write([]byte("Go")) must(e) must(w.Close()) r, e := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len())) must(e) fmt.Println(r.File[0].Name)}