源**:
ziphelper.h
#pragma once
#ifndef zip_h
#define zip_h
#include #include #include #include #include #include "zlib\unzip.h"
#include using namespace std;
class cpackfile
;static inline bool isfileexist(string ppath);
class cunpackfile
;#endif
ziphelper.cpp
#include "ziphelper.h"
//將檔案新增到zip檔案中,注意如果原始檔srcfile為空則新增空目錄
//filenameinzip: 在zip檔案中的檔名,包含相對路徑
void cpackfile::addfiletozip(zipfile zf, string filenameinzip, string srcfile)
; zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
zi.dosdate = 0;
zi.internal_fa = 0;
zi.external_fa = 0;
//如果srcfile為空,加入空目錄
string new_file_name = "";
new_file_name += filenameinzip;
if (srcfile == "")
//在zip檔案中建立新檔案
zipopennewfileinzip(zf, new_file_name.c_str(), &zi, null, 0, null, 0, null, z_deflated, z_default_compression);
if (srcfile != "")
//讀入原始檔並寫入zip檔案
char buf[100 * 1024]; //buffer
int numbytes = 0;
while (!feof(srcfp))
//關閉原始檔
fclose(srcfp);
} //關閉zip檔案
zipclosefileinzip(zf);
}//遞迴新增子目錄到zip檔案
void cpackfile::collectfilesindirtozip(zipfile zf, string strpath, string parentdir)
else
fullpath += fileinfo.name;
currentpath = strpath;
currentpath += "//";
currentpath += fileinfo.name;
if (fileinfo.attrib & _a_subdir)// is a directory
addfiletozip(zf, fullpath, currentpath); //將檔案新增到zip檔案中
} while (_findnext(handle, &fileinfo) == 0);//find next file
}//將某個目錄或者檔案(dirname)壓縮為zipfilename(.zip)檔案
void cpackfile::createzipfromdir(string dirname, string zipfilename)
if (fileinfo.attrib & _a_subdir)// is a directory
else// is a file
addfiletozip(newzipfile, fileinfo.name, dirname);
zipclose(newzipfile, null); //關閉zip檔案
}cpackfile::cpackfile()
cpackfile::~cpackfile()
static inline bool isfileexist(string ppath)
void cunpackfile::createdirfromzip(string dirname, string zipfilename)
if (!isfileexist(dirname))
_mkdir(dirname.c_str());
int nresult = unzgotofirstfile(unzipdir);
unsigned char * buf;
while (nresult == unz_ok)
if (last_slash_pos != std::string::npos)
}int size = unzipfileinfo.uncompressed_size;
buf = new unsigned char[size];
if (!buf)
memset(buf, 0, size*sizeof(char));
int nreadbytes;
if (unz_ok == unzopencurrentfilepassword(unzipdir, null))
file * pfile;
string filepos;
filepos = dirname;
if (slashflag)
filepos += "\\";
else
filepos += "/";
filepos += filepath;
fopen_s(&pfile, filepos.c_str(), "wb");
if (pfile)
fwrite(buf, nreadbytes, 1, pfile);
else
printf("無法開啟輸出檔案 %s \n", filepos);
fclose(pfile);
delete buf;
nresult = unzgotonextfile(unzipdir);
} unzclose(unzipdir);
}void cunpackfile::createfilefromzip(string fname, string zipfilename)
int nresult = unzgotofirstfile(unzipdir);
unsigned char * buf;
if (nresult == unz_ok)
memset(buf, 0, size*sizeof(char));
int nreadbytes;
if (unz_ok == unzopencurrentfilepassword(unzipdir, null))
file * pfile;
fopen_s(&pfile, fname.c_str(), "wb");
if (pfile)
fwrite(buf, nreadbytes, 1, pfile);
else
printf("無法開啟輸出檔案 %s \n", fname);
fclose(pfile);
delete buf;
} unzclose(unzipdir);
}cunpackfile::cunpackfile()
cunpackfile::~cunpackfile()
main.cpp
#include #include #include "ziphelper.h"
using namespace std;
int main(int argc, char** ar**)
Zlib檔案壓縮和解壓
zlib檔案壓縮和解壓 開源 http www.zlib.net zlib使用手冊 http www.zlib.net manual.html zlib wince版 http www.tenik.co.jp adachi wince 在這裡,你可以檢視基於各種作業系統平台的壓縮與解縮 實現。以下是...
充電 庫 Zlib檔案壓縮和解壓
開源 zlib使用手冊 manual.html zlib wince版 在這裡,你可以檢視基於各種作業系統平台的壓縮與解縮 實現。以下是經過測試的 wince 及 winxp 下的 第三步 包含標頭檔案 include zlib.h 主要使用fopen等c標準介面實現的,只用到zlib的compre...
Zlib庫的使用實現對zip檔案的解壓縮(二)
接之前的一篇文章 現在詳細地給出利用zlib對.zip檔案進行解壓縮的過程 參考文章 非常詳細 include unzip.h include zip.h include include using namespace std pragma comment lib,zdll.lib const ch...