最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ 壓縮文件及文件夾方法 使用zlib開(kāi)源庫(kù)

 更新時(shí)間:2018年03月22日 09:28:25   作者:sadwjy520  
下面小編就為大家分享一篇C++ 壓縮文件及文件夾方法 使用zlib開(kāi)源庫(kù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

使用zlib-1.2.11版本的開(kāi)源庫(kù),實(shí)現(xiàn)我需要的對(duì)文件或者文件夾的壓縮,查閱了一些博客大牛的資料,后面根據(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())
	{
		//打開(kāi)源文件
		FILE* srcfp = _fsopen(srcfile.c_str(), "rb", _SH_DENYNO);
		if (NULL == srcfp)
		{
			std::cout << "Open source file failed." << std::endl;
			return false;
		}
		//讀入源文件寫(xiě)入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文件中的相對(duì)路徑
		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)
		{
			//注意:處理單獨(dú)文件的壓縮
			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庫(kù)自行下載來(lái)編譯,然后加入到自己的項(xiàng)目里面去,(需要重新編譯哦,不順利的話(huà)可能會(huì)遇到很多問(wèn)題哦x_O)。在下剛出自茅廬,不足之處還望指教,相互學(xué)習(xí)。后面還有解壓部分(閱讀了一個(gè)大神的代碼,后來(lái)發(fā)現(xiàn)了一個(gè)嚴(yán)肅的問(wèn)題,我進(jìn)行了改正。x_O)到時(shí)候再把demo上傳吧!

以上這篇C++ 壓縮文件及文件夾方法 使用zlib開(kāi)源庫(kù)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

托里县| 高雄县| 崇左市| 张家口市| 英吉沙县| 张掖市| 静宁县| 枞阳县| 开封市| 鄢陵县| 武汉市| 民丰县| 海南省| 盐边县| 岳阳县| 两当县| 枞阳县| 昌都县| 江川县| 绍兴县| 南溪县| 修水县| 嘉峪关市| 江口县| 林口县| 昭苏县| 绍兴县| 泌阳县| 安仁县| 桐梓县| 西乡县| 伊春市| 五寨县| 鄂托克旗| 寿光市| 大名县| 新和县| 广河县| 益阳市| 延寿县| 沧州市|