Invalid GZIP header

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 ?

Upvotes: 3

Views: 21043

Answers (2)

danny wang
danny wang

Reputation: 39

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)
}

Upvotes: -1

user7882603
user7882603

Reputation: 41

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
}

Upvotes: 4

Related Questions