Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble with gzip.Reader in Golang

Tags:

go

Why doesn't this work? (sorry for some reason I cannot get a share button on Go Playground).

package main

import (
    "bytes"
    "compress/gzip"
    "fmt"
    "io"
)

func main() {
    // ENCODE
    data := []byte{1, 2, 3, 4, 5, 6, 7}
    bb0 := bytes.NewBuffer(data)
    byts := bb0.Bytes()
    fmt.Printf("data = % x\n", data)
    fmt.Printf("byte buffer bb0 contains = % x\n", byts)
    bb1 := new(bytes.Buffer)
    w := gzip.NewWriter(bb1)
    s1, err := w.Write(byts)
    fmt.Printf("%d bytes written using gzip writer, err = %v\n", s1, err)
    byts = bb1.Bytes()
    fmt.Printf("byte buffer bb1 contains = % x\n", byts)
    // DECODE
    r, err := gzip.NewReader(bb1)
    bb2 := new(bytes.Buffer)
    s2, err := io.Copy(bb2, r)
    r.Close()
    fmt.Printf("%d bytes copied from gzip reader, err = %v\n", s2, err)
    byts = bb2.Bytes()
    fmt.Printf("byte buffer bb2 contains = % x\n", byts)
}

The output I get

data = 01 02 03 04 05 06 07
byte buffer bb0 contains = 01 02 03 04 05 06 07
7 bytes written using gzip writer, err = <nil>
byte buffer bb1 contains = 1f 8b 08 00 00 09 6e 88 00 ff
0 bytes copied from gzip reader, err = unexpected EOF
byte buffer bb2 contains = 

The reader doesn't seem to be doing anything, what am I doing wrong?

like image 601
rino Avatar asked Jan 08 '23 00:01

rino


1 Answers

Probably it doesn't work because you didn't close the gzip writer and so the gzipped data was never flushed to the underlying writer (for which you are using a bytes.Buffer), or at least it wasn't finalized.

You need to w.Close() the gzip writer after writing.

Alternatively, it could be that the bytes.Buffer needs to be seeked to zero before attempting to read from it, as it might be that the reader is trying to read from the end of it.

Also what you're doing is inefficient, I'd suggest you use: https://github.com/AlasdairF/Custom

like image 181
Alasdair Avatar answered Feb 24 '23 16:02

Alasdair