Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid GZIP header

Tags:

http

gzip

go

tar

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 ?

like image 520
Julien Séveno-Piltant Avatar asked Sep 22 '26 08:09

Julien Séveno-Piltant


1 Answers

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
}
like image 165
user7882603 Avatar answered Sep 23 '26 23:09

user7882603



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!