libzip开源库地址:https://libzip.org
下载源码:
wget https://libzip.org/download/libzip-1.8.0.tar.gz
解压:
tar -zxvf libzip-1.8.0.tar.gz
进入解压目录:
cd libzip-1.8.0
创建构建文件夹并进入
mkdir build
cd build
执行cmake进行编绎
cmake …
编绎与安装
make
make install
以上完成源码编绎
生成库文件libzip.so, libzipso.5, libzipso.5.4
#include "zip/zipconf.h"
#include "zip/zip.h"
#include "dirent.h"
#include <sys/stat.h>
#include <vector>
#include <string>
#include <iostream>
int main()
{
string zip_path = GetExePath();
string zip_name = GetExePath() + "zip/test.zip";
vector<string> filenames;
DIR* pdir = nullptr;
struct dirent* pdirent = nullptr;
if((pdir = opendir(zip_path.c_str())) == nullptr) {
cout << "PackFileToZip, folder open failed: " << zip_path;
return 0;
}
while ((pdirent = readdir(pdir)) != nullptr) {
if(strcmp(pdirent->d_name, ".") != 0 && strcmp(pdirent->d_name, "..") != 0) {
filenames.push_back(zip_path + pdirent->d_name);
cout << zip_path + pdirent->d_name <<"\n";
}
}
int ierr = 0;
zip* zipfile = nullptr;
zip_source* srcfile = nullptr;
string path = zip_name.substr(0, zip_name.find_last_of("/"));
if (NULL == opendir(path.c_str())) {
if (mkdir(path.c_str(), 0777) != 0) {
HDSLOG_THROW("mkdir failed:" << path << ",errno=" << errno);
}
} else if (access(zip_name.c_str(), F_OK) != -1) {
int r = remove(zip_name.c_str());
if (r != 0) HDSLOG_THROW("remove file error:" << zip_name);
}
zipfile = zip_open(zip_name.c_str(), ZIP_CREATE | ZIP_TRUNCATE, &ierr);
if(!zipfile) {
cout << "PackFileToZip, zip file open failed:" << ierr;
return 0;
}
for (const auto& file : filenames) {
srcfile = zip_source_file(zipfile, file.c_str(), 0, -1);
if (!srcfile) {
cout << "PackFileToZip, open source file failed";
zip_close(zipfile);
return;
}
zip_file_add(zipfile, file.c_str(), srcfile, ZIP_FL_OVERWRITE);
}
zip_close(zipfile);
//以上打包完毕
//以下为解压
zipfile = zip_open(zip_name.c_str(), ZIP_CHECKCONS, &ierr);
if(zipfile == nullptr) {
cout << "PackFileToZip, zip file open failed:" << ierr;
return;
}
zip_int64_t i64num = zip_get_num_entries(zipfile, 0);
struct zip_stat stat;
zip_file* pfile;
string unzip_path = GetExePath() + "upzip/";
if (NULL == opendir(unzip_path.c_str())) {
if (mkdir(unzip_path.c_str(), 0777) != 0) {
cout << "mkdir failed:" << unzip_path << ",errno=" << errno;
zip_close(zipfile);
return 0;
}
}
for (zip_int64_t i=0; i<i64num; i++) {
zip_stat_index(zipfile, i, 0, &stat);
pfile = zip_fopen_index(zipfile, i, 0);
if (!pfile) {
cout << "zip file open failed";
zip_close(zipfile);
return 0;
}
string filename(stat.name);
filename = unzip_path + filename.substr(filename.find_last_of("/")+1);
FILE *fp = fopen(filename.c_str(), "w+");
if (!fp) {
cout << "create local file failed";
zip_close(zipfile);
return 0;
}
char buff[stat.size] = {0};
zip_fread(pfile, buff, stat.size);
fwrite(buff, 1, stat.size, fp);
fclose(fp);
}
zip_close(zipfile);
}
string GetExePath() {
const int MAX_PATH = 1024;
char current_absolute_path[MAX_PATH + 1];
// 获取当前程序绝对路径
int cnt = readlink("/proc/self/exe", current_absolute_path, MAX_PATH);
if (cnt < 0 || cnt >= MAX_PATH) {
return "";
}
// 获取当前目录绝对路径,即去掉程序名
int i;
for (i = cnt; i >= 0; --i) {
if (current_absolute_path[i] == '/') {
current_absolute_path[i + 1] = '\0';
break;
}
}
return current_absolute_path;
}
/*
char buff[stat.size] = {0};
zip_fread(pfile, buf, stat.size);
std::fstream fs;
fs.open(stat.name, std::fstream::binary | std::fstream::out);
fs.write(buf,stat.size);
fs.close();
*/