溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-10-25 13:40:04 來源:腳本之家 閱讀:762 作者:sadwjy520 欄目:編程語言

使用zlib-1.2.11版本的開源庫,實(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())
	{
		//打開源文件
		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文件中的相對(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庫自行下載來編譯,然后加入到自己的項(xiàng)目里面去,(需要重新編譯哦,不順利的話可能會(huì)遇到很多問題哦x_O)。在下剛出自茅廬,不足之處還望指教,相互學(xué)習(xí)。后面還有解壓部分(閱讀了一個(gè)大神的代碼,后來發(fā)現(xiàn)了一個(gè)嚴(yán)肅的問題,我進(jìn)行了改正。x_O)到時(shí)候再把demo上傳吧!

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI