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

📄 threadmd5.cpp

📁 选择两个文件夹
💻 CPP
字号:
// ThreadMD5.cpp: implementation of the CThreadMD5 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "File.h"
#include "ThreadMD5.h"

#include "md5.h"
#pragma  comment(lib,"hash")

#include "FileDlg.h"
CFileDlg FileDlg;

#include <vector>
using namespace std;
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


typedef struct  
{
	char szFileName[300];
	char szMD5[33];
}FILEINFO,*PFILEINFO;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CThreadMD5::CThreadMD5()
{
	bSuccess=FALSE;
}

CThreadMD5::~CThreadMD5()
{
	bSuccess=FALSE;
}

BOOL CThreadMD5::ThreadBegin()
{
	HANDLE hCreate=CreateThread(0,0,ThreadMD5,this,0,0);
	if (hCreate==NULL)
	{
		return FALSE;
	}
	else
	{
		CloseHandle(hCreate);
		return TRUE;
	}
}

DWORD WINAPI CThreadMD5::ThreadMD5(LPVOID lpParam)
{
	CThreadMD5 *pData=(CThreadMD5 *)lpParam;
	if (pData==NULL)
	{
		return 0;
	}
	else
	{
		pData->Run();
		return 0;
	}
}

void CThreadMD5::Run()
{
	CString strPath1=m_strPath1;
	CString strPath2=m_strPath2;
	strPath1=strPath1+"\\"+"*.*";
	strPath2=strPath2+"\\"+"*.*";
	CFileFind Find1;
	CFileFind Find2;
	BOOL bFinding1=Find1.FindFile(strPath1.GetBuffer(strPath1.GetLength()));
	CString strFileName1,strPathName1,strFileName2,strPathName2;
	vector<FILEINFO> vFI;
	FILEINFO FI={0};
	char szMD5[33]={0};
	while (bFinding1)
	{
		bFinding1=Find1.FindNextFile();
		strFileName1=Find1.GetFileName();
		strPathName1=m_strPath1+"\\"+strFileName1;
		if ((strcmp(strFileName1.GetBuffer(strFileName1.GetLength()),".")==0)||
			(strcmp(strFileName1.GetBuffer(strFileName1.GetLength()),"..")==0))
		{
			continue;	
		}
		else if(GetFileAttributes(strPathName1.GetBuffer(strPathName1.GetLength()))&FILE_ATTRIBUTE_DIRECTORY)
		{
			continue;
		}
		else
		{	
			strncpy(FI.szFileName,strFileName1.GetBuffer(strFileName1.GetLength()),sizeof(FI.szFileName)-1);
			
			if(GetFileMD5(strPathName1.GetBuffer(strPathName1.GetLength()),szMD5)==TRUE)
			{
				strncpy(FI.szMD5,szMD5,sizeof(FI.szMD5)-1);
			}
			else
			{
				memset(FI.szMD5,0,sizeof(FI.szMD5));
			}
			vFI.push_back(FI);
			memset((void*)&FI,0,sizeof(FI));
			memset(szMD5,0,sizeof(szMD5));
		}
	}
	vector<FILEINFO>::iterator p1;
	BOOL bFinding2=Find2.FindFile(strPath2.GetBuffer(strPath2.GetLength()));
	while (bFinding2)
	{
		bFinding2=Find2.FindNextFile();
		strFileName2=Find2.GetFileName();
		strPathName2=m_strPath2+"\\"+strFileName2;
		if ((strcmp(strFileName2.GetBuffer(strFileName2.GetLength()),".")==0)||
			(strcmp(strFileName2.GetBuffer(strFileName2.GetLength()),"..")==0))
		{
			continue;
		}
		else if(GetFileAttributes(strPathName2.GetBuffer(strPathName2.GetLength()))&FILE_ATTRIBUTE_DIRECTORY)
		{
			continue;
		}
		else
		{
			for (p1=vFI.begin();p1!=vFI.end();p1++)
			{
				if (strcmp((*p1).szFileName,strFileName2.GetBuffer(strFileName2.GetLength()))==0)
				{
					strPathName2=m_strPath2+"\\"+strFileName2;
					if (GetFileMD5(strPathName2.GetBuffer(strPathName2.GetLength()),szMD5)==TRUE)
					{
						
						if(strcmp((*p1).szMD5,szMD5)==0)
						{
							FileDlg.SetDisplay(strFileName2.GetBuffer(strFileName2.GetLength()),TRUE,TRUE);
							memset(szMD5,0,sizeof(szMD5));
						}
						else
						{
							FileDlg.SetDisplay(strFileName2.GetBuffer(strFileName2.GetLength()),TRUE,FALSE);
							memset(szMD5,0,sizeof(szMD5));
						}
					}
					else
					{
						FileDlg.SetDisplay(strFileName2.GetBuffer(strFileName2.GetLength()),TRUE,FALSE);
					}
				}
				
				
			}
		}
	}
	bSuccess=TRUE;	
}

void CThreadMD5::DigestToReadable(unsigned char digest[], char *readable)
{
	char *temp = new char[3];
	for (int i=0; i < 16; i++)
	{
		sprintf(temp,"%02x",digest[i]);  
		strcat(readable,temp);
	}	
	delete [] temp;
	temp = NULL;
}

BOOL CThreadMD5::GetFileMD5(LPCTSTR lpszFileName, LPTSTR lpMD5)
{
	const DWORD dwChunkSize=1024*10;
	HANDLE hFileHandle = CreateFile(
		lpszFileName,
		GENERIC_READ,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	if(hFileHandle==INVALID_HANDLE_VALUE) return FALSE;
	
	char szBuffer[dwChunkSize];
	//	MD5_CTX ctx;
	CMD5 md5; 
	MD5_32 md532;
	ZeroMemory( &md532, sizeof( MD5_32 ) );
	
	while(TRUE)
	{
		DWORD dwBytesRead;
		if(ReadFile(hFileHandle,szBuffer,dwChunkSize,&dwBytesRead,NULL)==0)
		{
			CloseHandle(hFileHandle);
			return FALSE;
		}
		md5.Update( (unsigned char *)szBuffer, dwBytesRead );
		if(dwBytesRead==0 || dwBytesRead<dwChunkSize) 
			break;
	}
	md5.Final();
	md5.GetHash( &md532);
	CloseHandle(hFileHandle);
	DigestToReadable((unsigned char*)md532._digest,lpMD5);
	return TRUE;
}

⌨️ 快捷键说明

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