简体   繁体   中英

Error on trying to read gzip files in golang

I am trying to read gzip files using compress/gzip. I am using http.DetectContentType as I do not know if I get a normal txt file or a gzipped one. My code is very straight forward and as below:

f, err := os.Open(fullpath)
if err != nil {
    log.Panicf("Can not open file %s: %v", fullpath, err)
    return ""
}
defer f.Close()

buff := make([]byte, 512)
_, err = f.Read(buff)
if err != nil && err != io.EOF{
    log.Panicf("Cannot read buffer %v", err);
    return ""
}

switch filetype := http.DetectContentType(buff); filetype {
case "application/x-gzip":
    log.Println("File Type is", filetype)
    reader, err := gzip.NewReader(f)
    if err != nil && err != io.EOF{
        log.Panicf("Cannot read gzip archive %v", err);
        return ""
    }
    defer reader.Close()
    target := "/xx/yy/abcd.txt"
    writer, err := os.Create(target)
    if err != nil {
        log.Panicf("Cannot write unarchived file %v", err);
        return ""
    }
    defer writer.Close()

    _, err = io.Copy(writer, reader)
    return target

The problem is that the gzip reader always errors out saying "Cannot read gzip archive gzip: invalid header" I have tried the zlib library too but in vain. I gzipped the source file in mac using the command line gzip tool. Please show me where I am going wrong.

You're reading the first 512 bytes of the file, so the gzip.Reader won't ever see that. Since these are regular files, you can seek back to the start after a successful Read :

f.Seek(0, os.SEEK_SET)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM