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

解压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 991516973@qq.com
     * @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;
}

很经典的MFC教程。 目 录 译者序 前言 第一部分 基础知识 第1章 窗口 2 1.1 窗口API环境 2 1.1.1 三种类型窗口 2 1.1.2 客户区非客户区 3 1.2 窗口MFC环境 4 1.3 怎样应用MFC创建一个窗口 5 1.4 怎样使用MFC销毁一个窗口 9 1.4.1 捆绑到一个已有的窗口 9 1.4.2 窗口类 10 1.4.3 窗口进程 10 1.5 怎样使用MFC创建一个窗口类 11 1.5.1 使用AfxRegisterWndClass () 函数注册一个窗口类 11 1.5.2 使用AfxRegisterClass ()函数 创建一个窗口类 12 1.6 怎样销毁一个MFC窗口类 14 1.7 厂商安装的窗口类 14 1.8 其他类型窗口 15 1.9 桌面窗口 16 1.10 小结 16 第2章 类 18 2.1 基类 18 2.1.1 CObject 18 2.1.2 CCmdTarget 19 2.1.3 CWnd 19 2.2 应用程序、框架、文档视图类 19 2.2.1 CWinApp(O/C/W) 20 2.2.2 CView (O/C/W) 21 2.3 其他用户界面类 22 2.3.1 通用控件类 23 2.3.2 菜单类 23 2.3.3 对话框类 24 2.3.4 控制条类 24 2.3.5 属性类 25 2.4 绘图类 25 2.4.1 设备环境类 25 2.4.2 图形对象类 25 2.5 文件类 26 2.6 数据库类 26 2.6.1 ODBC类 26 2.6.2 DAO类 27 2.7 数据集类 27 2.8 其他数据类 27 2.9 通信类 28 2.10 其他类 29 2.11 小结 31 第3章 消息处理 32 3.1 发送或寄送一个消息 32 3.1.1 发送一个消息 32 3.1.2 寄送一个消息 32 3.1.3 发送一个消息与寄送一个消息 的比较 32 3.2 怎样使用MFC发送一个消息 33 3.3 怎样用MFC寄送一个消息 33 3.4 三种类型的消息 34 3.4.1 窗口消息 34 3.4.2 命令消息 34 3.4.3 控件通知 34 3.5 MFC怎样接收一个寄送的消息 36 3.6 MFC怎样处理一个接收到的消息 36 3.7 处理用户界面的对象 44 3.8 创建自定义窗口消息 45 3.8.1 静态分配的窗口消息 45 3.8.2 动态分配的窗口消息 46 3.9 重定向消息 47 3.9.1 子分类超分类 47 3.9.2 用MFC子分类窗口 48 3.9.3 重载OnCmdMsg ( ) 49 3.9.4 使用SetWindowsHookEx ( ) 49 3.9.5 使用SetCapture ( ) 49 3.9.6 专有的消息泵 50 3.10 小结 50 第4章 绘图 51 4.1 设备环境 51 4.2 在MFC环境中创建一个设备环境 52 4.2.1 屏幕 52 4.2.2 打印机 53 4.2.3 内存 54 4.2.4 信息 54 4.3 绘图例程 55 4.3.1 画点 55 4.3.2 画线 55 4.3.3 画形状 55 4.3.4 形状填充翻转 55 4.3.5 滚动 56 4.3.6 绘制文本 56 4.3.7 绘制位图图标 56 4.4 绘图属性 56 4.4.1 设备环境属性 57 4.4.2 画线属性 58 4.4.3 形状填充属性 58 4.4.4 文本绘制属性 58 4.4.5 映像模式 59 4.4.6 调色板属性 62 4.4.7 混合属性 62 4.4.8 剪裁属性 63 4.4.9 位图绘制属性 64 4.5 元文件路径 65 4.5.1 元文件 65 4.5.2 路径 66 4.6 颜色调色板 66 4.6.1 抖动色 67 4.6.2 未经抖动色 67 4.6.3 系统调色板 67 4.6.4 使用系统调色板 68 4.6.5 动画色 71 4.7 控制什么时候在哪里绘图 71 4.7.1 处理WM_PAINT 71 4.7.2 只绘制被无效化的区域 72 4.7.3
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值