Standard library / compress/gzip
compress/gzip.Header
Optional GZIP metadata: Name and Comment are Go strings restricted to non-NUL Latin-1 code points on writing, Extra is arbitrary bytes, ModTime stores the modification time, and OS is a platform identifier. Metadata is untrusted and must not be used directly as a filesystem path. Expected stdout: 'demo.txt sample [1 2] 123 3\n'.
Formal Syntax & Structure
go syntaxgo
type Header struct { Comment string Extra []byte ModTime time.Time Name string OS byte}Working Example
Example Snippetgo
package main
import ( "bytes" "compress/gzip" "fmt" "io" "time")
func must(e error) { if e != nil { panic(e) }}func pack(s string) []byte { var b bytes.Buffer w := gzip.NewWriter(&b) _, e := w.Write([]byte(s)) must(e) must(w.Close()) return b.Bytes()}func unpack(b []byte) string { r, e := gzip.NewReader(bytes.NewReader(b)) must(e) p, e := io.ReadAll(r) must(e) must(r.Close()) return string(p)}func main() { var b bytes.Buffer w := gzip.NewWriter(&b) w.Header = gzip.Header{Name: "demo.txt", Comment: "sample", Extra: []byte{1, 2}, ModTime: time.Unix(123, 0), OS: 3} must(w.Close()) r, e := gzip.NewReader(&b) must(e) _, e = io.ReadAll(r) must(e) must(r.Close()) fmt.Println(r.Name, r.Comment, r.Extra, r.ModTime.Unix(), r.OS)}