#include "stdafx.h"
#include <Windows.h>
#include <locale.h>
#include <shellapi.h>
#pragma comment(lib, "shell32.lib")
//pFilePath 需要压缩的文件路径
//pCompressPath 压缩后的文件路径
bool Compress(PWCHAR pFilePath, PWCHAR pCompressPath)
{
WCHAR sz7zPath[256] = {0};
wcscpy(sz7zPath, L"C:\\Program Files (x86)\\7-Zip\\7z.exe");
WCHAR szParam[1024] = {0};
wsprintf(szParam, L"a -t7z %s %s", pCompressPath, pFilePath);
HINSTANCE hRet = ShellExecute(NULL, L"open", sz7zPath, szParam, NULL, SW_HIDE);//SW_SHOW显示对话框
if (!hRet)
{
wprintf(L"压缩失败!\n");
return false;
}
wprintf(L"压缩成功!\n");
return true;
}
//pFilePath 需要解压缩的文件路径
//pUnCompressPath 解压缩之后的文件路径
bool UnCompress(PWCHAR pFilePath, PWCHAR pUnCompressPath)
{
WCHAR sz7zPath[256] = {0};
wcscpy(sz7zPath, L"C:\\Program Files (x86)\\7-Zip\\7z.exe");
WCHAR szParam[1024] = {0};
wsprintf(szParam, L"e %s -o%s -y", pFilePath, pUnCompressPath);
HINSTANCE hRet =ShellExecute(NULL,L"open",sz7zPath,szParam,NULL,SW_HIDE);//SW_SHOW显示对话框
if (!hRet)
{
wprintf(L"解压失败!\n");
return false;
}
wprintf(L"解压成功!\n");
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL,"chs");
WCHAR szFilePath[256] = {0};
wcscpy(szFilePath, L"C:\\1");
WCHAR szCompressPath[256] = {0};
wcscpy(szCompressPath, L"C:\\1.7z");
//Compress(szFilePath,szCompressPath);
UnCompress(szCompressPath, szFilePath);
return 0;
}