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

📄 filespltdlg.cpp

📁 一个小的文件分割器。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		    m_path = m_targetpath + m_filename;
		else
			m_path = m_targetpath + _T("\\") + m_filename;
		//create target file
		if (!destFile.Open(m_path,  
			CFile::modeWrite		| 
			CFile::shareExclusive	| 
			CFile::typeBinary		| 
			CFile::modeCreate, &ex)) {
			TCHAR szError[1024];
			ex.GetErrorMessage(szError, 1024);
			::AfxMessageBox(szError);
			return 0;
		}
	}
	else if(m_path.IsEmpty())
	{//souce is not there
		MessageBox(_T("请选择待合并的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION);
		return 1;
	}
	if (m_targetpath.IsEmpty()) {//target is not there
		MessageBox(_T("请选择合并后要保存到的目标文件夹."), _T("文件分割器"), MB_ICONEXCLAMATION);
		return 1;
	}
	//do merge 
	do {
		//constuct a new name by dynamicly incrementing prefix
		pref = _ltoa(l, buff, 10);
		pref += _T("_");
		//open file with new name
		if (!m_SourceFile.Open(newpath + pref + m_filename,  
			CFile::modeRead		| 
			CFile::shareExclusive	| 
			CFile::typeBinary, &ex)) {
			TCHAR szError[1024];
			ex.GetErrorMessage(szError, 1024);
			destFile.Close();
			m_path = _T("");
			m_filename = _T("");
//			pProgress.SetPos(0);
			newpath = _T("");
//			m_parts = _T("");
			UpdateData(FALSE);
			//return OK because this f_n is aborting the loop if name is not found
			return 0;
		}
		else
			//constuct a new name 
			name = _T(newpath + pref + m_filename);
		do  {//write into file while it size < than 1.4 MB
			dwRead = m_SourceFile.Read(buffer, nCount);
			destFile.Write(buffer, dwRead);
		}
		//while we can read from source file
		while (dwRead > 0);

		m_SourceFile.Close();

		// Set the range to be 0 to 500.
		pProgress.SetRange(0, 500);
		// Set the position
		for (int i = 0; i < 500; i++)
			pProgress.SetPos(i);
		m_parts = _ltoa(l, buff, 10);
		m_parts += _T("个文件已合并");
		UpdateData(FALSE);
		l++;
		UpdateWindow();
	}
	while (l < 500);//little bit dirty solution, but you can always improve it!...
		
	return 0;
}

//文件分割涵数
int CFileSpltDlg::SplitMe() 
{
	CWaitCursor wait;
	// constructing these file objects
	CFile destFile;
	// we'll use a CFileException object to get error information
	CFileException ex;

	DWORD dwRead;
	UINT newlen;
	char buff [20];
	char b [20];
	long l = 1;
	CString name;
	UINT len = 0;
//	CGradientProgressCtrl *pProgress = (CProgressCtrl*) GetDlgItem(IDC_PROGRESS);

	UpdateData(TRUE);

	//获取文件分割后的大小,定义相对应变量数值
    newlen=GetSplitFileSize();
	UINT nCount = newlen/10;
    BYTE buffer[140000];

	//open file for read
	//m_path contain the file path
	if (!m_path.IsEmpty()) {
		if (!m_SourceFile.Open(m_path, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary, &ex)) {
			TCHAR szError[1024];
			ex.GetErrorMessage(szError, 1024);
      		::AfxMessageBox(szError);
			m_edit.SetFocus();
			m_edit.SetSel(0, -1);
			return 1;
		}
		//get file length
		len = m_SourceFile.GetLength();
	}
	//too lazy to put all "hard coded" strings in string table
	else {
		MessageBox(_T("请选择待分割的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION);
		return 1;
	}
	if (m_targetpath.IsEmpty()) {
		MessageBox(_T("请选择分割后保存到的目标文件夹."), _T("文件分割器"), MB_ICONEXCLAMATION);
		return 1;
	}
	//quick and dirty check for file size
	if (len < newlen) {
		CString length = _itoa(len, b, 10);
		MessageBox(_T("文件长度为 " + length + " 字节,不够指定的分割大小, 没有必要再进行分割."), _T("文件分割器"), MB_ICONEXCLAMATION);
		m_SourceFile.Close();
		m_path = _T("");
		m_filename = _T("");
		UpdateData(FALSE);
		return 1;
	}

	//do split
	do {
		//constuct a new name dynamicly changing prefix
		name = _ltoa(l, buff, 10);
		name += _T("_");
		CString newpath; 

		//判断选择目录未尾是否已有"\"符
		if(m_targetpath.Right(1)=='\\')
			newpath = m_targetpath;
		else
			newpath = m_targetpath + _T("\\");
		if (!destFile.Open(newpath + name + m_SourceFile.GetFileName(),  
			CFile::modeWrite		| 
			CFile::shareExclusive	| 
			CFile::typeBinary		|
			CFile::modeCreate, &ex)) {
			TCHAR szError[1024];
			ex.GetErrorMessage(szError, 1024);
			::AfxMessageBox(szError);
			m_SourceFile.Close();
			return 1;
		}
		do {
			dwRead = m_SourceFile.Read(buffer, nCount);
			destFile.Write(buffer, dwRead);
		}//while size is less than 1.4 MB
		while (dwRead > 0 && destFile.GetLength() < newlen); 
		destFile.Close();
		// Set the range
		pProgress.SetRange(0, len /newlen*10);
		// Set the position
		pProgress.SetPos(l);
		m_parts = _ltoa(l , buff, 10);
		m_parts += _T("个文件生成");
		UpdateData(FALSE);
		l++;
		UpdateWindow();
	}
	while (dwRead > 0);
		// close source
		m_SourceFile.Close();

	m_path = _T("");
	m_filename = _T("");
//	pProgress.SetPos(0);
//	m_parts = _T("");
	UpdateData(FALSE);

	return 0;
}

//选择待分割或合并的源文件
void CFileSpltDlg::OnBrowse() 
{
	//set filter for split source
	static char BASED_CODE szSplitFilter[] = _T("待分割文件(*.*)|*.*||");
	//set filter for target source
	static char BASED_CODE szMergeFilter[] = _T("待合并文件(1_*.*)|1_*.*||");

	CString filter;
	if (!m_split)
		filter = szMergeFilter;
	else 
		filter = szSplitFilter;

	CFileDialog dlg(TRUE,
					NULL, 
					NULL, 
					OFN_HIDEREADONLY	| 
					OFN_OVERWRITEPROMPT | 
					OFN_FILEMUSTEXIST,
					filter,
					0);

	//set source path
	if (dlg.DoModal() == IDOK) {
		m_path = dlg.GetPathName();
		m_filename = dlg.GetFileName();
		UpdateData(FALSE);
	}
	
}

//开始分割文件
void CFileSpltDlg::OnOk() 
{
    //split file
	int error = SplitMe();

	//get result
	if (!error) 
		MessageBox(_T("文件已被成功分割!"), _T("文件分割器"), MB_ICONEXCLAMATION);
		
}

//开始合并文件
void CFileSpltDlg::OnButtonMerge() 
{
    //merge file
	int error = MergeMe();

	//get result
	if (!error) 
		MessageBox(_T("文件合并成功!"), _T("文件分割器"), MB_ICONEXCLAMATION);
	
}

//选中文件合并单选框
void CFileSpltDlg::OnRadio4() 
{
  	CButton *pButtonMerge = (CButton*)(GetDlgItem(IDC_BUTTON_MERGE));
	CButton *pButtonSplit = (CButton*)(GetDlgItem(IDOK));
	
	//当选择合并文件时,置选择文件大小的下位框为无效状态
	m_FileSize.EnableWindow(false); 
	//play with button and edit ctrl states
	pButtonSplit->EnableWindow(FALSE);
	pButtonMerge->EnableWindow(TRUE);
	m_split = FALSE;
	m_path = _T("");
	UpdateData(FALSE);	
}

//选中文件分割单选框
void CFileSpltDlg::OnRadio3() 
{
	CButton *pButtonMerge = (CButton*)(GetDlgItem(IDC_BUTTON_MERGE));
	CButton *pButtonSplit = (CButton*)(GetDlgItem(IDOK));
	
	//当选择分割文件时,恢复选择文件大小的下位框为有效状态
	m_FileSize.EnableWindow(true); 
	//play with button and edit ctrl states
	pButtonSplit->EnableWindow(TRUE);
	pButtonMerge->EnableWindow(FALSE);
	m_split = TRUE;
	m_path = _T("");
	UpdateData(FALSE);	
}

//选择目标文件夹
void CFileSpltDlg::OnBrowse2() 
{
  CDirDialog	dlg;
	if (dlg.DoBrowse(this) == IDOK) {
		//set target path
		m_targetpath = dlg.m_strPath;
		UpdateData(FALSE);
	}	
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//设置位图按钮
	m_OK.LoadBitmaps(IDB_BITMAP1,5, 5, 5, 5, 4 );
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

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