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 ?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With