Home > database >  How can I stream data from a file within a gzip archive so that I can test the header?
How can I stream data from a file within a gzip archive so that I can test the header?

Time:11-19

I am trying to write an extraction tool, in Go, which will decompress/extract several types of archive files. To check the type of file, I am making use of the magic numbers, and have been successful in checking for .gz and .tar archives directly, but for any .tar.gz/.tgz archives, I do not seem to be able to check the tar file within the Gzip bundle.

My current train of thought is that I need to take the output of gzip.NewReader(f) and feed pull the header of its output, but this is not working.

f, err := os.Open(file)
if err != null {
  return fmt.Errorf("problem opening %s", filename)
}
r, _ := gzip.NewReader(f)
h, err := GetHeader(r, l)

Here, GetHeader is...

func GetHeader(r io.Reader, l uint32) (in []byte, err error) {
    fmt.Println("Here C")
    in = make([]byte, l)
    n := 0
    n, err = io.ReadFull(r, in)
    if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
        fmt.Println("Here D")
        return nil, err
    }
    fmt.Println(n)
    in = in[:n]
    return in, nil
}

It is used for both checking the Gzip and Tar archive on their own successfully, and in all, l is set to uint32 3072.

I can only assume that I'm missing something pretty trivial here, but at the minute I'm at a loss.

If there is anyone that can assist me with this, it would be greatly appreciated.

CodePudding user response:

It should be

h, err := GetHeader(r, l)

instead of

h, err := GetHeader(f, l)
  • Related