一种基于WinRAR的程序自动打包的方法

因客户的部分需求需要在测试时确定,某CAA项目需要客户频繁测试程序。并且客户为军工企业,无法通过网络发送程序包或使用U盘拷贝,只能通过光盘拷贝。这也就需要在打包程序时压缩成一个文件,所以有了程序自动打包的想法。网上查找WinRAR相关资料后,WinRAR果然支持脚本运行。所以有了如下代码。

个人觉得代码核心是有关WinRAR的脚本命令,这些命令网上有非常详细的资料,本文的技巧注要有2个:

1.打包前自动打开程序包的ReadMe,然后system("pause"),这样可以无限等待编写更新日志,编写之后再按任意键继续之后的执行;

2.由于是CAA项目,这个自动压缩的项目的编译结果xxx.exe也会被包含进程序包(没办法通过VS设置),而这个exe显然是不需要的。此时,脚本的move命令来了,它可以把正在执行的exe从文件夹移除,并且不影响该exe之后的执行。

本来还想自动刻盘,但网上资料太少,尝试失败之后没再继续研究。


//CAA自动打包
bool MakeCAAPackage()
{
	const string strAppPath="C:\\Program Files\\WinRAR\\WinRAR.exe";//WinRAR的安装路径
	const string strOriginPath="E:\\XXXWsp\\win_b64";//需要压缩的CAA程序包路径
	const string strUselessFilePath=strOriginPath+"\\code\\bin";//工程中可能存在测试模块,删掉这些模块编译的结果

	const string strResultFolder="D:\\CAA包";//压缩包要放置的文件夹

	//压缩文件名称
	string strResultPath;//压缩包的路径//我的压缩包采用win_b64_日期_版本,如win_b64_20181123_4
	string strRarFileName_NoSuffix=string("win_b64_")+GetSysLocalTime();//获取日期
	for (int i=1;i<=100;i++)//获取版本后缀
	{
		string strTempFilePath=strResultFolder+"\\"+strRarFileName_NoSuffix+"_"+NumberToStr(i)+".rar";
		if (_access(strTempFilePath.c_str(), 00)==-1)
		{
			strResultPath=strResultFolder+"\\"+strRarFileName_NoSuffix+"_"+NumberToStr(i);
			break;
		}
	}

	ShellExecuteA(NULL, ("open"),(strOriginPath+"\\readme.txt").c_str() , NULL,NULL,SW_SHOW);//在readme中添加修改内容

	system("pause");//等待对readme修改完毕后再继续压缩,才能连同readme一起打包

	string cmdDelete;	//删除不需要打包的某些文件
	cmdDelete="move "+strUselessFilePath+"\\*.pdb";
	system(cmdDelete.c_str());
	cmdDelete="move "+strUselessFilePath+"\\*.exp";
	system(cmdDelete.c_str());
	cmdDelete="move "+strUselessFilePath+"\\*.lib";
	system(cmdDelete.c_str());
	cmdDelete="move "+strUselessFilePath+"\\SWIEEOtherUseful*.*";//把this也删掉
	system(cmdDelete.c_str());


	string rarCmd = ("a -ep1 ")+strResultPath + (" ") +strOriginPath;//压缩脚本
	int nResult = (int)ShellExecuteA(NULL,("open"),strAppPath.c_str(),rarCmd.c_str(),NULL,SW_HIDE);//执行压缩脚本

	ShellExecuteA(NULL, ("open"), strResultFolder.c_str() , NULL,NULL,SW_SHOW);//打开压缩后放置的文件夹,便于发送或刻盘

	return true;
}

猜你喜欢

转载自blog.csdn.net/originalcandy/article/details/84568173