Standard library / archive/zip
archive/zip.Reader.RegisterDecompressor()
Register per-reader decompression factory, overriding global method lookup for that reader. Expected stdout: 'Go\n'.
Formal Syntax & Structure
go syntaxgo
func (r *Reader) RegisterDecompressor(method uint16, dcomp Decompressor)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| method | uint16 | ZIP compression method identifier. |
| dcomp | Decompressor | Factory for a decompressing reader. |
Working Example
Example Snippetgo
package main
import ( "archive/zip" "bytes" "fmt" "io")
func must(e error) { if e != nil { panic(e) }}func archive() []byte { 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()) return b.Bytes()}func reader() *zip.Reader { b := archive() r, e := zip.NewReader(bytes.NewReader(b), int64(len(b))) must(e) return r}func main() { r := reader() r.RegisterDecompressor(zip.Store, func(in io.Reader) io.ReadCloser { return io.NopCloser(in) }) f, e := r.File[0].Open() must(e) p, e := io.ReadAll(f) must(e) must(f.Close()) fmt.Println(string(p))}