因为7z是个开源的,本来以为实现的代码会很容易找,结果都没有可以直接用的,自己就根据网上的代码搞了一下。
本来是想利用dll文件的,但是一直就没有找到相关的代码。就直接用了exe文件。文末有链接~大家可以直接下载,也可以去官方去找源代码运行~
代码部分:
#include"tchar.h"
#include<windows.h>
#include<stdio.h>
#include <iostream>
using namespace std;
void Compress()
{
LPCSTR Path;
Path= "C:\\util\\7za.exe";
LPCSTR FileName ="C:\\util\\test.txt";
string str;
str+=" a ";
str+="C:\\util\\test.7z ";
str+=FileName;
LPCSTR strP=str.c_str();
HINSTANCE hRet = ShellExecute(NULL,"open",Path,strP,NULL,SW_SHOW);//SW_SHOW显示对话框
if (!hRet)
{
cout<<"压缩失败"<<"\n";
}
else
{
cout<<"压缩成功"<<"\n";
}
}
void uncompress()
{
LPCSTR Path;
Path= "C:\\util\\7za.exe";
LPCSTR FileName ="C:\\util\\test.txt";
string str;
str+=" x ";
str+="C:\\util\\test.7z ";
str+="-oout";
LPCSTR strP=str.c_str();
HINSTANCE hRet = ShellExecute(NULL,"open",Path,strP,NULL,SW_SHOW);//SW_SHOW显示对话框
if (!hRet)
{
cout<<"解压缩失败"<<"\n";
}
else
{
cout<<"解压缩成功"<<"\n";
}
}
int main()
{
Compress();
uncompress();
return 0;
}
我这边是直接可以拿来用的。 但是路径问题大家要注意一下,每个人根据自己的情况会有所不同。
https://download.youkuaiyun.com/download/h123x1/10312648