/** * @brief 拷贝目录 * @param[in] szSrcDir 源目录 * @param[in] szDstDir 目标目录 * @param[in] bCoverFile 是否覆盖已存在的文件 * @return 返回拷贝文件的数目 * @see */ static int CopyDirectory(LPCTSTR lpSrcDir, LPCTSTR lpDstDir, BOOL bCoverFile = TRUE) { if (!lpSrcDir || !lpDstDir) return 0; int nReturn = 0; CString strFind; CString strSubFile; CString strSubDstFile; CString strSrcDir(lpSrcDir); CString strDstDir(lpDstDir); WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; ZLPath::PathAddBackslash(strSrcDir); ZLPath::PathAddBackslash(strDstDir); CreateDeepDirectory(strDstDir); strFind.Format(_T("%s*.*"), strSrcDir); hFind = ::FindFirstFile(strFind, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) goto Exit0; do { if (_tcscmp(FindFileData.cFileName, _T(".")) == 0 || _tcscmp(FindFileData.cFileName, _T("..")) == 0) continue; if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { strSubFile.Format(_T("%s%s\\"), strSrcDir, FindFileData.cFileName); strSubDstFile.Format(_T("%s%s\\"), strDstDir, FindFileData.cFileName); CreateDeepDirectory(strSubDstFile); nReturn += CopyDirectory(strSubFile, strSubDstFile, !bCoverFile); } else { strSubFile.Format(_T("%s%s"), strSrcDir, FindFileData.cFileName); strSubDstFile.Format(_T("%s%s"), strDstDir, FindFileData.cFileName); if (!::CopyFile(strSubFile, strSubDstFile, !bCoverFile)) nReturn++; } } while (::FindNextFile(hFind, &FindFileData) != 0); Exit0: if (hFind != INVALID_HANDLE_VALUE) ::FindClose(hFind); return nReturn; }
BOOL KFunction::CopyFolder_Force(LPCTSTR szSrcPath, LPCTSTR szDstPath) { BOOL bReturn = FALSE; BOOL bRetCode = FALSE; CString strSrcPath(szSrcPath); CString strDstPath(szDstPath); KFunction::PathAddBackslash(strSrcPath); KFunction::PathAddBackslash(strDstPath); bRetCode = CreateDeepDirectory(strDstPath); if (!bRetCode) goto Exit0; bRetCode = _CopyFolder_Force(strSrcPath, strDstPath); if (!bRetCode) goto Exit0; bReturn = TRUE; Exit0: log_w(L"KFunction::CopyFolder_Force return:%d, src:%s, dst:%s\n", bReturn, szSrcPath, szDstPath); return bReturn; }
/** * @brief 创建目录 * @param[in] szPath 路径 * @return 成功返回TRUE,失败返回FALSE * @see */ static BOOL CreateDeepDirectory(LPCTSTR lpPath) { if (!lpPath) return FALSE; BOOL bRetCode = FALSE; CString strPath(lpPath); if (::GetFileAttributes(lpPath) != INVALID_FILE_ATTRIBUTES) return TRUE; bRetCode = ::CreateDirectory(lpPath, NULL); if (!bRetCode && ::GetLastError() != ERROR_ALREADY_EXISTS) { ZLPath::PathRemoveFileSpec(strPath); if (strPath.IsEmpty()) return FALSE; bRetCode = CreateDeepDirectory(strPath); if (!bRetCode) return FALSE; bRetCode = ::CreateDirectory(lpPath, NULL); if (!bRetCode && ::GetLastError() != ERROR_ALREADY_EXISTS) return FALSE; } return TRUE; }
bool CExtInstaller::InstallCRX() { tstring crxPath = m_CurrentDir+_T("/") + SAMPLE_CRX_PATH; HANDLE hFile = ::CreateFile(crxPath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if ( INVALID_HANDLE_VALUE == hFile ) { DWORD dwError = GetLastError(); SHOW_LOG( _T("CExtInstaller::InstallCRX CreateFile [ERROR CODE]%d\n"), dwError); return false; } ULARGE_INTEGER liFileSize; liFileSize.QuadPart = 0; liFileSize.LowPart = ::GetFileSize(hFile, &liFileSize.HighPart); if( liFileSize.LowPart > CHROME_CRX_HEADER_LEN + 2/*PK header len */) { byte* byteOriginBuffer = new byte[liFileSize.LowPart],*byteBuffer = byteOriginBuffer; DWORD dwReadSize = 0; byteBuffer = (byte*)((size_t)byteBuffer+CHROME_CRX_HEADER_LEN); BOOL bRet = ::ReadFile(hFile,(LPVOID)byteOriginBuffer,liFileSize.LowPart,&dwReadSize,NULL); if( 'P' == *(char*)((size_t)byteBuffer+1) && 'K' == *(char*)((size_t)byteBuffer+2) ) { int writeLen = liFileSize.LowPart - CHROME_CRX_HEADER_LEN; DWORD outwriteLen = 0; tstring tmpZipPath = m_CurrentDir+_T("/") + TMP_CRX_ZIP_PATH; HANDLE houtZipFile = ::CreateFile(tmpZipPath.c_str(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); WriteFile(houtZipFile, (LPVOID)byteBuffer, writeLen, &outwriteLen, NULL); CloseHandle(houtZipFile); HANDLE hZipFile = CreateZip(tmpZipPath.c_str(),false); tstring extractPath = GetChromeExtPath(CHROME_SAMPLE_CRX_ID_W, CHROME_SAMPLE_CRX_VER); CreateDeepDirectory(extractPath.c_str(), NULL); if( false == ZipExtract(hZipFile, extractPath.c_str())) { SHOW_LOG( " CExtInstaller::InstallCRX extractFile error:%d\n",GetLastError()); } CloseZip(hZipFile,false); DeleteFile(tmpZipPath.c_str()); } else return false; if( NULL != byteOriginBuffer) { delete [] byteOriginBuffer; } } CloseHandle(hFile); return true;