[MFC] VC calls WinRar to decompress the file (eg: .gz)

[cpp]  view plain copy  
  1. void UnpackFile(const CString & strFilePath)  
  2. {  
  3.     CString winRarInstallPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";   
  4.     CString strDestPath;  //Destination decompression location  
  5.     int pos  = strFilePath.ReverseFind('.');  
  6.     strDestPath = strFilePath.Left(pos);  
  7.     // delete the file with the same name  
  8.     if(::PathFileExists(strDestPath));  
  9.     {  
  10.       DeleteFile(strDestPath);  
  11.     }  
  12.     // clear the file  
  13.     DeleteDirectories(strDestPath);  //The function in the previous article  
  14.       
  15.     if (FALSE == ::CreateDirectory(strDestPath,NULL))  
  16.     {  
  17.         AfxMessageBox( "Failed to create decompression path" );  
  18.         return;  
  19.     }  
  20.       
  21.     //x decompression -ibck background execution -o+ if it exists, overwrite -inul without popping up an error message    
  22.     //Use -ibck to shrink to the system tray area  
  23.     CString strCmd= "\"" + winRarInstallPath + "\" x -ibck -o+ -inul \"" + strFilePath + "\" \"" + strDestPath+"\"";   
  24.     STARTUPINFO si = { sizeof (si)};  
  25.     PROCESS_INFORMATION pi;   
  26.   
  27.     BOOL bRet=CreateProcess(NULL,strCmd.GetBuffer(MAX_PATH),NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);    
  28.     DWORD dwExit=0;    
  29.     if(bRet)    
  30.     {    
  31.         //This place will cause the function to block    
  32.         WaitForSingleObject(pi.hProcess,INFINITE);          
  33.         ::GetExitCodeProcess(pi.hProcess,&dwExit);    
  34.         CloseHandle(pi.hThread);    
  35.         CloseHandle(pi.hProcess);    
  36.     }      
  37.     return;  
  38. }  


Matters needing attention:

1. It is said on the Internet that a "\" needs to be added to the target file path. It may be correct to adjust the function of the system, but if you copied winrar.exe, you cannot add "\", otherwise it cannot be decompressed.

2. The reason why winrar.exe is called instead of rar.exe, so that winrar can be trayed to the notification area, and rar.exe will have a black window, and the user experience is not good

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325967676&siteId=291194637