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

[cpp]  view plain  copy
  1. void UnpackFile(const CString & strFilePath)  
  2. {  
  3.     CString winRarInstallPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";   
  4.     CString strDestPath; //目标解压位置  
  5.     int pos  = strFilePath.ReverseFind('.');  
  6.     strDestPath = strFilePath.Left(pos);  
  7.     //删除同名文件  
  8.     if(::PathFileExists(strDestPath));  
  9.     {  
  10.       DeleteFile(strDestPath);  
  11.     }  
  12.     // 清空文件  
  13.     DeleteDirectories(strDestPath); //上篇文章中的函数  
  14.       
  15.     if (FALSE == ::CreateDirectory(strDestPath,NULL))  
  16.     {  
  17.         AfxMessageBox("创建解压路径失败");  
  18.         return;  
  19.     }  
  20.       
  21.     //x解压  -ibck后台执行 -o+如存在,则覆盖 -inul不弹出错误提示    
  22.     //使用 -ibck,缩小到了系统托盘区域  
  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.         //这个地方将导致该函数为阻塞状态    
  32.         WaitForSingleObject(pi.hProcess,INFINITE);          
  33.         ::GetExitCodeProcess(pi.hProcess,&dwExit);    
  34.         CloseHandle(pi.hThread);    
  35.         CloseHandle(pi.hProcess);    
  36.     }      
  37.     return;  
  38. }  


需注意问题:

1.网上有说目标文件路径需要加一个“\“,可能调系统的函数的话是对的,但如果你是拷贝的winrar.exe的话,就不能加"\",不然是无法解压的。

2.调用winrar.exe 而不是rar.exe的原因,这样winrar可以托盘到通知区域,而rar.exe会出现黑窗口,用户体验不好

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值