C++ 壓縮文件及文件夾方法 使用zlib開源庫
使用zlib-1.2.11版本的開源庫,實現(xiàn)我需要的對文件或者文件夾的壓縮,查閱了一些博客大牛的資料,后面根據(jù)自己的需要修改。
下面給出我的代碼:
#include "stdafx.h" #include <string> #include <iostream> #include <vector> #include <Shlwapi.h> #include "zip.h" #include "unzip.h" #include "zlib.h" #include <stdio.h> #include <stdlib.h> #include <fstream> #include <sstream> using namespace std; //部分頭文件不需要(自行去掉) #pragma comment(lib, "Shlwapi.lib") bool nyAddfiletoZip(zipFile zfile, const std::string& fileNameinZip, const std::string& srcfile); bool nyCollectfileInDirtoZip(zipFile zfile, const std::string& filepath, const std::string& parentdirName); bool nyCreateZipfromDir(const std::string& dirpathName, const std::string& zipfileName, const std::string& parentdirName); int _tmain(int argc, _TCHAR* argv[]) { std::string dirpath = "D:\\RecycleBin\\wei"; //源文件/文件夾 std::string zipfileName = "D:\\RecycleBin\\lango.rar"; //目的壓縮包 nyCreateZipfromDir(dirpath, zipfileName, "wei"); system("pause"); return 0; } /* * 函數(shù)功能 :解壓zip文件 * 備 注 :參數(shù)strFilePath表示zip壓縮文件的路徑 * 參數(shù)strTempPath表示要解壓到的文件目錄 */ bool nyAddfiletoZip(zipFile zfile, const std::string& fileNameinZip, const std::string& srcfile) { if (NULL == zfile || fileNameinZip.empty()/* || srcfile.empty()為空代表空目錄*/) { return 0; } int nErr = 0; zip_fileinfo zinfo = {0}; tm_zip tmz = { 0 }; zinfo.tmz_date = tmz; zinfo.dosDate = 0; zinfo.internal_fa = 0; zinfo.external_fa = 0; char sznewfileName[MAX_PATH] = { 0 }; memset(sznewfileName, 0x00, sizeof(sznewfileName)); strcat_s(sznewfileName, fileNameinZip.c_str()); if (srcfile.empty()) { strcat_s(sznewfileName, "\\"); } nErr = zipOpenNewFileInZip(zfile, sznewfileName, &zinfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); if (nErr != ZIP_OK) { return false; } if (!srcfile.empty()) { //打開源文件 FILE* srcfp = _fsopen(srcfile.c_str(), "rb", _SH_DENYNO); if (NULL == srcfp) { std::cout << "Open source file failed." << std::endl; return false; } //讀入源文件寫入zip文件 int numBytes = 0; char* pBuf = new char[1024 * 100]; if (NULL == pBuf) { std::cout << "new buffer failed." << std::endl; return 0; } while (!feof(srcfp)) { memset(pBuf, 0x00, sizeof(pBuf)); numBytes = fread(pBuf, 1, sizeof(pBuf), srcfp); nErr = zipWriteInFileInZip(zfile, pBuf, numBytes); if (ferror(srcfp)) { break; } } delete[] pBuf; fclose(srcfp); } zipCloseFileInZip(zfile); return true; } bool nyCollectfileInDirtoZip(zipFile zfile, const std::string& filepath, const std::string& parentdirName) { if (NULL == zfile || filepath.empty()) { return false; } bool bFile = false; std::string relativepath = ""; WIN32_FIND_DATAA findFileData; char szpath[MAX_PATH] = { 0 }; if (::PathIsDirectoryA(filepath.c_str())) { strcpy_s(szpath, sizeof(szpath) / sizeof(szpath[0]), filepath.c_str()); int len = strlen(szpath) + strlen("\\*.*") + 1; strcat_s(szpath, len, "\\*.*"); } else { bFile = true; strcpy_s(szpath, sizeof(szpath) / sizeof(szpath[0]), filepath.c_str()); } HANDLE hFile = ::FindFirstFileA(szpath, &findFileData); if (NULL == hFile) { return false; } do { if (parentdirName.empty()) relativepath = findFileData.cFileName; else relativepath = parentdirName + "\\" + findFileData.cFileName;//生成zip文件中的相對路徑 if (findFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { if (strcmp(findFileData.cFileName, ".") != 0 && strcmp(findFileData.cFileName, "..") != 0) { nyAddfiletoZip(zfile, relativepath, ""); char szTemp[MAX_PATH] = { 0 }; strcpy_s(szTemp, filepath.c_str()); strcat_s(szTemp, "\\"); strcat_s(szTemp, findFileData.cFileName); nyCollectfileInDirtoZip(zfile, szTemp, relativepath); } continue; } char szTemp[MAX_PATH] = { 0 }; if (bFile) { //注意:處理單獨文件的壓縮 strcpy_s(szTemp, filepath.c_str()); } else { //注意:處理目錄文件的壓縮 strcpy_s(szTemp, filepath.c_str()); strcat_s(szTemp, "\\"); strcat_s(szTemp, findFileData.cFileName); } nyAddfiletoZip(zfile, relativepath, szTemp); } while (::FindNextFileA(hFile, &findFileData)); FindClose(hFile); return true; } bool nyCreateZipfromDir(const std::string& dirpathName, const std::string& zipfileName, const std::string& parentdirName) { bool bRet = false; /***********參數(shù)注釋*********/ /*APPEND_STATUS_CREATE 創(chuàng)建追加 APPEND_STATUS_CREATEAFTER 創(chuàng)建后追加(覆蓋方式) APPEND_STATUS_ADDINZIP 直接追加*/ /****************************/ zipFile zFile = NULL; if (!::PathFileExistsA(zipfileName.c_str())) { zFile = zipOpen(zipfileName.c_str(), APPEND_STATUS_CREATE); } else { zFile = zipOpen(zipfileName.c_str(), APPEND_STATUS_ADDINZIP); } if (NULL == zFile) { std::cout << "create zip file failed." << std::endl; return bRet; } if (nyCollectfileInDirtoZip(zFile, dirpathName, parentdirName)) { bRet = true; } zipClose(zFile, NULL); return bRet; }
zlib庫自行下載來編譯,然后加入到自己的項目里面去,(需要重新編譯哦,不順利的話可能會遇到很多問題哦x_O)。在下剛出自茅廬,不足之處還望指教,相互學(xué)習(xí)。后面還有解壓部分(閱讀了一個大神的代碼,后來發(fā)現(xiàn)了一個嚴(yán)肅的問題,我進(jìn)行了改正。x_O)到時候再把demo上傳吧!
以上這篇C++ 壓縮文件及文件夾方法 使用zlib開源庫就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持我們。
上一篇:C++設(shè)計模式之適配器模式(Adapter)
欄 目:C語言
下一篇:C++設(shè)計模式之組合模式(Composite)
本文標(biāo)題:C++ 壓縮文件及文件夾方法 使用zlib開源庫
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/833.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10fatal error LNK1104: 無法打開文件“l(fā)ibc.lib”的解決方法
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點數(shù)在內(nèi)存中的存儲方式詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文