英文:
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)
的输出,并提取其输出的头部,但是这种方法不起作用。
f, err := os.Open(file)
if err != nil {
return fmt.Errorf("problem opening %s", filename)
}
r, _ := gzip.NewReader(f)
h, err := GetHeader(r, l)
这里的GetHeader
函数是这样的...
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
}
它用于成功检查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.
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.
答案1
得分: 1
应该是这样的:
h, err := GetHeader(r, l)
而不是
h, err := GetHeader(f, l)
英文:
It should be
h, err := GetHeader(r, l)
instead of
h, err := GetHeader(f, l)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论