无效的GZIP头部

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

Invalid GZIP header

问题

我是新手使用Golang,所以这可能是一些很明显的东西,但我在Stackoverflow或Gzip文档中没有找到任何有效的解决方法。

我通过Http下载了一个.gz文件,并将响应体的内容写入文件。然而,当我尝试从文件中读取并解压缩时,出现了"invalid header"错误。

以下是我的代码:

reader, err := os.Open(completeName)
if err != nil {
	panic(err)
}
defer reader.Close()

archive, err := gzip.NewReader(reader)
if err != nil {
	panic(err)
}
defer archive.Close()

target := destDirectory()
writer, err := os.Create(target + completeName)
if err != nil {
	panic(err)
}
defer writer.Close()

_, err = io.Copy(writer, archive)
return err

我认为可能是我接收到的内容无效,但我尝试通过"tar -xjf file.gz"解压缩它,结果完全正常。

有什么想法吗?

英文:

I am new to Golang, so maybe this is something quite evident but I did not find anything that is working on Stackoverflow or on the Gzip documentation.

I download a .gz through Http and write the content of the response body in a file. However, when I try to read it from the file and uncompress it, I got the "invalid header" error.

Here is my code :

reader, err := os.Open(completeName)
if err != nil {
	panic(err)
}
defer reader.Close()

archive, err := gzip.NewReader(reader)
if err != nil {
	panic(err)
}
defer archive.Close()

target := destDirectory()
writer, err := os.Create(target + completeName)
if err != nil {
	panic(err)
}
defer writer.Close()

_, err = io.Copy(writer, archive)
return err

I though it would be that the content I receive is invalid but I tried to uncompress it via "tar -xjf file.gz" and it worked perfecly.

Any ideas ?

答案1

得分: 4

你需要使用zlib而不是gzip:

func readGzip(content []byte) error {
    var buf *bytes.Buffer = bytes.NewBuffer(content)

    gRead, err := zlib.NewReader(buf)
    if err != nil {
        return err
    }
    
    if t, err := io.Copy(os.Stdout, gRead); err != nil {
        fmt.Println(t)
        return err
    }

    if err := gRead.Close(); err != nil {
        return err
    }
    return nil
}

这段代码使用zlib库来读取gzip压缩的内容。

英文:

you need zlib rather than gzip:

func readGzip(content []byte) error {
	var buf *bytes.Buffer = bytes.NewBuffer(content)

	gRead, err := zlib.NewReader(buf)
	if err != nil {
		return err
	}
	
	if t, err := io.Copy(os.Stdout, gRead); err != nil {
		fmt.Println(t)
		return err
	}

	if err := gRead.Close(); err != nil {
		return err
	}
	return nil
}

答案2

得分: -1

在服务器端,如果使用以下代码发送响应,响应体将包含头信息。这将导致无效的头错误。

应该使用ServeContent()函数发送文件。

streamPDFbytes, _ := os.Create("./data/Matt Aimonettirepo.go")
b := bytes.NewBuffer(streamPDFbytes)

// 直接流式传输到客户端(浏览器)
w.Header().Set("Content-type", "application/octet-stream")
w.Header().Set("Content-Encoding", "gzip")

if _, err := b.WriteTo(w); err != nil { // <----- 这里!
    fmt.Fprintf(w, "%s", err)
}
英文:

In the Server: if use below code sends response, the response body will include header information. It will lead to invalid header error.

Should use ServeContent() function to send file.

streamPDFbytes, _ := os.Create(&quot;./data/Matt Aimonettirepo.go&quot;)
b := bytes.NewBuffer(streamPDFbytes)

// stream straight to client(browser)
w.Header().Set(&quot;Content-type&quot;, &quot;application/octet-stream&quot;)
w.Header().Set(&quot;Content-Encoding&quot;, &quot;gzip&quot;)

if _, err := b.WriteTo(w); err != nil { // &lt;----- here!
	fmt.Fprintf(w, &quot;%s&quot;, err)
}

huangapple
  • 本文由 发表于 2017年4月18日 04:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/43459174.html
匿名

发表评论

匿名网友

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

确定