如何从gzip归档文件中流式传输数据,以便我可以测试头部?

huangapple go评论111阅读模式
英文:

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

问题

我正在尝试编写一个用Go语言编写的提取工具,用于解压/提取多种类型的存档文件。为了检查文件类型,我正在利用魔术数字,并且已经成功地检查了.gz.tar存档文件,但是对于任何.tar.gz/.tgz存档文件,我似乎无法检查Gzip捆绑包中的tar文件。

我目前的思路是,我需要获取gzip.NewReader(f)的输出,并提取其输出的头部,但是这种方法不起作用。

  1. f, err := os.Open(file)
  2. if err != nil {
  3. return fmt.Errorf("problem opening %s", filename)
  4. }
  5. r, _ := gzip.NewReader(f)
  6. h, err := GetHeader(r, l)

这里的GetHeader函数是这样的...

  1. func GetHeader(r io.Reader, l uint32) (in []byte, err error) {
  2. fmt.Println("Here C")
  3. in = make([]byte, l)
  4. n := 0
  5. n, err = io.ReadFull(r, in)
  6. if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
  7. fmt.Println("Here D")
  8. return nil, err
  9. }
  10. fmt.Println(n)
  11. in = in[:n]
  12. return in, nil
  13. }

它用于成功检查Gzip和Tar存档文件本身,并且在所有情况下,l被设置为uint32 3072

我只能假设我在这里漏掉了一些非常微小的东西,但是目前我一头雾水。

如果有人能帮助我解决这个问题,我将非常感激。

英文:

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.

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

Here, GetHeader is...

  1. func GetHeader(r io.Reader, l uint32) (in []byte, err error) {
  2. fmt.Println("Here C")
  3. in = make([]byte, l)
  4. n := 0
  5. n, err = io.ReadFull(r, in)
  6. if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
  7. fmt.Println("Here D")
  8. return nil, err
  9. }
  10. fmt.Println(n)
  11. in = in[:n]
  12. return in, nil
  13. }

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.

答案1

得分: 1

应该是这样的:

  1. h, err := GetHeader(r, l)

而不是

  1. h, err := GetHeader(f, l)
英文:

It should be

  1. h, err := GetHeader(r, l)

instead of

  1. h, err := GetHeader(f, l)

huangapple
  • 本文由 发表于 2021年11月19日 02:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/70025046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定