how to skip a file inside the tar file to get a pa

2020-02-15 14:55发布

i am tring to get the contents of a html file which is present inside the tar file(i am using visual c++ to accomplish my task). my approach is to store the tar in a buffer using a stream and then store the contents of html in another buffer.Then using buffer going to the file name of each file present in tar file at location buffer[0-100](at this location we have the file name)and store the file name in "contents"(in my case) and search if it has the extension.html file ??

If it has .html in the name of the file then store its contents from the location buffer[PreviousFileSizes +512](by PreviousFileSizes i mean there were some files before this html file so we have to add their sizes in the buffer index to go to the correct location- i mean i am not assuming that the first file in tar file is html file-In my code i denote this PreviousFileSizes by "skip" - that means this much size to skip to go to our html file).

my code to achieve it is-

int skip=0;
            char contents [100];
            //char test[1000];
            do
            {

                    int SizeOfFile = CreateOctalToInteger(&buffer[skip+124],11);
                    size_t distance= ((SizeOfFile%512) ? SizeOfFile + 512 - (SizeOfFile%512) : SizeOfFile );
                    size_t skip= distance +512;
                    memcpy(contents,&buffer[skip],100);




            }
            while(strstr(contents,".html") != NULL);

am i going right ??Please correct me if there is anything wrong in my logic ??

2条回答
Root(大扎)
2楼-- · 2020-02-15 15:31

Finally i have made the solution for this question the code must be as follow-

char* StartPosition;
size_t skip= 0;
    char HtmlFileContents [200000];
    char contents [8000];
    do
    { 
            int SizeOfFile = CreateOctalToInteger(&buffer[skip+124],11);
            size_t distance= ((SizeOfFile%512) ? SizeOfFile + 512 - (SizeOfFile%512) : SizeOfFile );
            skip += distance + 512;
            memcpy(contents,&buffer[skip],100);
            if (StartPosition=strstr(contents,".html"))
            {
                MessageBox(m_hwndPreview,L"finally string is copied",L"BTN WND6",MB_ICONINFORMATION);
                int SizeOfFile = CreateOctalToInteger(&buffer[skip+124],11);
                memcpy(HtmlFileContents,&buffer[skip+512],SizeOfFile);
                break;
            }


    }
    while(strcmp(contents,".html") != NULL);

I guess its self explantory . and If not ?? Do not hesitate to ask me.

查看更多
太酷不给撩
3楼-- · 2020-02-15 15:39

Doesn't look too bad except for the errors :-)

  1. you set skip = ... instead of skip += .., so your position in buffer is only correct for the second file
  2. You don't check the first file (because it's do { ... } while() and the first time you call strstr(), contents is already filled with buffer at some poition skip > 0).
  3. You should also add a 'break' condition to stop looping when you find a 'file name' "".

EDIT and we should of course also check for the tar file size.

I would try it like that:

// I assume size_t bufsize to be the tar file size

size_t skip = 0;
while( bufsize > skip && strcmp( buffer+skip, "" ) != 0 && strstr( buffer+skip, ".html" ) != 0 ) {
     int SizeOfFile = CreateOctalToInteger(&buffer[skip+124],11);
     size_t distance= ((SizeOfFile%512) ? SizeOfFile + 512 - (SizeOfFile%512) : SizeOfFile );
     skip += distance +512;  
}

if( bufsize > skip && strstr( buffer+skip, ".html" ) == 0 ) {
    // hooray
    int SizeOfHTML = CreateOctalToInteger(&buffer[skip+124],11);
    char *htmlData = buffer+skip+512;

    // do stuff with htmlData
}
查看更多
登录 后发表回答