国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ 壓縮文件及文件夾方法 使用zlib開源庫

2020-01-26 13:44:39
字體:
來源:轉載
供稿:網友

使用zlib-1.2.11版本的開源庫,實現我需要的對文件或者文件夾的壓縮,查閱了一些博客大牛的資料,后面根據自己的需要修改。

下面給出我的代碼:

#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;}/** 函數功能 :解壓zip文件* 備 注 :參數strFilePath表示zip壓縮文件的路徑*			參數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, "http://");	}	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("http://*.*") + 1;		strcat_s(szpath, len, "http://*.*");	}	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 + "http://" + 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, "http://");				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, "http://");			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;		/***********參數注釋*********/	/*APPEND_STATUS_CREATE		創建追加	APPEND_STATUS_CREATEAFTER	創建后追加(覆蓋方式)	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)。在下剛出自茅廬,不足之處還望指教,相互學習。后面還有解壓部分(閱讀了一個大神的代碼,后來發現了一個嚴肅的問題,我進行了改正。x_O)到時候再把demo上傳吧!

以上這篇C++ 壓縮文件及文件夾方法 使用zlib開源庫就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 达尔| 德江县| 阿巴嘎旗| 颍上县| 正安县| 山阴县| 天气| 班玛县| 界首市| 黑河市| 大理市| 富锦市| 喀什市| 寻乌县| 大渡口区| 重庆市| 临沂市| 庆安县| 德兴市| 木兰县| 谢通门县| 肥西县| 左权县| 吴江市| 蒙城县| 仙居县| 沧源| 寻甸| 揭阳市| 措勤县| 兴隆县| 绵竹市| 罗田县| 唐河县| 保康县| 巨鹿县| 漾濞| 南投市| 漾濞| 稻城县| 吉林省|