关于C++解压和压缩

先去下载zip的学习文件http://download.csdn.net/detail/microsoftvisul/8087681

1、先新建一个项目,加入zip和unzip类。

2、新建一个类名字自己取,用来解压和压缩使用。我使用CMyzip。

头文件myzip.h:

#pragma once

#include "zip.h" 
#include "unzip.h"

class CMyzip
{
public:
	CMyzip();
	~CMyzip();
private:
	HZIP hz;          //Zip文件句柄 
	ZRESULT zr;    //操作返回值 
	ZIPENTRY ze;  //Zip文件入口 
	CString m_FolderPath;     //folder路径 
	CString  m_FolderName;  //folder将要被压缩的文件夹名 
private:
	//实现遍历文件夹 
	void BrowseFile(CString &strFile);

	//获取相对路径 
	void GetRelativePath(CString& pFullPath, CString& pSubString);

	//创建路径 
	BOOL CreatedMultipleDirectory(wchar_t* direct); 

public:
	//压缩文件夹接口 
	BOOL Zip_PackFiles(CString& pFilePath, CString& mZipFileFullPath);

	//解压缩文件夹接口 
	BOOL Zip_UnPackFiles(CString &mZipFileFullPath, CString& mUnPackPath);

public:
	//静态方法提供文件夹路径检查 
	static BOOL FolderExist(CString& strPath);


};
源文件myzip.cpp:

#include "stdafx.h"
#include "myzip.h"

#include <direct.h> 
#include <vector>
#include <xstring>

CMyzip::CMyzip()
{
}


CMyzip::~CMyzip()
{
}

/ 
// 函数说明: 实现压缩文件夹操作 
// 参数说明: [in]: pFilePath         要被压缩的文件夹 
//                         mZipFileFullPath  压缩后的文件名和路径 
// 返回值: 参数有误的情况下返回FALSE,压缩成功后返回TRUE 
 
/ 
BOOL CMyzip::Zip_PackFiles(CString& pFilePath, CString& mZipFileFullPath)
{
	//参数错误 
	if ((pFilePath == L"") || (mZipFileFullPath == L""))
	{
		//路径异常返回 
		return FALSE;
	}

	if (!CMyzip::FolderExist(pFilePath))
	{
		//要被压缩的文件夹不存在 
		return FALSE;
	}

	CString tZipFilePath = mZipFileFullPath.Left(mZipFileFullPath.ReverseFind('\\') + 1);
	if (!CMyzip::FolderExist(tZipFilePath))
	{
		//ZIP文件存放的文件夹不存在创建它 
		wchar_t* temp = tZipFilePath.GetBuffer(tZipFilePath.GetLength());
		if (FALSE == CreatedMultipleDirectory(temp))
		{
			//创建目录失败 
			return FALSE;
		}
	}

	//获得文件夹的名字 
	if (pFilePath.Right(1) == L"\\")
	{
		this->m_FolderPath = pFilePath.Left(pFilePath.GetLength() - 1);
		m_FolderName = m_FolderPath.Right(m_FolderPath.GetLength() - m_FolderPath.ReverseFind('\\') - 1);
	}
	else
	{
		this->m_FolderPath = pFilePath;
		m_FolderName = pFilePath.Right(pFilePath.GetLength() - pFilePath.ReverseFind('\\') - 1);
	}

	/************************************************************************/

	//创建ZIP文件 
	hz = CreateZip(mZipFileFullPath, 0);
	if (hz == 0)
	{
		//创建Zip文件失败 
		return FALSE;
	}

	//递归文件夹,将获取的问价加入ZIP文件 
	BrowseFile(pFilePath);
	//关闭ZIP文件 
	CloseZip(hz);

	/************************************************************************/

	CFileFind tFFind;
	if (!tFFind.FindFile(mZipFileFullPath))
	{
		//压缩失败(未发现压缩后的文件) 
		return FALSE;
	}

	return TRUE;
}

/ 
// 函数说明: 解压缩文件夹 
// 参数说明: [in]: mUnPackPath     解压后文件存放的路径 
//                mZipFileFullPath  ZIP文件的路径 
// 返回值: 


/ 
BOOL CMyzip::Zip_UnPackFiles(CString &mZipFileFullPath, CString& mUnPackPath)
{
	//参数错误 
	if ((mUnPackPath == L"") || (mZipFileFullPath == L""))
	{
		//路径异常返回 
		return FALSE;
	}

	CFileFind tFFind;
	if (!tFFind.FindFile(mZipFileFullPath))
	{
		//压缩失败(未发现压缩文件) 
		return FALSE;
	}

	//如果解压缩的路径不存在 试图创建它 
	CString tZipFilePath = mUnPackPath;
	if (!CMyzip::FolderExist(tZipFilePath))
	{
		//解压后存放的文件夹不存在 创建它 
		wchar_t* temp = tZipFilePath.GetBuffer(tZipFilePath.GetLength());
		if (FALSE == CreatedMultipleDirectory(temp))
		{
			//创建目录失败 
			return FALSE;
		}
	}
	/************************************************************************/
	//打开ZIP文件 
	hz = OpenZip(mZipFileFullPath, 0);
	if (hz == 0)
	{
		//打开Zip文件失败 
		return FALSE;
	}

	zr = SetUnzipBaseDir(hz, mUnPackPath);
	if (zr != ZR_OK)
	{
		//打开Zip文件失败 
		CloseZip(hz);
		return FALSE;
	}

	zr = GetZipItem(hz, -1, &ze);
	if (zr != ZR_OK)
	{
		//获取Zip文件内容失败 
		CloseZip(hz);
		return FALSE;
	}

	int numitems = ze.index;
	for (int i = 0; i<numitems; i++)
	{
		zr = GetZipItem(hz, i, &ze);
		zr = UnzipItem(hz, i, ze.name);

		if (zr != ZR_OK)
		{
			//获取Zip文件内容失败 
			CloseZip(hz);
			return FALSE;
		}
	}

	CloseZip(hz);
	return TRUE;
}

/ 
// 函数说明: 检查指定的文件夹是否存在 
// 参数说明: [in]:strPath 检查的文件夹 (此方法会主动向路径末尾添加*.*) 
// 返回值:BOOL类型,存在返回TRUE,否则为FALSE 
 
/ 
BOOL CMyzip::FolderExist(CString& strPath)
{
	CString sCheckPath = strPath;

	if (sCheckPath.Right(1) != L"\\")
		sCheckPath += L"\\";

	sCheckPath += L"*.*";

	WIN32_FIND_DATA wfd;
	BOOL rValue = FALSE;

	HANDLE hFind = FindFirstFile(sCheckPath, &wfd);

	if ((hFind != INVALID_HANDLE_VALUE) &&
		(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) || (wfd.dwFileAttributes&FILE_ATTRIBUTE_ARCHIVE))
	{
		//如果存在并类型是文件夹 
		rValue = TRUE;
	}

	FindClose(hFind);
	return rValue;
}

/ 
// 函数说明: 遍历文件夹 
// 参数说明: [in]:strFile 遍历的文件夹(此方法会主动向路径末尾添加*.*) 
// 返回值:BOOL类型,存在返回TRUE,否则为FALSE 
 
/ 
void CMyzip::BrowseFile(CString &strFile)
{
	CFileFind ff;
	CString szDir = strFile;

	if (szDir.Right(1) != L"\\")
		szDir += L"\\";

	szDir += L"*.*";

	BOOL res = ff.FindFile(szDir);
	while (res)
	{
		res = ff.FindNextFile();
		if (ff.IsDirectory() && !ff.IsDots())
		{
			//如果是一个子目录,用递归继续往深一层找 
			CString strPath = ff.GetFilePath();

			CString subPath;
			GetRelativePath(strPath, subPath);
			//将文件添加到ZIP文件 
			ZipAddFolder(hz, subPath);
			BrowseFile(strPath);
		}
		else if (!ff.IsDirectory() && !ff.IsDots())
		{
			//显示当前访问的文件(完整路径) 
			CString strPath = ff.GetFilePath();
			CString subPath;

			GetRelativePath(strPath, subPath);
			//将文件添加到ZIP文件 
			ZipAdd(hz, subPath, strPath);
		}
	}

	//关闭 
	ff.Close();
}

/ 
// 函数说明: 获取相对路径 
// 参数说明: [in]:pFullPath 当前文件的完整路径 [out] : 解析后的相对路径 

/ 
void CMyzip::GetRelativePath(CString& pFullPath, CString& pSubString)
{
	pSubString = pFullPath.Right(pFullPath.GetLength() - this->m_FolderPath.GetLength() + this->m_FolderName.GetLength());
}

/ 
// 函数说明: 创建多级目录 
// 参数说明: [in]: 路径字符串 
// 返回值: BOOL 成功True 失败False 
/ 
BOOL CMyzip::CreatedMultipleDirectory(wchar_t* direct)
{
	std::wstring Directoryname = direct;

	if (Directoryname[Directoryname.length() - 1] != '\\')
	{
		Directoryname.append(1, '\\');
	}
	std::vector< std::wstring> vpath;
	std::wstring strtemp;
	BOOL  bSuccess = FALSE;
	for (int i = 0; i < Directoryname.length(); i++)
	{
		if (Directoryname[i] != '\\')
		{
			strtemp.append(1, Directoryname[i]);
		}
		else
		{
			vpath.push_back(strtemp);
			strtemp.append(1, '\\');
		}
	}
	std::vector<std::wstring>::const_iterator vIter;
	for (vIter = vpath.begin(); vIter != vpath.end(); vIter++)
	{
		bSuccess = CreateDirectory(vIter->c_str(), NULL) ? TRUE : FALSE;
	}

	return bSuccess;
}



  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值