0

I am developing a visual c++ application . i need to know the file type (i mean whether it contains .png file or.html file or .txt file) present inside the tar file(just by c++ prgramming)-Nothing to deal with the commands. I have got some knowledge on the link below- how to parse a tar file here i have got information that at buffer[512] we have contents of a file present inside thge tar file.My first quesion is

(1.) suppose if i have more then 1 files present in tar and i got the size from the location (&buffer[124], 11); and from 512 to size of the file i had conntents of that file and i stored it in a buffer for my personal use.But as i understand this rule of (contents start from 512 location) is valid for the file present at the first position in the tar file. What if i have to get the position, contents and size of the file which is at 3/4 positions(what if am not sure about the position of the file present in the tar file) ???

(2.) Am i thinking right ??? if i have to go to next file contents i have to do 512*2 (because first file contents starting at 512 location so the next file will be having at 1024- I am sure its a wrong approach but could any one please correct it ??).

Actually i have to store only Html file contents in my buffer from the tar file(which contains number of files of different type)

4

1 回答 1

2

tar 文件的内容总是header block , data block , header block , data block ... 其中每个 header block 包含一个文件的所有信息(文件名,大小,权限,...),以下数据块包含该文件的内容。每个数据块的大小是文件大小的 512 的下一个倍数,因为它在标题块中(这句话对我来说看起来很糟糕。请任何母语人士正确)。因此,如果您已经阅读了一个标题块并想跳到下一个,请计算

 size_t skip = filesize % 512 ? filesize + 512 - (filesize % 512) : filesize

或者,更高效

 size_t skip = filesize + 511 & ~512;

并向前寻找skip字节。

例如,如果您的 tar 文件包含两个大小为 12345 的文件a.bin(512 的下一个倍数是 12800)和大小为 123 的b.txt(512 的下一个倍数是 - 显然 - 512),那么您将拥有:

  1. 包含有关a.bin的信息的标头,从 Pos 开始。0
  2. a.bin的数据从 Pos 开始。512
  3. 包含有关b.txt信息的标头,从 Pos 开始。512 + 12800 = 13312
  4. b.txt 的数据从Pos开始。13312 + 512 = 13824
  5. tar 文件的文件大小将至少为 13824 + 512 = 14324。在实践中,您通常会发现 tar 文件更大,接下来的 512 个字节位于 Pos。14324 将是\0
于 2013-07-25T15:45:12.523 回答