#include "stdafx.h"
#include "PackageFile.h"
CPackageFile CPackageFile::m_Instance;
CPackageFile::CPackageFile()
{
}
CPackageFile::~CPackageFile()
{
}
std::string CPackageFile::getRarRegeditPath()
{
#define MY_BUFSIZE 132 // Arbitrary initial value.
// Dynamic allocation will be used.
HKEY hKey = NULL;
TCHAR szProductType[MY_BUFSIZE] = { 0 };
DWORD dwBufLen = MY_BUFSIZE;
LONG lRet; // 下面是打开注册表, 只有打开后才能做其他操作
lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, // 要打开的根键
//TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\WinRAR.exe"), // 要打开的子子键
TEXT("SOFTWARE\\7-Zip"),
0,
// 这个一定要为0
KEY_QUERY_VALUE, // 指定打开方式,此为读
&hKey); // 用来返回句柄
if (lRet != ERROR_SUCCESS) // 判断是否打开成功
return ""; // 下面开始查询
lRet = RegQueryValueEx(hKey, // 打开注册表时返回的句柄
TEXT("Path"), //要查询的名称,qq安装目录记录在这个保存
NULL, // 一定为NULL或者0
NULL,
(LPBYTE)szProductType, // 我们要的东西放在这里
&dwBufLen);
if (lRet != ERROR_SUCCESS) // 判断是否查询成功
return "";
RegCloseKey(hKey);
char szroductStr[MY_BUFSIZE];
szProductType[MY_BUFSIZE - 1] = 0;
unsigned int num = strlen((char *)szProductType);
unsigned sizeNum = 1;
if (num == 1) sizeNum = 2;
unsigned int j = 0;
for (unsigned int i = 0; i < dwBufLen; i += sizeNum)
{
szroductStr[j] = ((char *)szProductType)[i];
j++;
}
string returnString(szroductStr);
return returnString;
}
bool CPackageFile::CompressingFile(const char * SrcDir, const char *DstcTarGzFileName)
{
string strCmd; //存放命令字符串
string strRegitPath = getRarRegeditPath(); //通过注册表获取7.exe安装路径
CFileFind fileFind;
if (strRegitPath.empty())
{
AfxMessageBox("没有找到7-Zip.exe!");
return false;
}
CString strFileSrcPath = SrcDir;
CString strAllFile = "*";
strFileSrcPath = strFileSrcPath + strAllFile;
CString strFilePath = DstcTarGzFileName;
int iLen = strFilePath.ReverseFind('.');
CString strTarFilePathe;
strTarFilePathe = strFilePath.Mid(0, iLen);
strCmd = "\"" + strRegitPath + "\\7z.exe\" a -tTar \"" + strTarFilePathe.GetBuffer(0) + "\" \"" + strFileSrcPath.GetBuffer(0) + "\"";
if (string::npos != strCmd.find("&&") || string::npos != strCmd.find("||") || string::npos != strCmd.find("&") || string::npos != strCmd.find(";"))
{
return false;
}
if (!ExecuteCMD((char*)strCmd.c_str()))
{
return false;
}
strCmd = "\"" + strRegitPath + "\\7z.exe\" a -tGZip \"" + strFilePath.GetBuffer(0) + "\" \"" + strTarFilePathe.GetBuffer(0) + "\"";
if (string::npos != strCmd.find("&&") || string::npos != strCmd.find("||") || string::npos != strCmd.find("&") || string::npos != strCmd.find(";"))
{
return false;
}
if (!ExecuteCMD((char*)strCmd.c_str()))
{
return false;
}
DeleteFile(strTarFilePathe);
return true;
}
bool CPackageFile::ExecuteCMD(char *strCmd)
{
BOOL bResult = TRUE;
DWORD dwExitCode = 0;
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = { 0 };
bResult = CreateProcess(NULL, strCmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (bResult)
{
// 关闭子进程的主线程句柄
CloseHandle(pi.hThread);
// 等待子进程的退出
if (WAIT_FAILED == WaitForSingleObject(pi.hProcess, INFINITE))
{
;
}
// 获取子进程的退出码
GetExitCodeProcess(pi.hProcess, &dwExitCode);
if (0 != dwExitCode)
{
CloseHandle(pi.hProcess);
return false;
}
// 关闭子进程句柄
CloseHandle(pi.hProcess);
}
else
{
return false;
}
return true;
}
没有合适的资源?快使用搜索试试~ 我知道了~
PackageFile
共2个文件
h:1个
cpp:1个
需积分: 50 3 下载量 38 浏览量
2016-07-12
14:46:57
上传
评论
收藏 2KB RAR 举报
温馨提示
通过在注册表中寻找安装了的压缩软件,再通过CMD调用,进行压缩和解压
资源推荐
资源详情
资源评论
收起资源包目录
PackageFile.rar (2个子文件)
PackageFile
PackageFile.h 348B
PackageFile.cpp 4KB
共 2 条
- 1
资源评论
GongKiro
- 粉丝: 5
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功