Standard library / archive/zip

archive/zip.FileInfoHeader()

Convert filesystem metadata into a ZIP header. Set Name to the intended relative archive path and choose Method explicitly. Expected stdout: 'a.txt 2\n'.

Formal Syntax & Structure

go syntaxgo
func FileInfoHeader(fi fs.FileInfo) (*FileHeader, error)

Parameters & Attributes

NameTypeDescription
fifs.FileInfoMetadata for the source filesystem entry.

Return Value

(*FileHeader, error)

ZIP header or metadata conversion error.

Working Example

Example Snippetgo
package main
import (  "archive/zip"  "fmt"  "io/fs"  "testing/fstest")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  fsys := fstest.MapFS{"a.txt": &fstest.MapFile{Data: []byte("Go"), Mode: 0600}}  fi, e := fs.Stat(fsys, "a.txt")  must(e)  h, e := zip.FileInfoHeader(fi)  must(e)  fmt.Println(h.Name, h.UncompressedSize64)}
Go Reference