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

📄 bindfiledlg.cpp

📁 利用vc++实现多文件合并与任意文件提取功能
💻 CPP
字号:
// BindFileDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "BindFile.h"
#include "BindFileDlg.h"
#include ".\bindfiledlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CBindFileDlg 对话框



CBindFileDlg::CBindFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBindFileDlg::IDD, pParent)
	, m_destfilestr(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBindFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_destfilestr);
	DDX_Control(pDX, IDC_DESTFILE_LIST, m_destfilectr);
	DDX_Control(pDX, IDC_LIST2, m_scrfilectr);
}

BEGIN_MESSAGE_MAP(CBindFileDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4)
	ON_BN_CLICKED(IDC_NEWBIND_BUTTON, OnBnClickedNewbindButton)
	ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedButton5)
	ON_BN_CLICKED(IDC_LOOK_BUTTON, OnBnClickedLookButton)
	ON_BN_CLICKED(IDC_UBIND_BUTTON, OnBnClickedUbindButton)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
END_MESSAGE_MAP()


// CBindFileDlg 消息处理程序

BOOL CBindFileDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将\“关于...\”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	///////列表框初始化
	m_destfilectr.InsertColumn(0,"文件名称",LVCFMT_LEFT,100);
	m_destfilectr.InsertColumn(1,"文件大小",LVCFMT_RIGHT,100);
	m_destfilectr.InsertColumn(2,"文件初始位置",LVCFMT_RIGHT,100);

	m_scrfilectr.InsertColumn(0,"文件名称",LVCFMT_LEFT,100);
	m_scrfilectr.InsertColumn(1,"文件大小",LVCFMT_RIGHT,100);
	//m_scrfilectr.InsertColumn(2,"文件初始位置",LVCFMT_RIGHT,100);




	
	return TRUE;  // 除非设置了控件的焦点,否则返回 TRUE
}

void CBindFileDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CBindFileDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CBindFileDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CBindFileDlg::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码
	/////添加所需要压缩的文件
	do
	{
		CFileDialog fileDialog(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT,"所有文件(*.*)|*.*||");
		if (fileDialog.DoModal() == IDOK) 
		{
			POSITION mPos=fileDialog.GetStartPosition(); 
			CString pathName;
			while(mPos!=NULL) 
			{ 
				pathName=fileDialog.GetNextPathName(mPos); 
				scrFiles.Add(pathName);
				CFile file(pathName,CFile::modeRead);
				ULONGLONG dwLength = file.GetLength();
				file.Close();
				CString   str; 
				str.Format(_T( "%I64u"),   dwLength); 
				int index = m_scrfilectr.InsertItem(0,pathName);
				m_scrfilectr.SetItemText(index,1,str);
	             
			} 		
		}
	}
	while(MessageBox("还需要继续添加吗?","通知",MB_YESNO|MB_ICONQUESTION)==IDYES) ;
}

// 合并文件模块
bool CBindFileDlg::Bind_Files(CString destFileName,////合并后的文件名称
							  bool bUp////是否为续添还是新建
							  )
{
	 CFile out;      //最终合成文件     
	 BYTE *buf ;	 
	 CFileException e;
	 if(bUp)////续添,打开已有文件
	 {
		if(!out.Open(destFileName,CFile::modeWrite,&e))////创建
		{
			#ifdef _DEBUG
			afxDump << "File could not be opened " << e.m_cause << "\n";
			#endif
			return false;
		}
		out.SeekToEnd();////到文件结尾
	 }
	 else////新建文件
	 {
		if(!out.Open(destFileName,CFile::modeCreate|CFile::modeWrite,&e))////创建
		{
			#ifdef _DEBUG
			afxDump << "File could not be Created " << e.m_cause << "\n";
			#endif
			return false;
		}	 
	 }
	  

	  for( int i =0;i < scrFiles.GetSize();i++)
	  {
		  CString temp = scrFiles.GetAt(i);
		  CFile in;
		  if(!in.Open(temp,CFile::modeRead,&e))
		  {
			   #ifdef _DEBUG
				afxDump << "File could not be opened " << e.m_cause << "\n";
				#endif
				out.Close();
				return false;
		  }		 
		  MODIFY_DATA modify;
		  strcpy(modify.filename,in.GetFileName());
		  modify.dwPos = out.GetPosition()+sizeof(MODIFY_DATA);
		  modify.dwLength=in.GetLength();
		  out.Write(&modify,sizeof(MODIFY_DATA));////保存文件头信息
		  buf = (BYTE *)malloc(modify.dwLength);
			 ULONGLONG num=0;
			 ULONGLONG total=0;
			do/////保存文件数据
			{
				num+=in.Read(buf+total,modify.dwLength-total);
				out.Write(buf+total,modify.dwLength-total);
				total=num;
				CString t;
				t.Format("正在捆绑:%s, 大小%d,当前量:%d\n",modify.filename,modify.dwLength,total);
				TRACE0((LPCTSTR)t);
			}
			while(num<modify.dwLength);
			in.Close();
			free(buf);
	  }
	  out.Close();
	 return true;

}

// 提取合并后的文件信息
bool CBindFileDlg::GetBindFileInfo(CString bindfilename)
{
	CFileException e;
	CFile in;
	if(!in.Open(bindfilename,CFile::modeRead,&e))
	{
		#ifdef _DEBUG
		afxDump << "File could not be opened " << e.m_cause << "\n";
		#endif
		in.Close();
		return false;
	}
	MODIFY_DATA modify;
	while(in.GetPosition()!=in.GetLength())/////读取所有合并文件的信息
	{
		in.Read(&modify,sizeof(MODIFY_DATA));
		in.Seek(modify.dwLength,CFile::current);
		bindFileInfo.Add(modify);
	}
	in.Close();

}

void CBindFileDlg::OnBnClickedNewbindButton()//新建合并文件
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog fileDialog(FALSE,"bdf","合并文件",NULL,"合并文件(*.bdf)|*.bdf||");
	if(fileDialog.DoModal() == IDOK)
	{
		Bind_Files(fileDialog.GetPathName(),FALSE);///合并为新文件

	}

}

void CBindFileDlg::OnBnClickedButton5()////续添
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(0);
	CFileFind find;
	if(!find.FindFile(m_destfilestr))
	{
		MessageBox("没有发现需添加的目标文件!","通知",MB_YESNO|MB_ICONSTOP);
		return;
	}
	Bind_Files(m_destfilestr,TRUE);
}

void CBindFileDlg::OnBnClickedLookButton()////浏览文件;
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog fileDialog(TRUE,NULL,NULL,NULL,"合并文件(*.bdf)|*.bdf||");
	if(fileDialog.DoModal() == IDOK)
	{
		m_destfilestr = fileDialog.GetPathName();

		GetBindFileInfo(fileDialog.GetPathName());///合并为新文件
		for(int i = 0;i<bindFileInfo.GetSize();i++)
		{
			MODIFY_DATA modify = bindFileInfo.GetAt(i);
			int index = m_destfilectr.InsertItem(0,modify.filename);
			m_destfilectr.SetItemData(index,i);
			CString   str; 
			str.Format(_T( "%I64u"),   modify.dwLength);			
			m_destfilectr.SetItemText(index,1,str);
			str.Format(_T( "%I64u"),   modify.dwPos);			
			m_destfilectr.SetItemText(index,2,str);

		}

	}
	UpdateData(0);
}

void CBindFileDlg::OnBnClickedUbindButton()////提取指定文件信息
{
	// TODO: 在此添加控件通知处理程序代码
	POSITION pos = m_destfilectr.GetFirstSelectedItemPosition();
     if (pos == NULL)
          MessageBox("没有选定指定文件!","通知",MB_YESNO|MB_ICONSTOP);
      else
      {
           while (pos)
           {
                int nItem = m_destfilectr.GetNextSelectedItem(pos);
				int index = m_destfilectr.GetItemData(nItem);
				MODIFY_DATA modify = bindFileInfo.GetAt(index);
				CString path;
				path.GetEnvironmentVariable("TEMP");
				CString temp;
				temp.Format("%s\\%s",path,modify.filename);
				UNBind_File(temp,modify.dwPos,modify.dwLength);
				tempFile.Add(temp);
				CString strOpen;
				strOpen.Format("Rundll32.exe shell32.dll OpenAs_RunDLL %s",temp);
				if (strOpen.Find("%L") != -1)
					strOpen.Replace("%L",temp);
				else if (strOpen.Find("%1") != -1)
					strOpen.Replace("%1",temp);				
				::WinExec(strOpen,SW_SHOW);
                // you could do your own processing on nItem here
           }
      }


}

bool CBindFileDlg::UNBind_File(CString destFileName, ULONGLONG dwPos, ULONGLONG dwLength)/////提取某一个文件信息
{
	UpdateData(1);
	CFileException e;
	CFile in;
	if(!in.Open(m_destfilestr,CFile::modeRead,&e))
	{
		#ifdef _DEBUG
		afxDump << "File could not be opened " << e.m_cause << "\n";
		#endif
		in.Close();
		return false;
	}
	in.SeekToBegin();
	in.Seek(dwPos,CFile::current);
	CFile out;
	if(!out.Open(destFileName,CFile::modeCreate|CFile::modeWrite,&e))
	{
		#ifdef _DEBUG
		afxDump << "File could not be opened " << e.m_cause << "\n";
		#endif
		in.Close();
		return false;
	}
	BYTE *buf ;
	buf = (BYTE *)malloc(dwLength);
	ULONGLONG num=0;
	ULONGLONG total=0;
	do/////保存文件数据
	{
		num+=in.Read(buf+total,dwLength-total);
		out.Write(buf+total,dwLength-total);
		total=num;
		CString t;
		t.Format("正在捆绑:%s, 大小%d,当前量:%d\n",destFileName,dwLength,total);
		TRACE0((LPCTSTR)t);
	}
	while(num<dwLength);
	in.Close();
	free(buf);
	out.Close();


	return true;
}

// 删除所产生的零时文件
void CBindFileDlg::DelTempFile(void)
{
	for(int i = 0;i<tempFile.GetSize();i++)
	{
		CString temp= tempFile.GetAt(i);
		CFileFind find;
		if(find.FindFile(temp))
			::DeleteFile(temp);
	}
	tempFile.RemoveAll();
}

void CBindFileDlg::OnDestroy()
{
	CDialog::OnDestroy();
    DelTempFile();
	// TODO: 在此处添加消息处理程序代码
}

void CBindFileDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	POSITION pos = m_destfilectr.GetFirstSelectedItemPosition();
     if (pos == NULL)
          MessageBox("没有选定指定文件!","通知",MB_YESNO|MB_ICONSTOP);
      else
      {
           while (pos)
           {
                int nItem = m_destfilectr.GetNextSelectedItem(pos);
				int index = m_destfilectr.GetItemData(nItem);
				MODIFY_DATA modify = bindFileInfo.GetAt(index);
				CFileDialog fileDialog(FALSE,".*",modify.filename,NULL,"提取文件(*.*)|*.*||");
				if(fileDialog.DoModal()!= IDOK)
				{
					break;

				}
				CString path = fileDialog.GetPathName();
				UNBind_File(path,modify.dwPos,modify.dwLength);                
           }
      }
}

⌨️ 快捷键说明

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