只能压缩解压zip格式的,不需要dll或者库文件,核心是HZIP,支持带密码压缩解压(但是有时不完美,属于HZIP本身的问题,请不要纠结这一点)。
完整源代码(VS2010编译通过)下载地址:
以下为核心封装文件,建议大家下载源代码查看,下载地址在本文末尾。
/
// 文件名: <ZipImplement.h>
// 说明:压缩解压缩文件夹
/
#pragma once
//#include "stdafx.h"
#include <atlconv.h>
#include "Zip.h"
#include "Unzip.h"
class CZipImplement
{
public:
CZipImplement(void);
~CZipImplement(void);
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, CStringA strPW = "");
//解压缩文件夹接口
BOOL Zip_UnPackFiles(CString &mZipFileFullPath, CString& mUnPackPath, CStringA strPW = "");
public:
//静态方法提供文件夹路径检查
static BOOL FolderExist(CString& strPath);
};
/
// 文件名: <ZipImplement.cpp>
// 说明:压缩解压缩文件夹
/
#include "StdAfx.h"
#include "zipimplement.h"
#include <direct.h>
#include <vector>
#include <xstring>
CZipImplement::CZipImplement(void)
{
}
CZipImplement::~CZipImplement(void)
{
}
/
// 函数说明: 实现压缩文件夹操作
// 参数说明: [in]:
// pFilePath 要被压缩的文件夹
// mZipFileFullPath 压缩后的文件名和路径
// strPW 压缩密码,可空,默认为空,表示不带密码压缩
// 返回值: 参数有误的情况下返回FALSE,压缩成功后返回TRUE
/
BOOL CZipImplement::Zip_PackFiles(CString& pFilePath, CString& mZipFileFullPath, CStringA strPW)
{
//参数错误
if ((pFilePath == L"") || (mZipFileFullPath == L""))
{
//路径异常返回
return FALSE ;
}
if(!CZipImplement::FolderExist(pFilePath))
{
//要被压缩的文件夹不存在
return FALSE ;
}
CString tZipFilePath = mZipFileFullPath.Left(mZipFileFullPath.ReverseFind('\\') + 1);
if(!CZipImplement::FolderExist(tZipFilePath))
{
//ZIP文件存放的文件夹不存在创建它
CStringW strWZipFilePath(tZipFilePath);
if (FALSE == CreatedMultipleDirectory((LPWSTR)(LPCWSTR)strWZipFilePath))
{
//创建目录失败
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文件
if (strPW.IsEmpty())
{
hz=CreateZip(mZipFileFullPath,0);
}
else
{
hz=CreateZip(mZipFileFullPath,(LPCSTR)strPW);
}
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文件的路径
// strPW 解压密码,可空,默认为空,表示压缩包没有密码
/
BOOL CZipImplement::Zip_UnPackFiles(CString &mZipFileFullPath, CString& mUnPackPath, CStringA strPW)
{
//参数错误
if ((mUnPackPath.IsEmpty()) || (mZipFileFullPath.IsEmpty()))
{
//路径异常返回
return FALSE ;
}
CFileFind tFFind;
if (!tFFind.FindFile(mZipFileFullPath))
{
//压缩失败(未发现压缩文件)
return FALSE;
}
//如果解压缩的路径不存在 试图创建它
CString tZipFilePath = mUnPackPath;
if(!CZipImplement::FolderExist(tZipFilePath))
{
//解压后存放的文件夹不存在 创建它
CStringW strWZipFilePath(tZipFilePath);
if (FALSE == CreatedMultipleDirectory((LPWSTR)(LPCWSTR)strWZipFilePath))
{
//创建目录失败
return FALSE;
}
}
/************************************************************************/
//打开ZIP文件
if (strPW.IsEmpty())
{
hz=OpenZip(mZipFileFullPath,0);
}
else
{
hz=OpenZip(mZipFileFullPath,(LPCSTR)strPW);
}
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 CZipImplement::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 CZipImplement::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 CZipImplement::GetRelativePath(CString& pFullPath,CString& pSubString)
{
pSubString = pFullPath.Right(pFullPath.GetLength() - this->m_FolderPath.GetLength() + this->m_FolderName.GetLength());
}
/
// 函数说明: 创建多级目录
// 参数说明: [in]: 路径字符串
// 返回值: BOOL 成功True 失败False
/
BOOL CZipImplement::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++)
{
USES_CONVERSION;
bSuccess = CreateDirectoryA( W2A(vIter->c_str()), NULL ) ? true :false;
}
return bSuccess;
}
下面为Demo核心代码
// ZipDemoDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "ZipDemo.h"
#include "ZipDemoDlg.h"
#include "afxdialogex.h"
#include "zip\ZipImplement.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CZipDemoDlg 对话框
CZipDemoDlg::CZipDemoDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CZipDemoDlg::IDD, pParent)
, m_strDirPath(_T(""))
, m_strZipFilePath(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CZipDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_strDirPath);
DDX_Text(pDX, IDC_EDIT2, m_strZipFilePath);
}
BEGIN_MESSAGE_MAP(CZipDemoDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_COMPRESS, &CZipDemoDlg::OnBnClickedBtnCompress)
ON_BN_CLICKED(IDC_BTN_DECOMPRESS, &CZipDemoDlg::OnBnClickedBtnDecompress)
END_MESSAGE_MAP()
void CZipDemoDlg::OnBnClickedBtnCompress()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData();
if (m_strDirPath.IsEmpty())
{
AfxMessageBox(_T("要压缩的文件目录不能为空!"));
return;
}
else if (!PathFileExists(m_strDirPath))
{
AfxMessageBox(_T("要压缩的文件目录不存在,请重新填写!"));
return;
}
else
{
CFileDialog dlg(FALSE,_T("zip"),m_strDirPath + _T(".zip"),OFN_OVERWRITEPROMPT,_T("zip(*.zip)|*.zip||"));
if (dlg.DoModal() == IDOK)
{
CString strZipFilePath = dlg.GetPathName();
CZipImplement compress;
compress.Zip_PackFiles(m_strDirPath,strZipFilePath,"123456789");//只是个Demo就不开线程去做了
MessageBox(_T("压缩完成!"),_T("完成"),MB_ICONINFORMATION);
}
}
}
void CZipDemoDlg::OnBnClickedBtnDecompress()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData();
if (m_strZipFilePath.IsEmpty())
{
AfxMessageBox(_T("请先填写要解压的文件!"));
return;
}
else if (!PathFileExists(m_strZipFilePath))
{
AfxMessageBox(_T("要解压的文件不存在,请重新填写!"));
return;
}
else
{
CString strDeOutputPath = GetFolderPath();
if (!strDeOutputPath.IsEmpty())
{
CZipImplement compress;
compress.Zip_UnPackFiles(m_strZipFilePath,strDeOutputPath,"123456789");//只是个Demo就不开线程去做了
MessageBox(_T("解压完成!"),_T("完成"),MB_ICONINFORMATION);
}
}
}
CString CZipDemoDlg::GetFolderPath(void)
{
CString str;
CFolderPickerDialog fd(NULL,0,this,0);
if(fd.DoModal() == IDOK)
{
str = fd.GetFolderPath();
}
return str;
}
Demo界面如下:
完整源代码(VS2010编译通过)下载地址:
http://download.csdn.net/detail/sunflover454/9167735