I'm trying, just for fun, to connect gzip Writer directly to gzip Reader so that I can write in Writer and read from Reader on the fly. I expected to read exactly what I wrote. I use gzip, but I would like to use this method also with crypto / aes, I suppose it should work very similar, and it can be used with other readers / writers like jpeg, png ...
This is my best option that doesn't work, but I hope you can see what I mean: http://play.golang.org/p/7qdUi9wwG7
package main
import (
"bytes"
"compress/gzip"
"fmt"
)
func main() {
s := []byte("Hello world!")
fmt.Printf("%s\n", s)
var b bytes.Buffer
gz := gzip.NewWriter(&b)
ungz, err := gzip.NewReader(&b)
fmt.Println("err: ", err)
gz.Write(s)
gz.Flush()
uncomp := make([]byte, 100)
n, err2 := ungz.Read(uncomp)
fmt.Println("err2: ", err2)
fmt.Println("n: ", n)
uncomp = uncomp[:n]
fmt.Printf("%s\n", uncomp)
}
It seems to be gzip.NewReader(&b)trying to read right away and EOF is returning.
source
share