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)了一個嚴肅的問題,我進行了改正。x_O)到時候再把demo上傳吧!
以上這篇C++ 壓縮文件及文件夾方法 使用zlib開源庫就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++類的自動轉(zhuǎn)換和強制類型轉(zhuǎn)換的實現(xiàn)示例
類的自動轉(zhuǎn)換和強制類型轉(zhuǎn)換是面向?qū)ο缶幊讨刑幚眍愋椭g轉(zhuǎn)換的兩種重要機制,本文就來介紹一下這兩種方法的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-07-07
c語言 數(shù)據(jù)結(jié)構(gòu)實現(xiàn)之字符串
這篇文章主要介紹了c語言 數(shù)據(jù)結(jié)構(gòu)實現(xiàn)之字符串的相關(guān)資料,需要的朋友可以參考下2017-05-05
C++實戰(zhàn)之二進制數(shù)據(jù)處理與封裝
在電腦上一切數(shù)據(jù)都是通過二進制(0或1)進行存儲的,通過多位二進制數(shù)據(jù)可以進而表示整形、浮點型、字符、字符串等各種基礎(chǔ)類型數(shù)據(jù)或者一些更復(fù)雜的數(shù)據(jù)格式。本文將為大家詳細講講二進制數(shù)據(jù)處理與封裝,需要的可以參考一下2022-08-08
關(guān)于c++11與c風(fēng)格路徑拼接的速度對比
這篇文章主要介紹了關(guān)于c++11與c風(fēng)格路徑拼接的速度對比分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

