form1.cn
Make a little progress every day

C++中ZipArchive压缩与解压的编译安装与使用

20th of October 2020 Linux Command 2154

C++使用ZipArchive在linux中进行压缩,这方面网上的资料比较少,特别对于新手说,我是在windows中用vs2017新建linux工程,然后配置远程Linux服务器进行编译,下分步进行说明


1,到ZipArchive官方网站下载源码

http://www.artpol-software.com/Download.aspx

打开网站后下载这一项:Previous Version (Full Package) 

下载后为ziparchive_prev.zip,解压后为ziparchive_prev目录 里面有主程序源码和doc文档


2,放到linux中进行编译

先把ziparchive_prev\ZipArchive目录拷贝到linux系统的任意目录中

在把ziparchive_prev\_projects\Linux中的Makefile拷贝到ziparchive_prev\ZipArchive目录中

进入ziparchive_prev\ZipArchive目录

执行 make && make install

安装成功后会显示安装后的目录位置:一般都在(/usr/lib 和 /usr/include/ziparchive)

到此位置ZipArchive库就准备完成,下面可以使用了


3,默认情况下,ZipArchive库针对Windows平台进行编译。如果要编译Linux / Mac OS X平台的库,请确保已定义_ZIP_SYSTEM_LINUX。如果未定义,则将自动定义 _ZIP_SYSTEM_WIN, 并且该库将针对Windows平台进行编译。

所以需要:项目属性-> C/C ++->预处理程序->预处理程序定义 加入_ZIP_SYSTEM_LINUX

注意:Linux / OS X 的makefile已经定义了_ZIP_SYSTEM_LINUX ,而ZipArchive库MFC项目配置已经定义了_ZIP_IMPL_MFC,如果您使用的是原始ZipArchive项目(它们已经定义了_ZIP_IMPL_MFC或_ZIP_SYSTEM_LINUX),则可能会收到宏重新定义警告。


4,需要增加附加依赖项,保证在编译的时候可以正确找到ziparchive库

项目属性->链接器->输入->附加依赖项->编辑,增加-lstdc++ -lziparch,确定


5,代码使用例子


#include <ziparchive/ZipArchive.h>
//strCompressPath:要打包的目录
//strZipFilePath:要打包成ZIP的路径
bool MainLogic::CreateZip(const string &strCompressPath, const string &strZipFilePath)
{
CZipArchiveZipFile;
int nStartTime = time(0);
try
{
cout << "start zip:" << strZipFilePath.c_str() << endl;
if (!ZipFile.Open(strZipFilePath.c_str(), CZipArchive::zipCreate))
{
// cout << "create zip fail:" << strZipFilePath.c_str() << endl;
return false;
}
}
catch (CZipException& ex)
{
//cout << "create zip fail:" << strZipFilePath.c_str() << endl;
return false;
}
ZipFile.AddNewFiles(strCompressPath.c_str(), (".*"), true, 0, true);
ZipFile.Close();
cout << "zip finish:" << time(0) - nStartTime << endl;
return true;
}

到此压缩完成,后面会补上在windows中解压的操作流程