Reputation: 1529
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)
Upvotes: 0
Views: 838
Reputation: 9904
The contents of a tar file is always header block, data block, header block, data block ... where every header block contains all the information of one file (filename, size, permissions,...) and the following data block contains the contents that file. The size of each data block is the next multiple of 512 of the file size as it is in the header block (that sentence looks awful to me. Could any native speaker correct is please). So if you have read one header block and want to skip to the next one calculate
size_t skip = filesize % 512 ? filesize + 512 - (filesize % 512) : filesize
or, more performantly
size_t skip = filesize + 511 & ~512;
and seek skip
bytes forward.
For example if your tar file contains two files a.bin of size 12345 (next multiple of 512 is 12800) and b.txt of size 123 (next multiple of 512 is -- obviously -- 512) then you would have:
\0
Upvotes: 2