void UnpackFile(const CString & strFilePath)
{
CString winRarInstallPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";
CString strDestPath; //目标解压位置
int pos = strFilePath.ReverseFind('.');
strDestPath = strFilePath.Left(pos);
//删除同名文件
if(::PathFileExists(strDestPath));
{
DeleteFile(strDestPath);
}
// 清空文件
DeleteDirectories(strDestPath); //上篇文章中的函数
if (FALSE == ::CreateDirectory(strDestPath,NULL))
{
AfxMessageBox("创建解压路径失败");
return;
}
//x解压 -ibck后台执行 -o+如存在,则覆盖 -inul不弹出错误提示
//使用 -ibck,缩小到了系统托盘区域
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)
{
//这个地方将导致该函数为阻塞状态
WaitForSingleObject(pi.hProcess,INFINITE);
::GetExitCodeProcess(pi.hProcess,&dwExit);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
return;
}
需注意问题:
1.网上有说目标文件路径需要加一个“\“,可能调系统的函数的话是对的,但如果你是拷贝的winrar.exe的话,就不能加"\",不然是无法解压的。
2.调用winrar.exe 而不是rar.exe的原因,这样winrar可以托盘到通知区域,而rar.exe会出现黑窗口,用户体验不好