- void UnpackFile(const CString & strFilePath)
- {
- CString winRarInstallPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";
- CString strDestPath; //Destination decompression location
- int pos = strFilePath.ReverseFind('.');
- strDestPath = strFilePath.Left(pos);
- // delete the file with the same name
- if(::PathFileExists(strDestPath));
- {
- DeleteFile(strDestPath);
- }
- // clear the file
- DeleteDirectories(strDestPath); //The function in the previous article
- if (FALSE == ::CreateDirectory(strDestPath,NULL))
- {
- AfxMessageBox( "Failed to create decompression path" );
- return;
- }
- //x decompression -ibck background execution -o+ if it exists, overwrite -inul without popping up an error message
- //Use -ibck to shrink to the system tray area
- CString strCmd= "\"" + winRarInstallPath + "\" x -ibck -o+ -inul \"" + strFilePath + "\" \"" + strDestPath+"\"";
- STARTUPINFO si = { sizeof (si)};
- PROCESS_INFORMATION pi;
- BOOL bRet=CreateProcess(NULL,strCmd.GetBuffer(MAX_PATH),NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
- DWORD dwExit=0;
- if(bRet)
- {
- //This place will cause the function to block
- WaitForSingleObject(pi.hProcess,INFINITE);
- ::GetExitCodeProcess(pi.hProcess,&dwExit);
- CloseHandle(pi.hThread);
- CloseHandle(pi.hProcess);
- }
- return;
- }
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