Standard library / archive/zip

archive/zip.Writer.CreateRaw()

Create an entry with already-encoded bytes. Caller is responsible for matching CRC, sizes and compression method; use normal CreateHeader unless raw control is necessary. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func (w *Writer) CreateRaw(fh *FileHeader) (io.Writer, error)

Parameters & Attributes

NameTypeDescription
fh*FileHeaderZIP 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"  "hash/crc32"  "io")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  w := zip.NewWriter(&b)  p := []byte("Go")  h := &zip.FileHeader{Name: "a.txt", Method: zip.Store, CRC32: crc32.ChecksumIEEE(p), CompressedSize64: 2, UncompressedSize64: 2}  f, e := w.CreateRaw(h)  must(e)  _, e = f.Write(p)  must(e)  must(w.Close())  r, e := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))  must(e)  s, e := r.File[0].Open()  must(e)  q, e := io.ReadAll(s)  must(e)  must(s.Close())  fmt.Println(string(q))}
Go Reference