【MFC】VC调用WinRar解压文件(如:.gz)

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会出现黑窗口,用户体验不好

人生就像是一场旅行,遇到的既有感人的,

【MFC】VC调用WinRar解压文件(如:.gz)

相关文章:

你感兴趣的文章:

标签云: