Standard library / archive/zip
archive/zip.RegisterCompressor()
Register a process-wide compression factory for a previously unused method ID. Duplicate registration panics; prefer per-writer registration when possible. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func RegisterCompressor(method uint16, comp Compressor)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| method | uint16 | ZIP compression method identifier. |
| comp | Compressor | Factory for a compressing writer. |
Working Example
Example Snippetgo
package main
import ( "archive/zip" "bytes" "compress/flate" "fmt" "io")
func must(e error) { if e != nil { panic(e) }}func compressor(w io.Writer) (io.WriteCloser, error) { return flate.NewWriter(w, flate.BestSpeed) }func main() { zip.RegisterCompressor(99, compressor) var b bytes.Buffer w := zip.NewWriter(&b) f, e := w.CreateHeader(&zip.FileHeader{Name: "a", Method: 99}) must(e) _, e = f.Write([]byte("Go")) must(e) must(w.Close()) fmt.Println(b.Len() > 0)}