以下是几个主要的函数,具体的实现请参考源代码:
long CFileSpltDlg::GetSplitFileSize() //获取用户选择文件分割后的大小
{
char buf[100];
m_FileSize.GetWindowText(buf,100);
for(int i=0; i<(int)strlen(buf); i++)
{
if(!::isdigit(buf[i]))
{
MessageBox("请输入数字!","文件分割", MB_ICONEXCLAMATION);
return 0;
}
}
return atoi(buf)*1000000;
}
|
int CFileSpltDlg::MergeMe() // 文件合并涵数 { CWaitCursor wait; CFile destFile; CFileException ex; BYTE buffer[140000]; DWORD dwRead; UINT nCount = 140000; UINT newlen = 1400000; char buff[20]; long l = 1; CString name; CString pref; CString newpath; UpdateData(TRUE); 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; } m_filename = m_path.Right((m_path.GetLength() - m_path.ReverseFind(@#\\@#)) - 1); m_SourceFile.Close(); } newpath = m_path.Left(m_path.GetLength() - m_filename.GetLength()); if (!m_targetpath.IsEmpty()) { if (!m_filename.IsEmpty() && m_filename.Left(2) != _T("1_")) { ::AfxMessageBox(_T("待合并的源文件名不对..."), MB_ICONERROR); return 1; } else if (m_filename.IsEmpty()) { MessageBox(_T("请选择待合并的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } m_filename = m_filename.Right(m_filename.GetLength() - 2); if (m_targetpath.Right(1) == @#\\@#) m_path = m_targetpath + m_filename; else m_path = m_targetpath + _T("\\") + m_filename; 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()) { MessageBox(_T("请选择待合并的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } if (m_targetpath.IsEmpty()) { //target is not there MessageBox(_T("请选择合并后要保存到的目标文件夹."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } do { pref = _ltoa(l, buff, 10); pref += _T("_"); 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(""); newpath = _T(""); UpdateData(FALSE); return 0; } else name = _T(newpath + pref + m_filename); do { dwRead = m_SourceFile.Read(buffer, nCount); destFile.Write(buffer, dwRead); } while (dwRead > 0); m_SourceFile.Close() ; pProgress.SetRange(0, 500); 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) ; return 0; } |
int CFileSpltDlg::SplitMe() //文件分割涵数
{
CWaitCursor wait;
CFile destFile;
CFileException ex;
DWORD dwRead;
UINT newlen;
char buff [20];
char b [20];
long l = 1;
CString name;
UINT len = 0;
UpdateData(TRUE);
newlen=GetSplitFileSize(); //获取文件分割后的大小,定义相对应变量数值
UINT nCount = newlen/10;
BYTE buffer[140000];
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;
}
len = m_SourceFile.GetLength();
}
else
{
MessageBox(_T("请选择待分割的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION);
return 1;
}
if (m_targetpath.IsEmpty())
{
MessageBox(_T("请选择分割后保存到的目标文件夹."), _T("文件分割器"),MB_ICONEXCLAMATION);
return 1;
}
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 {
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 (dwRead > 0 && destFile.GetLength() < newlen);
destFile.Close();
pProgress.SetRange(0, len /newlen*10);
pProgress.SetPos(l);
m_parts = _ltoa(l , buff, 10);
m_parts += _T("个文件生成");
UpdateData(FALSE);
l++;
UpdateWindow();
}while (dwRead > 0);
m_SourceFile.Close();
m_path = _T("");
m_filename = _T("");
UpdateData(FALSE);
return 0;
}
|
void CFileSpltDlg::OnBrowse() { static char BASED_CODE szSplitFilter[] = _T("待分割文件(*.*)|*.*||"); 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); if (dlg.DoModal() == IDOK) { m_path = dlg.GetPathName(); m_filename = dlg.GetFileName(); UpdateData(FALSE); } } |