Standard library / archive/zip

archive/zip.Writer.RegisterCompressor()

Override a compression factory for one writer; use a compatible codec with the declared method ID. Expected stdout: 'a.txt\n'.

Formal Syntax & Structure

go syntaxgo
func (w *Writer) RegisterCompressor(method uint16, comp Compressor)

Parameters & Attributes

NameTypeDescription
methoduint16ZIP compression method identifier.
compCompressorFactory 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() {  var b bytes.Buffer  w := zip.NewWriter(&b)  w.RegisterCompressor(zip.Deflate, compressor)  f, e := w.Create("a.txt")  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].Name)}
Go Reference