Standard library / archive/zip
archive/zip.Writer.CreateHeader()
Create an entry with explicit metadata and method. Writer takes ownership of header and may mutate it. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| fh | *FileHeader | ZIP entry metadata; writer takes ownership and may modify it. |
Return Value
(io.Writer, error)
Entry writer or error; finish before creating another entry.
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.CreateHeader(&zip.FileHeader{Name: "a.txt", Method: zip.Store}) 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].Method == zip.Store)}