⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ziputil_cpp.cpp

📁 文件压缩/解压动态链接库
💻 CPP
字号:
#include "stdafx.h"
#include "ZipUtil_CPP.h"

bool ZipCallBack(int iNumber, int iCode, void *pData)
{
// iNumber  - disk number needed
// iCode :
//		-1 - disk needed for reading
// other codes occurs during writting
//		>=0 : number of bytes needed
// 		-2 - the file with the archive name already exists on the disk
//		-3 - the disk is probably write - protected
//		-4 - couldn't create a file
	CString sz;
	switch(iCode){
	case -2:
		if ( iNumber == 1 ){ return 1; }
		if ( AfxGetMainWnd()->MessageBox("文件已存在,替换它吗?","提示",MB_ICONQUESTION | MB_DEFBUTTON2 | MB_YESNO) == IDNO ){ return false; }
		break;
	case -3:
		if ( AfxGetMainWnd()->MessageBox("磁盘写保护,换盘重试?","提示",MB_ICONQUESTION | MB_DEFBUTTON2 | MB_YESNO) == IDNO ){ return false; }
		break;
	case -4:
		AfxGetMainWnd()->MessageBox("创建文件失败!","错误",MB_ICONSTOP); return 0;
		break;
	default:
		if ( iNumber > 1 ){
			sz.Format("请插入第 %d 张盘",iNumber);
			if ( AfxGetMainWnd()->MessageBox(sz,"提示",MB_ICONQUESTION | MB_DEFBUTTON2 | MB_YESNO) == IDNO ){ return false; }
		}
	}
	return 1;
}


ZipUtil_CPP::ZipUtil_CPP()
{
	zipLevel=9;
}
//##ModelId=3C185C0A0041
void ZipUtil_CPP::setZipLevel(int aZipLevel)
{
	zipLevel=aZipLevel;
}

//##ModelId=3C185C1A026B
int ZipUtil_CPP::getZipLevel()
{
	return zipLevel;
}

//##ModelId=3C185C250325
int ZipUtil_CPP::zip(LPCTSTR srcFilePath, LPCTSTR desFilePath)
{
	CString srcFile= srcFilePath;
	CString destFile= desFilePath;

	try
	{
		m_zip.Open(destFile, CZipArchive::create,0 );
	}
	catch (CException* e)
	{
		e->Delete();

		#ifdef _DEBUG
		afxDump << "zip " << desFilePath << " 创建压缩文件产生失败\n";
		#endif

		m_zip.Close();
		return 1;
	}	
	m_zip.AddNewFile (srcFile,zipLevel,false);
	m_zip.Close();

	return 0;
}

//##ModelId=3C185C820383
int ZipUtil_CPP::unzip(LPCTSTR srcFilePath, LPCTSTR desPath, bool bFullPath,LPCTSTR desFileName)
{
	try
	{
		m_zip.Open(srcFilePath, CZipArchive::open,0 );
	}
	catch (CException *e)
	{
		e->Delete();//e->ReportError(MB_OK,0);

		#ifdef _DEBUG
		afxDump << "unzip " << srcFilePath << " 打开压缩文件失败\n";
		#endif

		m_zip.Close();
		return 1;
	}
	//
	WORD wIndex;
	CString szdesFileName = desFileName;

	try
	{
		if ( szdesFileName.IsEmpty() ){
			for ( wIndex = 0; wIndex < m_zip.GetNoEntries(); wIndex++){
				if ( !m_zip.ExtractFile(wIndex,desPath,bFullPath,NULL,NULL,NULL) ){
					m_zip.Close();
					return 1;
				}
			}
		}
		else{
			m_zip.ExtractFile((WORD)(0),desPath,bFullPath,szdesFileName,NULL,NULL);
		}
		m_zip.Close();
	}
	catch (CException *e)
	{
		//e->ReportError(MB_OK,0);
		e->Delete();

		#ifdef _DEBUG
		afxDump << "unzip " << srcFilePath << " 打开压缩文件失败\n";
		#endif

		m_zip.Close();
		return 1;
	}
	//
	//m_zip.ExtractFile((WORD)(0),desPath,bFullPath,desFileName,NULL,NULL);
	//m_zip.Close();
	//
	return 0;
}


BOOL ZipUtil_CPP::CreateArchive(LPCSTR lpszArchivePathName,BOOL bSpan/*=false*/)
{
	CString szArchivePathName= lpszArchivePathName;

	try
	{
		if ( bSpan ){
			m_zip.SetSpanCallback((ZIPCALLBACKFUN)ZipCallBack);
			m_zip.Open(szArchivePathName, CZipArchive::createSpan/*create*/,0 );
		}
		else {
			m_zip.Open(szArchivePathName, CZipArchive::create,0 );
		}
	}
	catch (CException* e)
	{

		#ifdef _DEBUG
		afxDump << "zip " << szArchivePathName << " 创建压缩文件产生失败\n" << e;
		#endif

		e->Delete();

		m_zip.Close();
		return FALSE;
	}	
	return TRUE;
}

BOOL ZipUtil_CPP::AddNewFile(LPCSTR lpszFilePathName,bool bFullPath)
{
	return m_zip.AddNewFile (lpszFilePathName,zipLevel,bFullPath);
}

void ZipUtil_CPP::CloseArchive()
{
	m_zip.Close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -