C++实现解zip解压缩功能:将一个目录内的内容打包到zip文件以及将zip文件解压到某个目录(windows平台)

您所在的位置:网站首页 怎么把文件解压到一个文件夹 C++实现解zip解压缩功能:将一个目录内的内容打包到zip文件以及将zip文件解压到某个目录(windows平台)

C++实现解zip解压缩功能:将一个目录内的内容打包到zip文件以及将zip文件解压到某个目录(windows平台)

2023-12-15 22:39| 来源: 网络整理| 查看: 265

本例用来简单实现windows平台下如何将一个目录下的文件压缩到指定的zip文件中,亦或是将一个zip文件的内容解压缩到某个目录。

 

以下是源码,代码中的注释解释了相关操作,一看就懂。

 

注:

本例将这两个功能封装在了一个叫ZipPack的命名空间中。

代码中使用到的zip.h zip.cpp unzip.h unzip.cpp可以从www.info-zip.org下载。同时我也提供了具体的代码示例:点击下载

 

.h

#pragma once #include namespace ZipPack { //* 遍历文件夹下的所有文件到zip //targetPath: 起始目录 //currentDir: 当前遍历所处的目录 //hZip: HZIP 句柄地址 void BrowseFileToZip(std::wstring targetDir, std::wstring currentDir, void* hZip); //* 将目录打包为zip //targetPath: 要打包的目录 //dstZip: 打包之后的zip文件位置 bool PackDirToZip(std::wstring targetDir, std::wstring dstZip); //* 将zip文件解压到对应的目录 //targetZip:需要解压的文件 //dstPath:解压之后的文件所在目录(如果目录内已存在相同文件则会覆盖) bool UnPackZipToDir(std::wstring targetZip, std::wstring dstPath, std::string psw=""); };

.cpp

#include "ZipPack.h" #include #include #include "zip.h" #include "unzip.h" #pragma comment(lib, "shlwapi.lib") //Windows API PathFileExists bool ZipPack::PackDirToZip( std::wstring targetDir, std::wstring dstZip ) { //用于输出调试信息 std::wstring dbgInfo; dbgInfo.resize(MAX_PATH); if(targetDir.empty() || dstZip.empty()) { wsprintfW(&dbgInfo[0], L"failed: empty path."); OutputDebugStringW(dbgInfo.c_str()); return false; } //检查目标路径是否有效 BOOL bTargetPathExists = ::PathFileExistsW(targetDir.c_str()); if(!bTargetPathExists) { wsprintfW(&dbgInfo[0], L"failed: invalid path(%s).", targetDir.c_str()); OutputDebugStringW(dbgInfo.c_str()); return false; } //创建HZIP, 这里默认不设置密码,如有需要可传入密码参数 HZIP hZip = CreateZip(dstZip.c_str(), 0); if(!IsZipHandleZ(hZip)) { wsprintfW(&dbgInfo[0], L"failed: create zip file(%s) failed.", dstZip.c_str()); OutputDebugStringW(dbgInfo.c_str()); return false; } //* 将目录下的所有子文件夹和子文件加入zip文件中 BrowseFileToZip(targetDir, targetDir, &hZip); CloseZip(hZip); return true; } void ZipPack::BrowseFileToZip(std::wstring targetDir, std::wstring currentDir, void* hZip) { HZIP* pHZip = (HZIP*)hZip; //用于输出调试信息 std::wstring dbgInfo; dbgInfo.resize(MAX_PATH); WIN32_FIND_DATA FindFileData; HANDLE hListFile; WCHAR szFilePath[MAX_PATH]; // 构造代表子目录和文件夹路径的字符串,使用通配符"*" lstrcpy(szFilePath, currentDir.c_str()); // 注释的代码可以用于查找所有以“.txt”结尾的文件 // lstrcat(szFilePath, "\\*.txt"); lstrcat(szFilePath, L"\\*"); // 查找第一个文件/目录,获得查找句柄 hListFile = FindFirstFile(szFilePath, &FindFileData); // 判断句柄 if(hListFile == INVALID_HANDLE_VALUE) { wsprintfW(&dbgInfo[0], L"BrowseFileToZip Error:%d\n", GetLastError()); OutputDebugStringW(dbgInfo.c_str()); return ; } else { do { //如果不想显示代表本级目录和上级目录的“.”和“..”, //可以使用注释部分的代码过滤 if(lstrcmp(FindFileData.cFileName, TEXT(".")) == 0 || lstrcmp(FindFileData.cFileName, TEXT("..")) == 0) { continue; } // 打印文件名、目录名 wsprintfW(&dbgInfo[0], L"%ws\t\t", FindFileData.cFileName); OutputDebugStringW(dbgInfo.c_str()); // 判断文件属性,是否为加密文件或者文件夹 if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED) { wsprintfW(&dbgInfo[0], L" "); OutputDebugStringW(dbgInfo.c_str()); } // 判断文件属性,是否为隐藏文件或文件夹 if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) { wsprintfW(&dbgInfo[0], L" "); OutputDebugStringW(dbgInfo.c_str()); } // 判断文件属性,是否为目录 if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { wsprintfW(&dbgInfo[0], L" "); OutputDebugStringW(dbgInfo.c_str()); //子文件夹的绝对路径 std::wstring absSubDir = currentDir; absSubDir.append(L"\\"); absSubDir.append(FindFileData.cFileName); //子文件夹的相对路径 std::wstring tSubDir = absSubDir.substr(targetDir.length() + 1, absSubDir.length() - targetDir.length()); //将文件夹添加到ZIP文件 ZipAddFolder(*pHZip,tSubDir.c_str()); //遍历子文件夹 BrowseFileToZip(targetDir, absSubDir, pHZip); } else //*不是目录的当文件处理 { //子文件的绝对路径 std::wstring absSubDir = currentDir; absSubDir.append(L"\\"); absSubDir.append(FindFileData.cFileName); //子文件的相对路径 std::wstring tSubDir = absSubDir.substr(targetDir.length() + 1, absSubDir.length() - targetDir.length()); ZipAdd(*pHZip,tSubDir.c_str(),absSubDir.c_str()); } // 读者可根据文件属性表中的内容自行添加、判断文件属性 wsprintfW(&dbgInfo[0], L"\n"); OutputDebugStringW(dbgInfo.c_str()); } while(FindNextFile(hListFile, &FindFileData)); } } bool ZipPack::UnPackZipToDir(std::wstring targetZip, std::wstring dstPath, std::string psw) { //用于输出调试信息 std::wstring dbgInfo; dbgInfo.resize(MAX_PATH); if(!::PathFileExistsW(targetZip.c_str()) || !PathFileExistsW(dstPath.c_str())) { wsprintfW(&dbgInfo[0], L"UnPackZipToPath failed, invalid file or dir"); OutputDebugStringW(dbgInfo.c_str()); return false; } HZIP hz = OpenZip(&targetZip[0], psw.c_str()); if(hz == NULL) { wsprintfW(&dbgInfo[0], L"UnPackZipToPath failed, open zip(%s) failed", targetZip.c_str()); OutputDebugStringW(dbgInfo.c_str()); return false; } bool bRtn; bRtn = true; ZRESULT zRtn; zRtn = SetUnzipBaseDir(hz, dstPath.c_str()); if(zRtn == ZR_OK) { ZIPENTRY ze; zRtn = GetZipItem(hz,-1,&ze); if(zRtn == ZR_OK) { int numitems = ze.index; for(int zi=0; zi


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3