// stdafx.cpp : source file that includes just the standard includes // DownloadShell.pch will be the pre-compiled header // stdafx.obj will contain the pre-compiled type information #include "stdafx.h" #include int FindAndCloseProcess(__in CString strProName) { int nIndex = strProName.ReverseFind('\\'); if (nIndex != -1) // 如果传的是全路径; strProName = strProName.Right(strProName.GetLength() - nIndex - 1); DWORD dwProcessID = 0; PROCESSENTRY32 pe32 = { 0 }; HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == NULL) { return 0; } pe32.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hProcessSnap, &pe32)) { do { if (_tcscmp(strProName, pe32.szExeFile) == 0) { dwProcessID = pe32.th32ProcessID; break; } } while (Process32Next(hProcessSnap, &pe32)); } CloseHandle(hProcessSnap); hProcessSnap = NULL; if (dwProcessID == 0) return 0; HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwProcessID); if (hProcess == NULL) return 0; DWORD dwError; if (!TerminateProcess(hProcess, 0)) { dwError = GetLastError(); CloseHandle(hProcess); hProcess = NULL; return -1; } // 等待进程结束响应; if (WAIT_OBJECT_0 != WaitForSingleObject(hProcess, INFINITE)) { CloseHandle(hProcess); AfxMessageBox(_T("结束进程失败")); return -1; } CloseHandle(hProcess); hProcess = NULL; return 1; } int UpdateFile(IN CONST DWORD &dwResourceID, IN LPCTSTR lpExt, IN LPCTSTR lpFileFullName) { if (lpFileFullName == NULL) return -1; HGLOBAL hGlobal = NULL; HRSRC hSource = NULL; LPVOID lpBuffer = NULL; DWORD dwSize = 0; BOOL bResult = FALSE; if (_tcscmp(lpExt, _T("EXE")) == 0) { FindAndCloseProcess(lpFileFullName); } if (DeleteFile(lpFileFullName) == 0) { DWORD dwError = GetLastError(); if (ERROR_FILE_NOT_FOUND != dwError && ERROR_PATH_NOT_FOUND != dwError) { CString strError; strError.Format(_T("%s:文件拒绝访问,删除文件失败!"), lpFileFullName); AfxMessageBox(strError); _exit(0); return -1; } } // 1.定位我们的自定义资源,这里因为我们是从本模块定位资源,所以将句柄简单地置为NULL即可 hSource = FindResource(NULL, MAKEINTRESOURCE(dwResourceID), lpExt); if (hSource == NULL) { AfxMessageBox(_T("载入资源失败, 升级失败!")); _exit(0); return -1; } // 2.获取资源的大小; dwSize = (UINT)SizeofResource(NULL, hSource); // 3.加载资源; hGlobal = LoadResource(NULL, hSource); if (hGlobal == NULL) { AfxMessageBox(_T("载入资源失败, 升级失败!")); _exit(0); return -1; } // 4.锁定资源,获取buffer; lpBuffer = LockResource(hGlobal); if (lpBuffer == NULL) { AfxMessageBox(_T("载入资源失败, 升级失败!")); _exit(0); return -1; } #if 0 // lpFileFullName需要先判断文件是否存在??不需要; CFile fp; if (fp.Open(lpFileFullName, CFile::modeCreate | CFile::modeWrite) == 0) { AfxMessageBox(_T("创建文件失败,升级失败!")); _exit(0); return -1; } fp.Write(lpBuffer, dwSize); fp.Close(); #else Uncompress(lpFileFullName, (BYTE*)lpBuffer, dwSize); #endif UnlockResource(hGlobal); FreeResource(hGlobal); return 0; } const TCHAR g_szLzma[] = _T("#LZMA#"); BOOL CompressFile(CString strCompressFile, CString &strSaveFile) { ////////////////////////////////////////////////////////////////////////// // 压缩文件; ////////////////////////////////////////////////////////////////////////// byte prop[5] = {0}; size_t nPropSize = 5; ULONGLONG ulTickCount = GetTickCount(); strSaveFile = strCompressFile.Mid(0,strCompressFile.ReverseFind('.')) + ".lam"; if ( PathFileExists(strSaveFile) ) { // 已经压缩了; return TRUE; } FILE* fp = fopen(strCompressFile, "rb"); if ( NULL != fp ) { fseek(fp, 0, SEEK_END); size_t nSrcLen = ftell(fp); fseek(fp, 0, SEEK_SET); byte* pSrcData = (byte*)malloc(nSrcLen); fread(pSrcData, nSrcLen, 1, fp); fclose(fp); size_t nDesLen = nSrcLen; byte* pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB int nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 8, 0, 2, 32, 2); #else int nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2); #endif if ( SZ_ERROR_OUTPUT_EOF == nRet ) {//申请内存不足; free(pDesData); nDesLen += nSrcLen; pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 5, (1<<24), 8, 0, 2, 32, 2); #else nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2); #endif } if ( SZ_OK == nRet ) {//压缩完成,保存文件; fp = fopen(strSaveFile, "wb+"); if ( NULL != fp ) { // 写入压缩标记; fwrite(g_szLzma, strlen(g_szLzma), 1, fp); // 写入prop; fwrite(prop, 5, 1, fp); // 写入nPropSize; fwrite(&nPropSize, sizeof(size_t), 1, fp); // 写入文件大小; fwrite(&nSrcLen, sizeof(size_t), 1, fp); // 写入压缩数据; fwrite(pDesData, nDesLen, 1, fp); fclose(fp); } else { nRet = SZ_ERROR_FAIL; } } free(pDesData); free(pSrcData); if ( nRet != SZ_OK ) return FALSE; } else { return FALSE; } return TRUE; } BOOL CompressFile(CString strCompressFile, BYTE** pBuffer, INT& nLen) { ////////////////////////////////////////////////////////////////////////// // 压缩文件; ////////////////////////////////////////////////////////////////////////// byte prop[5] = {0}; size_t nPropSize = 5; FILE* fp = fopen(strCompressFile, "rb"); if ( NULL != fp ) { fseek(fp, 0, SEEK_END); size_t nSrcLen = ftell(fp); fseek(fp, 0, SEEK_SET); byte* pSrcData = (byte*)malloc(nSrcLen); fread(pSrcData, nSrcLen, 1, fp); fclose(fp); size_t nDesLen = nSrcLen; byte* pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB int nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 8, 0, 2, 32, 2); #else int nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2); #endif if ( SZ_ERROR_OUTPUT_EOF == nRet ) {//申请内存不足; free(pDesData); nDesLen += nSrcLen; pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 5, (1<<24), 8, 0, 2, 32, 2); #else nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2); #endif } if ( SZ_OK == nRet ) {//压缩完成,保存文件; nLen = sizeof(size_t)*2 + 5 + nDesLen; if ( *pBuffer != NULL ) delete *pBuffer; *pBuffer = new BYTE[nLen]; memcpy(*pBuffer, g_szLzma, strlen(g_szLzma)); memcpy(*pBuffer, &prop, 5); memcpy(*pBuffer + 5, &nPropSize, sizeof(size_t)); memcpy(*pBuffer + 5 + sizeof(size_t), &nSrcLen, sizeof(size_t)); memcpy(*pBuffer + 5 + sizeof(size_t)*2, pDesData, nDesLen); } free(pDesData); free(pSrcData); if ( nRet != SZ_OK ) return FALSE; } else { return FALSE; } return TRUE; } BOOL IsCompress(CString strUnCompressFile) { TCHAR szLzma[50] = {0}; CString strLama = strUnCompressFile.Mid(0,strUnCompressFile.ReverseFind('.')); FILE* fp = fopen(strUnCompressFile, "rb"); if ( NULL != fp ) { // 获取压缩标记; fread(szLzma, strlen(g_szLzma), 1, fp); if ( _tcscmp(szLzma, _T("#LZMA#")) != 0 ) { fclose(fp); return FALSE; } fclose(fp); } return TRUE; } BOOL Uncompress(CString strUnCompressFile) { ////////////////////////////////////////////////////////////////////////// // 解压文件; ////////////////////////////////////////////////////////////////////////// TCHAR szLzma[50] = {0}; byte prop[5] = {0}; size_t nPropSize = 5; size_t nOrgSize = 0; CString strLama = strUnCompressFile.Mid(0,strUnCompressFile.ReverseFind('.')); FILE* fp = fopen(strUnCompressFile, "rb"); if ( NULL != fp ) { // 获取压缩文件长度; fseek(fp, 0, SEEK_END); size_t nSrcLen = ftell(fp) - 5 - sizeof(size_t)*2 - strlen(g_szLzma); // 返回头部,创建压缩缓存; fseek(fp, 0, SEEK_SET); byte* pSrcData = (byte*)malloc(nSrcLen) ; // 获取压缩标记; INT nLzmaLen = strlen(g_szLzma); fread(szLzma, nLzmaLen, 1, fp); if ( _tcscmp(szLzma, _T("#LZMA#")) != 0 ) { free(pSrcData); fclose(fp); return FALSE; } // 获取prop; fseek(fp, nLzmaLen, SEEK_SET); fread(prop, 5, 1, fp); // 获取nPropSize; fseek(fp,nLzmaLen + 5, SEEK_SET); fread(&nPropSize, sizeof(size_t), 1, fp); // 获取原文件大小; fseek(fp, nLzmaLen + 5 + sizeof(size_t), SEEK_SET); fread(&nOrgSize, sizeof(size_t), 1, fp); // 读取压缩内容; fseek(fp, 5 + 2*sizeof(size_t) + strlen(g_szLzma), SEEK_SET); fread(pSrcData, nSrcLen, 1, fp); fclose(fp); size_t nDesLen = nOrgSize;//1.1*nSrcLen + 16*1024; byte* pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB int nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #else int nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #endif if ( SZ_ERROR_INPUT_EOF == nRet ) {//申请空间不足; free(pDesData); pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #else nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #endif } if ( SZ_OK == nRet ) {//解压完成; fp = fopen(strLama, "wb+"); if ( NULL != fp ) { fwrite(pDesData, nDesLen, 1, fp); fclose(fp); } else { nRet = SZ_ERROR_FAIL; } } free(pSrcData); free(pDesData); if ( nRet == SZ_OK ) return TRUE; } return FALSE; } /************************************************************************/ /* 函数:[1/26/2018 Jeff]; /* 描述:; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ BOOL Uncompress(IN CString strSaveName, IN BYTE *pData, IN const size_t& nlen) { if ( pData == NULL || nlen == 0 ) return FALSE; ////////////////////////////////////////////////////////////////////////// // 解压文件; ////////////////////////////////////////////////////////////////////////// TCHAR szLzma[50] = {0}; byte prop[5] = {0}; size_t nPropSize = 5; size_t nOrgSize = 0; // 获取压缩文件长度; size_t nSrcLen = nlen - 5 - sizeof(size_t)*2 - strlen(g_szLzma); // 创建压缩缓存; byte* pSrcData = (byte*)malloc(nSrcLen) ; // 获取压缩标记; INT nLzmaLen = strlen(g_szLzma); memcpy(szLzma, pData, nLzmaLen); if ( _tcscmp(szLzma, _T("#LZMA#")) != 0 ) { return FALSE; } // 获取prop; memcpy(prop, pData +nLzmaLen, 5); // 获取nPropSize; memcpy(&nPropSize, pData +nLzmaLen + 5, sizeof(size_t)); // 获取原文件大小; memcpy(&nOrgSize, pData +nLzmaLen + 5 + sizeof(size_t), sizeof(size_t)); // 读取压缩内容; memcpy(pSrcData, pData +nLzmaLen + 5 + sizeof(size_t)*2, nSrcLen); size_t nDesLen = nOrgSize;//1.1*nSrcLen + 16*1024; byte* pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB int nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #else int nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #endif while ( SZ_ERROR_INPUT_EOF == nRet ) {//申请空间不足; free(pDesData); pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #else nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #endif } if ( SZ_OK == nRet ) {//解压完成; FILE* fp = fopen(strSaveName, "wb+"); if ( NULL != fp ) { fwrite(pDesData, nDesLen, 1, fp); fclose(fp); } else { nRet = SZ_ERROR_FAIL; } } free(pSrcData); free(pDesData); if ( nRet == SZ_OK ) return TRUE; return FALSE; }