文件分割与合并实现

// 文件合并函数
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, pref, 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);
m_path = m_targetpath.Right(1) == '' ? m_targetpath + m_filename : 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()) {
MessageBox(_T("请选择合并后要保存到的目标文件夹."), _T("文件分割器"), MB_ICONEXCLAMATION);
return 1;
}
do {
pref = _ltoa(l, buff, 10) + _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("");
UpdateData(FALSE);
return 0;
}
do {
dwRead = m_SourceFile.Read(buffer, nCount);
destFile.Write(buffer, dwRead);
} while (dwRead > 0);
m_SourceFile.Close();
for (int i = 0; i < 500> m_parts = _ltoa(l, buff, 10) + _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], 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) + _T("_");
CString newpath = m_targetpath.Right(1) == '' ? m_targetpath : 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) + _T("个文件生成");
UpdateData(FALSE);
l++;
UpdateWindow();
} while (dwRead > 0);
m_SourceFile.Close();
m_path = _T("");
m_filename = _T("");
UpdateData(FALSE);
return 0;
}
zip
分割合并文件.zip 预估大小:25个文件
folder
FileSplt 文件夹
file
resource.h 1KB
file
FileSplt.rc 7KB
file
FileSpltDlg.cpp 15KB
file
FileSplt.dsw 539B
file
ReadMe.txt 493B
folder
res 文件夹
file
FileSplt.rc2 400B
file
ButtonsWin3000.bmp 18KB
file
Button1.bmp 5KB
file
FileSplt.ico 1KB
file
MemDC.h 3KB
file
FileSplt.clw 2KB
file
GradientProgressCtrl.cpp 11KB
file
FileSplt.dsp 5KB
file
StdAfx.cpp 210B
file
GradientProgressCtrl.h 2KB
file
DirDialog.h 774B
file
AutoFont.h 2KB
file
AutoFont.cpp 7KB
file
FileSpltDlg.h 2KB
file
WBButton.h 4KB
file
FileSplt.cpp 2KB
file
DirDialog.cpp 4KB
file
StdAfx.h 1KB
file
WBButton.cpp 8KB
file
FileSplt.h 1KB
zip 文件大小:33.33KB