在Golang中遇到了gzip.Reader的问题。

huangapple go评论83阅读模式
英文:

Having trouble with gzip.Reader in Golang

问题

为什么这段代码不起作用?(抱歉,由于某种原因,我无法在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)
}

我得到的输出结果是:

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 = 

读取器似乎没有起作用,我做错了什么?

英文:

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

package main

import (
	&quot;bytes&quot;
	&quot;compress/gzip&quot;
	&quot;fmt&quot;
	&quot;io&quot;
)

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

答案1

得分: 9

可能是因为你没有关闭gzip写入器,所以压缩的数据没有被刷新到底层的写入器(你正在使用一个bytes.Buffer),或者至少没有被完成。

在写入后,你需要使用w.Close()关闭gzip写入器。

另外,可能需要将bytes.Buffer的位置定位到零,然后再尝试从中读取,因为读取器可能试图从其末尾读取。

此外,你的做法效率不高,我建议你使用:https://github.com/AlasdairF/Custom

英文:

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

huangapple
  • 本文由 发表于 2015年12月15日 16:15:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/34284001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定