英文:
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("./data/Matt Aimonettirepo.go")
b := bytes.NewBuffer(streamPDFbytes)
// stream straight to client(browser)
w.Header().Set("Content-type", "application/octet-stream")
w.Header().Set("Content-Encoding", "gzip")
if _, err := b.WriteTo(w); err != nil { // <----- here!
fmt.Fprintf(w, "%s", err)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论