#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include "minizip/zip.h"
#include "minizip/unzip.h"
using namespace std;
void EnumDirFiles(const string& dirPrefix,const string& dirName,vector<string>& vFiles)
{
if (dirPrefix.empty() || dirName.empty())
return;
string dirNameTmp = dirName;
string dirPre = dirPrefix;
if (dirNameTmp.find_last_of("/") != dirNameTmp.length() - 1)
dirNameTmp += "/";
if (dirNameTmp[0] == '/')
dirNameTmp = dirNameTmp.substr(1);
if (dirPre.find_last_of("/") != dirPre.length() - 1)
dirPre += "/";
string path;
path = dirPre + dirNameTmp;
struct stat fileStat;
DIR* pDir = opendir(path.c_str());
if (!pDir) return;
struct dirent* pDirEnt = NULL;
while ( (pDirEnt = readdir(pDir)) != NULL )
{
if (strcmp(pDirEnt->d_name,".") == 0 || strcmp(pDirEnt->d_name,"..") == 0)
continue;
string tmpDir = dirPre + dirNameTmp + pDirEnt->d_name;
if (stat(tmpDir.c_str(),&fileStat) != 0)
continue;
string innerDir = dirNameTmp + pDirEnt->d_name;
if (fileStat.st_mode & S_IFDIR == S_IFDIR)
{
EnumDirFiles(dirPrefix,innerDir,vFiles);
continue;
}
vFiles.push_back(innerDir);
}
if (pDir)
closedir(pDir);
}
int writeInZipFile(zipFile zFile,const string& file)
{
fstream f(file.c_str(),std::ios::binary | std::ios::in);
f.seekg(0, std::ios::end);
long size = f.tellg();
f.seekg(0, std::ios::beg);
if ( size <= 0 )
{
return zipWriteInFileInZip(zFile,NULL,0);
}
char* buf = new char[size];
f.read(buf,size);
int ret = zipWriteInFileInZip(zFile,buf,size);
delete[] buf;
return ret;
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
cout<<"usage: mini from to"<<endl;
return -1;
}
string dest = string(argv[1]);
string src = string(argv[2]);
if (src.find_last_of("/") == src.length() - 1)
src = src.substr(0,src.length()-1);
struct stat fileInfo;
stat(src.c_str(), &fileInfo);
if (S_ISREG(fileInfo.st_mode))
{
zipFile zFile = zipOpen(dest.c_str(),APPEND_STATUS_CREATE);
if (zFile == NULL)
{
cout<<"openfile failed"<<endl;
return -1;
}
zip_fileinfo zFileInfo = { 0 };
int ret = zipOpenNewFileInZip(zFile,src.c_str(),&zFileInfo,NULL,0,NULL,0,NULL,0,Z_DEFAULT_COMPRESSION);
if (ret != ZIP_OK)
{
cout<<"openfile in zip failed"<<endl;
zipClose(zFile,NULL);
return -1;
}
ret = writeInZipFile(zFile,src);
if (ret != ZIP_OK)
{
cout<<"write in zip failed"<<endl;
zipClose(zFile,NULL);
return -1;
}
zipClose(zFile,NULL);
cout<<"zip ok"<<endl;
}
else if (S_ISDIR(fileInfo.st_mode))
{
size_t pos = src.find_last_of("/");
string dirName = src.substr(pos + 1);
string dirPrefix = src.substr(0,pos);
zipFile zFile = zipOpen(dest.c_str(),APPEND_STATUS_CREATE);
if (zFile == NULL)
{
cout<<"openfile failed"<<endl;
return -1;
}
vector<string> vFiles;
EnumDirFiles(dirPrefix,dirName,vFiles);
vector<string>::iterator itF = vFiles.begin();
for(;itF != vFiles.end(); ++itF)
{
zip_fileinfo zFileInfo = { 0 };
int ret = zipOpenNewFileInZip(zFile,itF->c_str(),&zFileInfo,NULL,0,NULL,0,NULL,0,Z_DEFAULT_COMPRESSION);
if (ret != ZIP_OK)
{
cout<<"openfile in zip failed"<<endl;
zipClose(zFile,NULL);
return -1;
}
ret = writeInZipFile(zFile,*itF);
if (ret != ZIP_OK)
{
cout<<"write in zip failed"<<endl;
zipClose(zFile,NULL);
return -1;
}
}
zipClose(zFile,NULL);
cout<<"zip ok"<<endl;
}
return 0;
}target=mini
lib=-lminizip -laes -lz
libpath=-L/usr/local/lib
incpath=-I/usr/local/include
${target}:
g++ main.cpp -g -o $@ ${lib} ${libpath} ${incpath}
clean:
-rm ${target}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享名稱:minizipc++壓縮文件及文件夾-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://chinadenli.net/article32/edhpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、App設(shè)計(jì)、Google、App開發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)