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

📄 testzipdlgdlg.cpp

📁 wuinzup in the MFC way
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		wa.push_back((WORD)da[i]);
#else
		wa.Add((WORD)da[i]);
#endif

	try
	{
		m_zip.DeleteFiles(wa);
	}
	catch (CException* e)
	{
		e->Delete();
		m_bAfterException = true;
		
	}
	catch(...) // thrown in the STL version
	{
		m_bAfterException = true;		
	}
	if (m_bAfterException)
	{
		AfxMessageBox(_T("Delete failed. Closing the archive."), MB_ICONSTOP);
		OnArchiveClose();
	}

	Redisplay();	
	
}

void CTestZipDlgDlg::OnActionExtract() 
{
	CBrowseForFolder bf;	
	bf.hWndOwner = this->m_hWnd;
	bf.strTitle = _T("Select directory to extract files");
	CString sz;
	if (!bf.GetFolder(sz))
		return;
	CDWordArray da, ds;
	m_files.BuildSelectedArray(da);
	CProgressInfo p;
	p.m_pProgress = &m_progress;
	
	for (int i = 0; i < da.GetSize(); i++)
	{
		CZipFileHeader fh;
		m_zip.GetFileInfo(fh, (WORD)da[i]);
		p.m_iTotal += fh.m_uUncomprSize;
		ds.Add(fh.m_uUncomprSize);
	}
	m_progress.Init(p.m_iTotal);

	m_zip.SetPassword(m_options.m_szPassword);
	bool bErr = false;
	try
	{	
		for (int i = 0; i < da.GetSize(); i++)
		{
			m_zip.ExtractFile((WORD)da[i], sz, true, NULL, Callback, (void*) &p);
			p.m_iTotalSoFar += ds[i];
		}
	}
	catch (CException* e)
	{
		e->Delete();
		bErr = true;

	}
	catch(...) // thrown in the STL version
	{
		bErr = true;
	}
	if (bErr)
	{
		m_zip.CloseFile(NULL, true);
		AfxMessageBox(_T("Extract failed"), MB_ICONSTOP);
	}
	m_progress.Hide();	
}

void CTestZipDlgDlg::OnArchiveClose() 
{
	if (m_bCommentModified)
	{
		UpdateData();
		m_zip.SetGlobalComment(m_szComment);
		m_bCommentModified = false;
	}
	bool berr = false;
	try
	{
		m_zip.Close(m_bAfterException);	
	}
	catch (CException* e)
	{
		e->Delete();
		berr = true;
	}
	catch(...) // thrown in the STL version
	{
		berr = true;		
	}
	if (berr)
		AfxMessageBox(_T("Close failed"), MB_ICONSTOP);
	GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
	m_szComment.Empty();
	UpdateData(FALSE);
	Redisplay();
	if (m_bAfterException)
		m_bAfterException = false;
}

void CTestZipDlgDlg::OnArchiveExit() 
{
	EndDialog(IDOK);
	
}

void CTestZipDlgDlg::OnArchiveNew() 
{
	CString sz;
	if (!GetFileName(sz, false))
		return;
	OnArchiveClose();

	m_bCommentModified = false;
	m_szComment.Empty();
	UpdateData(FALSE);
	GetDlgItem(IDC_EDIT1)->EnableWindow();
	try
	{
		m_zip.Open(sz, m_options.m_iSpan == 0 ? CZipArchive::create : CZipArchive::createSpan,
			m_options.m_iSpan == 1 ? 0 : m_options.m_uVolumeSize);
	}
	catch (CException* e)
	{
		e->Delete();
		m_bAfterException = true;
	}
	catch(...) // thrown in the STL version
	{
		m_bAfterException = true;
	}
	if (m_bAfterException)
	{
		AfxMessageBox(_T("Create new failed"), MB_ICONSTOP);
		OnArchiveClose();
	}
	Redisplay();
	
}

void CTestZipDlgDlg::OnArchiveOpen() 
{
	CString sz;
	if (!GetFileName(sz, true))
		return;
	OnArchiveClose();
	int berr = 0;
	do 
	{
		try
		{
			m_zip.Open(sz, CZipArchive::open, m_options.m_bTdComp ? 1 : 0 );
			berr = 0;
		}
		catch (CZipException* e)
		{
			if (e->m_iCause == CZipException::cdirNotFound)
				berr = -1;
			else
				berr = 1;
			e->Delete();
		}
		catch (CException* e)
		{
			e->Delete();
			berr = 1;
			
		}
		// thrown in the STL version
		catch (CZipException e)
		{
			if (e.m_iCause == CZipException::cdirNotFound)
				berr = -1;
			else
				berr = 1;

		}
		catch(...) 
		{
			berr = 1;
		}
		if (berr == -1)
		{
			if (AfxMessageBox(_T("The central directory was not found. If you're opening a multi-disk archive, make sure you have inserted the last disk. Retry?"), MB_ICONSTOP|MB_YESNO) == IDNO)
				berr = 1;
			else
				m_zip.Close(true);
		}

		if (berr == 1)
		{
			AfxMessageBox(_T("Open failed"), MB_ICONSTOP);
			return;
		}

	} while (berr == -1);
	m_bCommentModified = false;
	m_szComment = m_zip.GetGlobalComment();
	UpdateData(FALSE);
	if (!m_zip.GetSpanMode())
		GetDlgItem(IDC_EDIT1)->EnableWindow();
	Redisplay();

	
}

void CTestZipDlgDlg::OnArchiveOptions() 
{
	COptionsDlg dlg;
	dlg.m_pOptions = &m_options;
	if (dlg.DoModal() == IDOK)
	{
		if (!m_options.m_szPassword.IsEmpty())
		{
			if (!m_zip.SetPassword(m_options.m_szPassword))
			{
				if (!m_zip.IsClosed(false))
					AfxMessageBox(IDS_STRING1, MB_ICONWARNING);
				else			
				{
					m_options.m_szPassword.Empty();
					AfxMessageBox(IDS_STRING2, MB_ICONWARNING);
				}
				
			}
		}
	}
	
}

void CTestZipDlgDlg::OnHelpAbout() 
{
	
	CAboutDlg dlg;
	dlg.DoModal();
	
}

void CTestZipDlgDlg::OnUpdateActionAdd(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(!m_zip.IsClosed() && (m_zip.GetSpanMode() >= 0));
}

void CTestZipDlgDlg::OnUpdateActionDelete(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(!m_zip.IsClosed() && !m_zip.GetSpanMode() && m_files.GetSelectedCount());
	
}

void CTestZipDlgDlg::OnUpdateActionExtract(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(!m_zip.IsClosed() && (m_zip.GetSpanMode() <= 0)&& m_files.GetSelectedCount());
	
}

void CTestZipDlgDlg::OnUpdateArchiveClose(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(!m_zip.IsClosed());
	
}

void CTestZipDlgDlg::OnChangeEdit1() 
{
	m_bCommentModified = true;	
}

void CTestZipDlgDlg::OnDblclkContents(NMHDR* , LRESULT* pResult) 
{
	if (m_zip.IsClosed() || (m_zip.GetSpanMode() < 0))
		return;
	int i = m_files.GetFirstSelectedItem();
	if (i == -1)
		return;
	CCommentDlg dlg;
	CZipFileHeader fh;
	m_zip.GetFileInfo(fh, (WORD)i);
	dlg.m_szComment = fh.GetComment();
	if (dlg.DoModal() == IDOK)
	{
		if (fh.GetComment().Collate(dlg.m_szComment))
		{
			m_zip.SetFileComment((WORD)i, dlg.m_szComment);
			Redisplay();
		}
	}

	*pResult = 0;
}

void CTestZipDlgDlg::UpdateMenu(CMenu *pMenu)
{
	CCmdUI cmd;
	cmd.m_nIndexMax = pMenu->GetMenuItemCount();
	cmd.m_pMenu = pMenu;
	int s = 0;
	for (UINT i = 0; i < cmd.m_nIndexMax; i++)
	{
		cmd.m_nIndex = i;
		MENUITEMINFO mi;
		mi.cbSize = sizeof(MENUITEMINFO);
		mi.fMask = MIIM_SUBMENU
#ifndef NO_CHECK_FOR_SEPARATOR
	#if(WINVER >= 0x0500)
				| MIIM_FTYPE
	#else
				| MIIM_TYPE
	#endif /* WINVER >= 0x0500 */
#endif //NO_CHECK_FOR_SEPARATOR
;
		if (!pMenu->GetMenuItemInfo(cmd.m_nIndex, &mi, TRUE))
			return;
#ifndef NO_CHECK_FOR_SEPARATOR
		if (mi.fType & MFT_SEPARATOR)
			continue;
#endif //NO_CHECK_FOR_SEPARATOR
		if (mi.hSubMenu)
			UpdateMenu(pMenu->GetSubMenu(s++));
		else
		{
			cmd.m_nID = pMenu->GetMenuItemID(i);
			OnCmdMsg(cmd.m_nID, CN_UPDATE_COMMAND_UI, &cmd, NULL);
		}
		
	}

}

void CTestZipDlgDlg::OnInitMenu(CMenu* pMenu) 
{
	CResizableDialog::OnInitMenu(pMenu);
	UpdateMenu(pMenu);	
}

void CTestZipDlgDlg::OnActionTest() 
{
	CDWordArray da;
	m_files.BuildSelectedArray(da);
	if (da.GetSize() == 0)
		return;
	m_zip.SetPassword(m_options.m_szPassword);
	CString szReport;
	CProgressInfo p;
	p.m_pProgress = &m_progress;
	
	for (int i = 0; i < da.GetSize(); i++)
	{
		CZipFileHeader fh;
		m_zip.GetFileInfo(fh, (WORD)da[i]);
		p.m_iTotal += fh.m_uUncomprSize;
	}
	m_progress.Init(p.m_iTotal);
	for (i = 0; i < da.GetSize(); i++)
	{
		WORD nIndex = (WORD)da[i];
		bool bTestOK = false;
		try
		{
			bTestOK = m_zip.TestFile(nIndex, Callback, (void*)&p);
		}
		catch(CException*e)
		{
			e->Delete();
		}
		catch(...) // thrown in the STL version
		{
			
		}
		CZipFileHeader fh;
		VERIFY(m_zip.GetFileInfo(fh, nIndex));
		p.m_iTotalSoFar += fh.m_uUncomprSize;
		szReport += fh.GetFileName();
		szReport += CString(_T("  -  ")) + (bTestOK ? _T("Passed") : _T("Failed"));
		szReport += _T("\r\n");
	}
	m_progress.Hide();
	CDlgTestReport dlg;
	dlg.m_pszData = &szReport;
	dlg.DoModal();
}


bool CTestZipDlgDlg::Callback(int , int iSoFar, void* pData)
{
	CProgressInfo* p = static_cast<CProgressInfo*>(pData);
	iSoFar += p->m_iTotalSoFar;
	//iTotal = p->m_iTotal;

	p->m_pProgress->SetPos(iSoFar);
	p->m_pProgress->RedrawWindow();
	return true;
}

⌨️ 快捷键说明

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