MFC下实现压缩和解压缩功能

您所在的位置:网站首页 怎样将rar文件解压成文件包 MFC下实现压缩和解压缩功能

MFC下实现压缩和解压缩功能

2024-07-17 12:38| 来源: 网络整理| 查看: 265

解压zip库(解决文件名中文乱码问题).zip,将zip.h,zip.cpp,unzip.h,unzip.cpp添加到你的项目下,下面是一个封装了简单压缩和解压的方法,注释已经很详细了,不再赘述。

递归压缩文件夹和文件

/*! * @brief 将一个文件夹下的文件添加到压缩文件 * * @param const HZIP& zip 压缩操作对象 * @param const CString& zipPath 被压缩的文件或文件夹路径 * @param const CString& filePath 压缩文件路径 * * @return bool 压缩成功返回true * * @author 刘杰达 * @date 2020年11月20日 */ bool _compressedFolder(const HZIP &zip, const CString &zipPath, const CString &filePath) { bool ret = false; //AfxMessageBox(_T("开始压缩文件夹内文件")); if(zip) { CFileFind finder; BOOL bWorking = finder.FindFile(zipPath + _T("\\*.*")); //AfxMessageBox(_T("压缩操作对象有效")); while(bWorking) { bWorking = finder.FindNextFile(); CString strPath = finder.GetFilePath(); if(finder.IsDirectory() && !finder.IsDots()) { // 文件夹递归调用 _compressedFolder(zip, finder.GetFilePath(), filePath); } else if(!finder.IsDirectory() && !finder.IsDots()) { // 文件添加到文件压缩文件 CString relatePath = _getRelateFolder(strPath, filePath); relatePath += finder.GetFileName(); ZipAdd(zip, relatePath, strPath); //ZipAdd(zip, finder.GetFileName(), strPath); //AfxMessageBox(_T("压入文件:") + strPath + _T(",相对路径:") + relatePath); } } } else { AfxMessageBox(_T("压缩对象无效")); } return ret; } /*! * @brief 创建压缩文件 * * @param const CString& zipPath 将要压缩的文件或文件夹路径 * @param const CString& filePath 压缩后生成压缩文件路径 * * @return bool 成功返回true * * @author 刘杰达 * @email [email protected] * @date 2020年11月19日 */ bool compressedFile(const CString& zipPath, const CString& filePath) { CFileFind find; if (!PathFileExists(zipPath)) return false; HZIP hz = CreateZip(filePath, 0); if (GetFileAttributes(zipPath) == FILE_ATTRIBUTE_DIRECTORY) _compressedFolder(hz, zipPath, filePath); else { CString fileName = zipPath.Right(zipPath.ReverseFind('\\')); ZipAdd(hz, fileName, zipPath); } CloseZip(hz); return true; }

解压缩

/*! * @brief 创建压缩文件,运行结果是把压缩文件解压到当前路径下 * * @param const CString& filePath 压缩文件路径 * * @return bool 成功返回true * * @author 刘杰达 * @date 2020年11月19日 */ bool Application::decompressionFile(const CString& filePath) { bool ret = false; CFileFind finder; if (finder.FindFile(filePath)) { HZIP hz = OpenZip(filePath, 0); ZIPENTRY ze; CString dir = finder.GetRoot(); GetZipItem(hz, -1, &ze); int numitems = ze.index; for (int zi = 0; zi < numitems; zi++) { ZIPENTRY ze; GetZipItem(hz, zi, &ze); UnzipItem(hz, zi, dir + _T("\\") + ze.name); } ret = true; CloseZip(hz); } return ret; }

获取文件相对于压缩文件的行对路径

/*! * @brief 获取文件相对于压缩文件的行对路径 * C:\\a\\b\\c.txt C:\\a\\d.zip 返回\\b * * @param const CString& compressedFile 被压缩文件路径 * @param const CString& zipFile 压缩文件路径 * * @date 2020年11月20日 */ CString _getRelateFolder(const CString &compressedFile, const CString &zipFile); CString Application::_getRelateFolder(const CString &compressedFile, const CString &zipFile) { CString result = _T(""); CFileFind compressedFileFind, zipFileFind; if(compressedFileFind.FindFile(compressedFile) && zipFileFind.FindFile(zipFile)) { CString compressedDir = compressedFileFind.GetRoot(); CString zipDir = zipFileFind.GetRoot(); compressedDir.Replace(zipDir, _T("")); result = compressedDir; } return result; }



【本文地址】


今日新闻


推荐新闻


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