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

前端 未结 2 875
深忆病人
深忆病人 2020-12-22 10:13

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

相关标签:
2条回答
  • 2020-12-22 10:25

    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.

    0 讨论(0)
  • 2020-12-22 10:40

    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
    }
    
    0 讨论(0)
提交回复
热议问题