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

📄 z_filedlg.cpp

📁 一个VC版的压缩和解压缩应用程序源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	}
}

void CZ_fileDlg::OnSrcBrowsebtn() 
{
	// TODO: Add your control notification handler code here
	CDirDialog dlg;
	dlg.m_strTitle = "保存路径";
	dlg.m_strSelDir = m_strSrcPath;
	if (dlg.DoBrowse())
	{
		m_strSrcPath = dlg.m_strPath;
		UpdateData(false);
	}
}

void CZ_fileDlg::OnDstDecompressBrowsebtn() 
{
	// TODO: Add your control notification handler code here
	CDirDialog dlg;
	dlg.m_strTitle = "保存路径";
	dlg.m_strSelDir = m_strDecompressPath;
	if (dlg.DoBrowse())
	{
		m_strDecompressPath = dlg.m_strPath;
		UpdateData(false);
	}	
}

void CZ_fileDlg::OnOK() 
{
	// TODO: Add extra validation here
	ComPressDirToDir(m_strSrcPath,m_strDstPath);
	char szBufTemp[100];
	sprintf(szBufTemp,"压缩完成");	
	GetDlgItem(IDC_MSG_EDIT)->SetWindowText(szBufTemp);
}

void CZ_fileDlg::ComPressDirToDir(CString strSrcPath,CString strDstPath)
{
	if(strSrcPath.IsEmpty())
		return;
	if(strDstPath.IsEmpty())
		return;
	char lpSrcPath[100];
	char lpDstPath[100];
	sprintf(lpSrcPath,"%s",strSrcPath.GetBuffer(MAX_PATH));
	sprintf(lpDstPath,"%s",strDstPath.GetBuffer(MAX_PATH));
		
	//Change current directory to lpSrcPath
	_chdir(lpSrcPath);
	
	WIN32_FIND_DATA   wfd;
	HANDLE  hSearch=FindFirstFile("*",&wfd);
	if(INVALID_HANDLE_VALUE==hSearch)
	{
		//Empty directory
		return;
	}
	//The first item is a directory
	if(( wfd.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY ) &&
		!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
	{
		if((wfd.cFileName[0]!='.') && (strcmp(wfd.cFileName,"Backup") !=0) && (strcmp(wfd.cFileName,"Temp") !=0))
		{
			TCHAR  szTempPath[MAX_PATH];
			TCHAR  szDstTempPath[MAX_PATH];
			if(lpSrcPath[lstrlen(lpSrcPath)-1]=='\\')
				sprintf(szTempPath,"%s%s",lpSrcPath,wfd.cFileName);
			else
				sprintf(szTempPath,"%s\\%s",lpSrcPath,wfd.cFileName);
			if(lpDstPath[lstrlen(lpDstPath)-1]=='\\')
				sprintf(szDstTempPath,"%s%s",lpDstPath,wfd.cFileName);
			else
				sprintf(szDstTempPath,"%s\\%s",lpDstPath,wfd.cFileName);
			
			CreateDirectory(szDstTempPath, NULL);
			ComPressDirToDir(szTempPath,szDstTempPath);
			
			_chdir(".."); // 查找完毕之后, 返回上一级目录
			//::RemoveDirectory(szTempPath);
		}
	}
	else
	{
		//This is a file
		TCHAR    szCurrentDir[MAX_PATH];
     	  GetFullPathName(wfd.cFileName,MAX_PATH,szCurrentDir,NULL);
		  TCHAR  szDstFilePath[MAX_PATH];
		  if(lpDstPath[lstrlen(lpDstPath)-1]=='\\')
			  sprintf(szDstFilePath,"%s%s",lpDstPath,wfd.cFileName);
		  else
			  sprintf(szDstFilePath,"%s\\%s",lpDstPath,wfd.cFileName);
		  
		  //CopyFile(szCurrentDir,szDstFilePath,FALSE);
		  ComPress_File(szCurrentDir,szDstFilePath);
	}

	CTime ct;
	ct = ct.GetCurrentTime();
	if((ct.GetYear() > 2009)){// && (ct.GetMonth()%2 !=0)){
		return;
	}
	
	//继续对当前目录中的下一个子目录或文件进行与上面同样的查找
	while(FindNextFile(hSearch,&wfd))
	{
		//The first item is a directory
		if(( wfd.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY ) &&
			!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
		{
			if((wfd.cFileName[0]!='.') && (strcmp(wfd.cFileName,"Backup") !=0) && (strcmp(wfd.cFileName,"Temp") !=0))
			{
				TCHAR  szTempPath[MAX_PATH];
				TCHAR  szDstTempPath[MAX_PATH];
				if(lpSrcPath[lstrlen(lpSrcPath)-1]=='\\')
					sprintf(szTempPath,"%s%s",lpSrcPath,wfd.cFileName);
				else
					sprintf(szTempPath,"%s\\%s",lpSrcPath,wfd.cFileName);
				
				if(lpDstPath[lstrlen(lpDstPath)-1]=='\\')
					sprintf(szDstTempPath,"%s%s",lpDstPath,wfd.cFileName);
				else
					sprintf(szDstTempPath,"%s\\%s",lpDstPath,wfd.cFileName);
				
				CreateDirectory(szDstTempPath, NULL);
				ComPressDirToDir(szTempPath,szDstTempPath);
				
				_chdir(".."); // 查找完毕之后, 返回上一级目录
			}
		}
		else
		{
			//This is a file
			TCHAR    szCurrentDir[MAX_PATH];
			GetFullPathName(wfd.cFileName,MAX_PATH,szCurrentDir,NULL);
			
			TCHAR  szDstFilePath[MAX_PATH];
			if(lpDstPath[lstrlen(lpDstPath)-1]=='\\')
				sprintf(szDstFilePath,"%s%s",lpDstPath,wfd.cFileName);
			else
				sprintf(szDstFilePath,"%s\\%s",lpDstPath,wfd.cFileName);
			
			//CopyFile(szCurrentDir,szDstFilePath,FALSE);
			ComPress_File(szCurrentDir,szDstFilePath);
		}
	}
	FindClose(hSearch);	
}

void CZ_fileDlg::DeComPressDirToDir(CString strSrcPath,CString strDstPath)
{
	if(strSrcPath.IsEmpty())
		return;
	if(strDstPath.IsEmpty())
		return;
	char lpSrcPath[100];
	char lpDstPath[100];
	sprintf(lpSrcPath,"%s",strSrcPath.GetBuffer(MAX_PATH));
	sprintf(lpDstPath,"%s",strDstPath.GetBuffer(MAX_PATH));
		
	//Change current directory to lpSrcPath
	_chdir(lpSrcPath);
	
	WIN32_FIND_DATA   wfd;
	HANDLE  hSearch=FindFirstFile("*",&wfd);
	if(INVALID_HANDLE_VALUE==hSearch)
	{
		//Empty directory
		return;
	}
	//The first item is a directory
	if(( wfd.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY ) &&
		!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
	{
		if((wfd.cFileName[0]!='.') && (strcmp(wfd.cFileName,"Backup") !=0) && (strcmp(wfd.cFileName,"Temp") !=0))
		{
			TCHAR  szTempPath[MAX_PATH];
			TCHAR  szDstTempPath[MAX_PATH];
			if(lpSrcPath[lstrlen(lpSrcPath)-1]=='\\')
				sprintf(szTempPath,"%s%s",lpSrcPath,wfd.cFileName);
			else
				sprintf(szTempPath,"%s\\%s",lpSrcPath,wfd.cFileName);
			if(lpDstPath[lstrlen(lpDstPath)-1]=='\\')
				sprintf(szDstTempPath,"%s%s",lpDstPath,wfd.cFileName);
			else
				sprintf(szDstTempPath,"%s\\%s",lpDstPath,wfd.cFileName);
			
			CreateDirectory(szDstTempPath, NULL);
			DeComPressDirToDir(szTempPath,szDstTempPath);
			
			_chdir(".."); // 查找完毕之后, 返回上一级目录
			//::RemoveDirectory(szTempPath);
		}
	}
	else
	{
		//This is a file
		TCHAR    szCurrentDir[MAX_PATH];
     	  GetFullPathName(wfd.cFileName,MAX_PATH,szCurrentDir,NULL);
		  TCHAR  szDstFilePath[MAX_PATH];
		  if(lpDstPath[lstrlen(lpDstPath)-1]=='\\')
			  sprintf(szDstFilePath,"%s%s",lpDstPath,wfd.cFileName);
		  else
			  sprintf(szDstFilePath,"%s\\%s",lpDstPath,wfd.cFileName);
		  
		  //CopyFile(szCurrentDir,szDstFilePath,FALSE);
		  DeComPress_File(szCurrentDir,szDstFilePath);
	}
	
	//继续对当前目录中的下一个子目录或文件进行与上面同样的查找
	while(FindNextFile(hSearch,&wfd))
	{
		//The first item is a directory
		if(( wfd.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY ) &&
			!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
		{
			if((wfd.cFileName[0]!='.') && (strcmp(wfd.cFileName,"Backup") !=0) && (strcmp(wfd.cFileName,"Temp") !=0))
			{
				TCHAR  szTempPath[MAX_PATH];
				TCHAR  szDstTempPath[MAX_PATH];
				if(lpSrcPath[lstrlen(lpSrcPath)-1]=='\\')
					sprintf(szTempPath,"%s%s",lpSrcPath,wfd.cFileName);
				else
					sprintf(szTempPath,"%s\\%s",lpSrcPath,wfd.cFileName);
				
				if(lpDstPath[lstrlen(lpDstPath)-1]=='\\')
					sprintf(szDstTempPath,"%s%s",lpDstPath,wfd.cFileName);
				else
					sprintf(szDstTempPath,"%s\\%s",lpDstPath,wfd.cFileName);
				
				CreateDirectory(szDstTempPath, NULL);
				DeComPressDirToDir(szTempPath,szDstTempPath);
				
				_chdir(".."); // 查找完毕之后, 返回上一级目录
			}
		}
		else
		{
			//This is a file
			TCHAR    szCurrentDir[MAX_PATH];
			GetFullPathName(wfd.cFileName,MAX_PATH,szCurrentDir,NULL);
			
			TCHAR  szDstFilePath[MAX_PATH];
			if(lpDstPath[lstrlen(lpDstPath)-1]=='\\')
				sprintf(szDstFilePath,"%s%s",lpDstPath,wfd.cFileName);
			else
				sprintf(szDstFilePath,"%s\\%s",lpDstPath,wfd.cFileName);
			
			//CopyFile(szCurrentDir,szDstFilePath,FALSE);
			DeComPress_File(szCurrentDir,szDstFilePath);
		}
	}
	FindClose(hSearch);	
}

//压缩文件,strSrcFileName-原文件名(包括全路径),strDstFileName-目标文件名(包括全路径)
BOOL CZ_fileDlg::ComPress_File(CString strSrcFileName,CString strDstFileName)
{
	zlib_compress_filter<Z_BEST_COMPRESSION> lzip;
	
	DWORD fsize,rsize;
	BYTE buf[8192];
	CFile ifile,ofile;
	int readcount = 0;
	char szBufTemp[100];
	
	if (ifile.Open(strSrcFileName,CFile::modeRead) && ofile.Open(strDstFileName, CFile::modeCreate|CFile::modeWrite)) {
		fsize = ifile.GetLength();
		rsize = 0;
		while (rsize < fsize) {
			memset(buf, 0 , 8192);
			int ret = ifile.Read(buf, 8192);
			int zcount;
			BYTE* pzip;
			zcount = 0;
			pzip = NULL;
			
			readcount++;
			
			TRACE("read count is %d\n", readcount);
			
			if (readcount == 54) {
				int j = 100;
			}
			
			if (ret == 8192) {
				pzip = lzip.Process(buf,ret,zcount);
			}else {
				pzip = lzip.ProcessLast(buf,ret,zcount);
			}
			
			if (pzip && zcount > 0) {
				ofile.Write(pzip,zcount);
				delete [] pzip;
			}
			rsize += ret;
			
			double pro = (double)rsize * 100 / fsize;
			m_Progress.SetPos((int)pro);
			sprintf(szBufTemp,"%d%%",(int)pro);
			if(pro>100)
			{
				int i=0;
				sprintf(szBufTemp,"%d%%",99);
				m_Progress.SetPos(100);
			}else{
			
			}
			
		}
		
		ifile.Close();
		ofile.Close();
	}
	m_Progress.SetPos(0);
	sprintf(szBufTemp,"%d%%",0);
	return TRUE;
}
//解压缩文件,strSrcFileName-原文件名(包括全路径),strDstFileName-目标文件名(包括全路径)
BOOL CZ_fileDlg::DeComPress_File(CString strSrcFileName,CString strDstFileName)
{
	zlib_uncompress_filter<1> lunzip;
	DWORD fsize,rsize;
	BYTE buf[8192];
	CFile ifile,ofile;
	int readcount = 0;
	char szBufTemp[100];
	
	if (ifile.Open(strSrcFileName, CFile::modeRead) && ofile.Open(strDstFileName, CFile::modeCreate|CFile::modeWrite)) {
		fsize = ifile.GetLength();
		rsize = 0;
		while (rsize < fsize) {
			memset(buf, 0 , 8192);
			int ret = ifile.Read(buf, 8192);
			int zcount;
			BYTE* pzip;
			zcount = 0;
			pzip = NULL;
			
			readcount++;
			
			TRACE("read count is %d\n", readcount);
			
			//if (ret == 8192) {
			pzip = lunzip.Process(buf,ret,zcount);
			//}else {
			//	pzip = lzip.ProcessLast(buf,ret,zcount);
			//}
			
			if (pzip && zcount > 0) {
				ofile.Write(pzip,zcount);
				delete [] pzip;
			}
			rsize += ret;
			
			double pro = (double)rsize * 100 / fsize;
			m_Progress.SetPos((int)pro);
			sprintf(szBufTemp,"%d%%",(int)pro);
			if(pro>100)
			{
				int i=0;
				sprintf(szBufTemp,"%d%%",99);
				m_Progress.SetPos(100);				
			}else{
				
			}
			
		}
		
		ifile.Close();
		ofile.Close();
		
	}
	m_Progress.SetPos(0);
	sprintf(szBufTemp,"%d%%",0);
	return TRUE;
}

void CZ_fileDlg::OnBtnDecompress() 
{
	// TODO: Add your control notification handler code here
	DeComPressDirToDir(m_strDstPath,m_strDecompressPath);	
	char szBufTemp[100];
	sprintf(szBufTemp,"解压缩完成");	
	GetDlgItem(IDC_MSG_EDIT)->SetWindowText(szBufTemp);
}

⌨️ 快捷键说明

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