Standard library / archive/zip

archive/zip.Writer.AddFS()

Add files and directories from a filesystem using Deflate for files. Introduced in Go 1.23. Expected stdout: 'a.txt\n'.

Formal Syntax & Structure

go syntaxgo
func (w *Writer) AddFS(fsys fs.FS) error

Parameters & Attributes

NameTypeDescription
fsysfs.FSFilesystem to traverse and add; use a trusted source.

Return Value

error

Nil on success, otherwise operation error.

Working Example

Example Snippetgo
package main
import (  "archive/zip"  "bytes"  "fmt"  "testing/fstest")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  w := zip.NewWriter(&b)  fsys := fstest.MapFS{"a.txt": &fstest.MapFile{Data: []byte("Go"), Mode: 0600}}  must(w.AddFS(fsys))  must(w.Close())  r, e := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))  must(e)  fmt.Println(r.File[0].Name)}
Go Reference