您的位置:首页 > 运维架构 > Shell

VC调用WinRAR压缩文件

2012-09-12 09:16 393 查看
     最近同学让帮忙写个小工具,基本要求如下:

按照以下要求调用WinRAR订做的程序:

1》 12abc,底板.dwg、

12abc.drw、

12abc.prt,然后如果存在.dwg文件就把这三个都以12abc开头命名的文件打包,打包文件以.dwg的文件名命名,后缀可以是.rar或者.zip,

最后的打包文件要以“12abc,底板”命名

2》对了,另外加一个a.frm的文件在里面,这个文件是固定的,每个打包文件里都放一个

3》如果可以在重命名的时候把小写转换为大写就更好了

并提供了一个bat文件供参考,bat内容如下:

 

@echo off
set "winrar=d:\Program Files\WinRAR\WinRAR.exe"
for /f "delims=" %%a in ('dir /a-d/b *.drw') do if exist "%%~na*.dwg" "%winrar%" a -afzip "%%~na" "%%~na.prt" "%%~na*.dwg" "a.frm" "%%a"

 

 代码下载:http://download.csdn.net/detail/stone_sky/4564602

     考虑到同学并不希望看到界面,故创建Win32 控制台应用程序,程序代码如下:

#include <afx.h>
#include <afxwin.h>
#include <tchar.h>  //添加对 TCHAR 的支持
#include <atlstr.h> //添加对 CString 的支持
#include <shellapi.h>//添加对ShellExcute()的支持,需要导入类库 shell32.lib
#include <iostream>
using namespace std;

//从注册表获取WinRAR的安装路径
CString GetWinRARPath()
{
HKEY hKEY;
LPCTSTR Rgspath = _T("Software\\WinRAR");
LONG ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Rgspath, 0, KEY_READ, &hKEY);
if(ret != ERROR_SUCCESS)
{
MessageBox(NULL, _T("获取WinRAR安装路径失败!"), _T("出错啦~!"), MB_OK);
RegCloseKey(hKEY);
return _T("");
}
// 读取键值内容
DWORD type = REG_SZ;
BYTE PathInfo[MAX_PATH];
DWORD dwInfoSize = MAX_PATH;
ret = RegQueryValueEx(hKEY, _T("exe32"), NULL, &type, PathInfo, &dwInfoSize);
if(ret != ERROR_SUCCESS)
{
MessageBox(NULL, _T("没有安装WinRar解压缩软件,请先安装WinRar解压缩软件"), _T("出错啦~!"), MB_OK);
RegCloseKey(hKEY);
return _T("");
}
CString Path = (wchar_t*)PathInfo;
return Path;
}
void _tmain(int argc, TCHAR **argv)
{
TCHAR Buffer[MAX_PATH];
DWORD dwRet;
//1. 判断处理的类型(默认按照 zip 格式处理)
CString strDealType=_T("zip");//默认按照zip格式处理替换
if(argc < 2)
{
// cout<<"默认将按照Zip格式进行替换"<<endl;
}
else
{
strDealType=argv[1];
strDealType.MakeLower();
if (strDealType!=_T("rar") && strDealType!=_T("zip"))
{
cout<<_T("输入处理的类型参数有误!将默认按照 zip 格式处理")<<endl;
strDealType=_T("zip");//默认按照zip格式处理
//  Sleep(1000);
}
}
//2.获取当前目录
dwRet = GetCurrentDirectory(MAX_PATH, Buffer);
if( dwRet == 0 )
{
printf("GetCurrentDirectory failed (Error Code:%d)\n", GetLastError());
return;
}
if(dwRet > MAX_PATH)
{
printf("Buffer too small; need %d characters\n", dwRet);
return;
}
//3.从注册表中读取winrar的安装路径
CString strWinRARPath=GetWinRARPath();
if(strWinRARPath.IsEmpty())
{
return;
}
//4.查找当前目录下drw文件
CFileFind finder;
CString strFoundFile=_T("");
CString strFileNameWithOutExt=_T("");
CString strOtherTypeFile2=_T("");
CString strOtherTypeFile3=_T("");
CString strOtherTypeFile4=_T("a.frm");
CString strConveredName=_T("");
CString strDestinName=_T("");
BOOL bTemVal=FALSE;
CString strCommandParam=_T("");

BOOL bWorking = finder.FindFile(_T("*.drw"));
while (bWorking)//找到了 .drw
{
bWorking = finder.FindNextFile();
strFoundFile=finder.GetFileName();//eg. 12Acd.acb
strFileNameWithOutExt=strFoundFile.Left(strFoundFile.GetLength()-wcslen(_T(".drw"))); //12Acd
CFileFind finderOthers;
strOtherTypeFile2=strFileNameWithOutExt+_T(".prt");
bTemVal = finderOthers.FindFile(strOtherTypeFile2);
if (bTemVal)//找到了 .prt
{
strOtherTypeFile3=strFileNameWithOutExt+_T("*.dwg");//如何使用正则表达式???
bTemVal = finderOthers.FindFile(strOtherTypeFile3);
if (bTemVal)//找到了 ",底板.dwg"
{
finderOthers.FindNextFile();
strOtherTypeFile3=finderOthers.GetFileName();

bTemVal=finderOthers.FindFile(strOtherTypeFile4);
if (bTemVal)
{
strConveredName=strOtherTypeFile3.Left(strOtherTypeFile3.GetLength()-wcslen(_T(".dwg")));
strConveredName.MakeUpper();//将文件名转换为大写
strDestinName=strConveredName+_T(".")+strDealType;

if (strDealType==_T("zip"))
{
strCommandParam=_T("a -afzip \"")+strDestinName+_T("\"  \"")+strFoundFile+_T("\"  \"")+strOtherTypeFile2+_T("\"  \"")+strOtherTypeFile3+_T("\"  \"")+strOtherTypeFile4+_T("\"");
}
else//strDealType为"rar"
{
strCommandParam=_T("a -afrar \"")+strDestinName+_T("\"  \"")+strFoundFile+_T("\"  \"")+strOtherTypeFile2+_T("\"  \"")+strOtherTypeFile3+_T("\"  \"")+strOtherTypeFile4+_T("\"");
}
}
//5.进行压缩处理
HANDLE handle=ShellExecute(NULL,NULL,strWinRARPath,strCommandParam,NULL,SW_SHOW);
if((int)handle<=32)
{
continue;//该文件压缩出错!进行下一个文件的压缩处理
}

}
}
}
}


该程序使用时:

1.系统安装winRAR

2.将该exe放到要进行压缩的文件所在的文件夹。

3.程序默认压缩为zip格式的,如果想压缩为rar格式的,则可以在该程序所在目录下为该程序创建一个快捷方式,右键该快捷方式,点击属性按钮,在目标后边添加参数:空格+RAR

 

参数的大小写随意,如果输入的参数不对,程序仍会按照zip格式进行压缩。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息