//文件夹匹配:对比文件夹,相同的目录结构、所有文件名称小写相同,制定文件外的MD5值相同 ,则两个文件夹匹配成功!
////// 批量匹配书籍H5资源包 /// /// 平台书籍H5资源所在路径 /// 原加工书籍包所在路径 private void MultiMatchBookH5Res(string strPlateDirPath, string strSourDirPath) { //所有平台书籍包路径集 string[] arryPlateDir = Directory.GetDirectories(strPlateDirPath); //所有加工书籍包路径集 string[] arrySourDir = Directory.GetDirectories(strSourDirPath); //遍历匹配 平台和原加工 书籍包 foreach (string plateBookDir in arryPlateDir) { foreach (string srcBookDir in arrySourDir) { bool isMatch = MatchTextBookH5Reses(plateBookDir, srcBookDir,true); if (isMatch) { break; } } //刷新进度 //mFindSamePackageworker.ReportProgress(nIndex + 1, arryDir.Length); } } ////// 匹配书籍文件夹 /// /// 平台书籍文件夹路径 /// 原加工书籍文件夹路径 private bool MatchTextBookH5Reses(string plateBookDirPath, string srcBookDirPath,bool isNoMatchReturn = false) { bool isMatch = false; try { //平台书籍H5资源文件夹路径集 string[] arryPlateBookResPath = Directory.GetDirectories(plateBookDirPath); //原加工书籍H5资源文件夹路径集 string[] arrySrcBookResPath = Directory.GetDirectories(srcBookDirPath); // 1: 判断H5资源数量是否相等 int plateBookResesCount = arryPlateBookResPath.Count(); int srcBookResesCount = arrySrcBookResPath.Count(); //if (plateBookResesCount != srcBookResesCount) //{ // //MessageBox.Show("平台书籍H5资源数量与原加工书籍H5资源数量不相等!"); // return isMatch; //} mDicMatchH5Res.Clear(); string SaveInfo = "平台书籍H5资源数量:" + plateBookResesCount; SaveInfo += "\r\n"; SaveInfo += "原加工书籍H5资源数量 : " + srcBookResesCount; //2:H5资源是否全匹配 foreach (string plateBookResPath in arryPlateBookResPath) { foreach (string srcBookResPath in arrySrcBookResPath) { // 1): 判断H5资源子文件夹目录是否全匹配 isMatch = MatchH5ResDir(plateBookResPath, srcBookResPath); if(isMatch) { //SaveInfo += "\r\n"; //SaveInfo += SaveInfo += plateBookResPath + " 目录匹配: " + srcBookResPath; //"平台书籍H5资源目录结构与原加工书籍H5资源目录结构相同 "; // 2): 判断H5资源子文件是否全匹配 isMatch = MatchH5Reses(plateBookResPath, srcBookResPath); if(isMatch) { SaveInfo += "\r\n"; SaveInfo += plateBookResPath+ " 匹配: " + srcBookResPath; mDicMatchH5Res.Add(plateBookResPath,srcBookResPath); break; } } } if (!isMatch) //平台任意一H5资源未匹配到该原加工书籍H5则正本书不匹配 { mDicMatchH5Res.Add(plateBookResPath, string.Empty); SaveInfo += "\r\n"; SaveInfo += plateBookResPath + " 匹配: " + string.Empty; if (isNoMatchReturn) { return isMatch; //单个H5资源匹配失败直接返回 } } } // 3:H5资源一对一匹配 int srcMatchH5ResCount = mDicMatchH5Res.Values.Count(); ListlstSrcMathcH5Res = new List (); lstSrcMathcH5Res.AddRange(mDicMatchH5Res.Values); int srcMatchH5ResDistinctCount = lstSrcMathcH5Res.Distinct().Count(); SaveInfo += "\r\n"; int count = srcMatchH5ResCount - srcMatchH5ResDistinctCount; SaveInfo += "资源匹配重复数量: " + count; if(srcMatchH5ResCount != srcMatchH5ResDistinctCount) { MessageBox.Show("平台书籍H5资源匹配原加工书籍H5资源重复!"); } //将匹配信息输出到文件夹 DirectoryInfo dirInfo = new DirectoryInfo(plateBookDirPath); string fileName = dirInfo.Name + ".txt"; string filePath = Path.GetDirectoryName(plateBookDirPath); string fullName = Path.Combine(filePath, fileName); SaveContentToFile(SaveInfo, fullName); // 4:同步为原加工书籍H5资源文件名称 } catch (Exception ex) { isMatch = false; } return isMatch; } /// /// 匹配H5资源目录结构 /// /// 平台书籍文件夹 /// 原加工书籍文件夹 private bool MatchH5ResDir(string plateBookResPath, string srcBookResPath) { bool isMatch = false; try { //平台书籍H5资源子文件夹路径集 string[] arryPlateH5ResPath = Directory.GetDirectories(plateBookResPath); var arryPlateH5ResDir = arryPlateH5ResPath.Select(o => o.Replace(plateBookResPath, string.Empty)); ListlstPlateH5ResDir = new List (); lstPlateH5ResDir.AddRange(arryPlateH5ResDir); //原加工书籍H5资源子文件夹路径集 string[] arrySrcH5ResPath = Directory.GetDirectories(srcBookResPath); var arrySrcH5ResDir = arrySrcH5ResPath.Select(o => o.Replace(srcBookResPath, string.Empty)); List lstSrcH5ResDir = new List (); lstSrcH5ResDir.AddRange(arrySrcH5ResDir); var lstSameBookResDir = lstPlateH5ResDir.Intersect(lstSrcH5ResDir); int plateH5ResDirCount = lstPlateH5ResDir.Count(); int sameH5ResesCount = lstSameBookResDir.Count(); if (sameH5ResesCount == plateH5ResDirCount) { isMatch = true; } } catch (Exception ex) { isMatch = false; } return isMatch; } /// /// 匹配H5资源 /// /// 平台书籍文件夹 /// 原加工书籍文件夹 private bool MatchH5Reses(string plateBookResPath, string srcBookResPath) { bool isMatch = false; try { //平台书籍H5资源子文件路径集 ListlstPlateH5ResFile = GetDirAllFiles(plateBookResPath,true); //解决H5资源内子文件同名问题 var lstPlateH5ResPathFile = lstPlateH5ResFile.Select(o => o.Replace(plateBookResPath, string.Empty).ToLower()); //原加工书籍H5资源子文件夹路径集 List lstSrcH5ResFile = GetDirAllFiles(srcBookResPath, true); //包含:easeljs-0.8.2.min.js 公共控件 、 *.ttf等文件 var lstSrcH5ResPathFile = lstSrcH5ResFile.Select(o => o.Replace(srcBookResPath, string.Empty).ToLower()); var lstSameH5ResPathFile = lstPlateH5ResPathFile.Intersect(lstSrcH5ResPathFile); int plateH5ResFilesCount = lstPlateH5ResPathFile.Count(); int sameH5ResFilesCount = lstSameH5ResPathFile.Count(); if (sameH5ResFilesCount == plateH5ResFilesCount) { foreach(string pathFile in lstSameH5ResPathFile) { if(pathFile.EndsWith(".html") || pathFile.EndsWith(".db")) { continue; } string plateFile = plateBookResPath + pathFile; string srcFile = srcBookResPath + pathFile; string plateFileMd5 = mMd5Oper.GetFileMd5Value(plateFile); string srcFileMd5 = mMd5Oper.GetFileMd5Value(srcFile); if(plateFileMd5 != srcFileMd5 || plateFileMd5 == null || srcFileMd5 == null ) { isMatch = false; return isMatch; } } isMatch = true; } } catch (Exception ex) { isMatch = false; } return isMatch; } /// /// 同步书籍H5资源内文件名称 /// /// 平台书籍文件夹路径 /// 原加工书籍文件夹路径 private bool SyncBookH5ResesName(string plateBookDirPath, string srcBookDirPath) { bool isSucess = false; try { //单个书籍匹配 bool isMatch = MatchTextBookH5Reses(plateBookDirPath, srcBookDirPath); //如果匹配则修改匹配的书籍名称为原始文件名称 if (isMatch) { //文件夹名称修改为原始文件夹名称(平台H5内文件夹名称=原始H5内文件夹名称)--不做修改 //文件名称修改为原始文件名称(平台H5内文件名称=原始H5内文件名称) foreach (var dicItem in mDicMatchH5Res) { bool isSuccess = SyncH5FilesName(dicItem.Key, dicItem.Value); if(isSuccess) { EncryptZipBookH5Res(dicItem.Key); } } } } catch (Exception ex) { isSucess = false; } return isSucess; } ////// 同步H5资源名称 /// /// 平台H5文件夹 /// 原加工H5文件夹 private bool SyncH5FilesName(string plateH5ResPath, string srcH5ResPath) { bool isMatch = false; try { //平台书籍H5资源子文件路径集 ListlstPlateH5ResFile = GetDirAllFiles(plateH5ResPath, true); //解决H5资源内子文件同名问题 var lstPlateH5ResPathFile = lstPlateH5ResFile.Select(o => o.Replace(plateH5ResPath, string.Empty).ToLower()); //原加工书籍H5资源子文件夹路径集 List lstSrcH5ResFile = GetDirAllFiles(srcH5ResPath, true); //包含:easeljs-0.8.2.min.js 公共控件 、 *.ttf等文件 var lstSrcH5ResPathFile = lstSrcH5ResFile.Select(o => o.Replace(srcH5ResPath, string.Empty).ToLower()); var lstSameH5ResPathFile = lstPlateH5ResPathFile.Intersect(lstSrcH5ResPathFile); int plateH5ResFilesCount = lstPlateH5ResPathFile.Count(); int sameH5ResFilesCount = lstSameH5ResPathFile.Count(); if (sameH5ResFilesCount == plateH5ResFilesCount) //为预防不是匹配的文件夹 为安全所以又重新匹配了下 { foreach (string pathFile in lstSameH5ResPathFile) //修改名称包含 .html .db 不需要H5完全匹配 因为是在匹配完毕的前提下同步的名称 { string plateFile = plateH5ResPath + pathFile; string srcSameFile = srcH5ResPath + pathFile; FileInfo plateH5Info = new FileInfo(plateFile); foreach(string srcFile in lstSrcH5ResFile) { string lowerSrcFile = srcFile.ToLower(); string lowerSameFile = srcSameFile.ToLower(); if (lowerSrcFile == lowerSameFile) { string fileName = Path.GetFileName(srcFile); string plateNewFile = plateFile.Replace(plateH5Info.Name,fileName); plateH5Info.MoveTo(plateNewFile); //直接修改名称 } } } isMatch = true; } } catch (Exception ex) { isMatch = false; } return isMatch; } /// /// 加密、压缩书籍内的H5资源包 /// private void EncryptZipBookH5Reses(string plateBookDirPath) { DirectoryInfo dirInfo = new DirectoryInfo(plateBookDirPath); if(dirInfo.Exists) { DirectoryInfo[] arryH5ResDirInfo = dirInfo.GetDirectories(); foreach (var dirInfoItem in arryH5ResDirInfo) { EncryptZipBookH5Res(dirInfoItem.FullName); } } } ////// 加密、压缩H5资源包 /// private bool EncryptZipBookH5Res(string h5ResDirPath) { bool isSucess = false; try { DirectoryInfo dirInfo = new DirectoryInfo(h5ResDirPath); if (dirInfo.Exists) { string dirName = dirInfo.Name; string dirParentPath = Path.GetDirectoryName(h5ResDirPath); string zipPath = Path.Combine(dirParentPath, "zip"); string zipFile = Path.Combine(zipPath, dirName) + ".zip"; string ppubPath = Path.Combine(dirParentPath, "ppub"); string ppubFile = Path.Combine(ppubPath, dirName) + ".ppub"; DirectoryInfo zipDirInfo = new DirectoryInfo(zipPath); if(!zipDirInfo.Exists) { zipDirInfo.Create(); } mZipOper.CreateZipFile(h5ResDirPath, zipFile); DirectoryInfo ppubDirInfo = new DirectoryInfo(ppubPath); if (!ppubDirInfo.Exists) { ppubDirInfo.Create(); } mEncryOper.FileEncrypt(zipFile, ppubFile); isSucess = true; } } catch (Exception ex) { isSucess = false; } return isSucess; }
//文件 操作
////// 内容保存至本地文件 /// /// /// ///public bool SaveContentToFile(string strContent, string fileFullName) { bool isSuccess = false; try { FileInfo fInfo = new FileInfo(fileFullName); if (fInfo.Exists) { fInfo.Delete(); } DirectoryInfo dirInfo = fInfo.Directory; if (!dirInfo.Exists) { dirInfo.Create(); } StreamWriter sw = fInfo.CreateText(); sw.Write(strContent); sw.Flush(); sw.Close(); isSuccess = true; } catch (Exception ex) { MessageBox.Show("SaveContentToFile : " + ex.Message); } return isSuccess; } /// /// 获取指定目录下的子文件名集 /// /// 指定目录 ///子目录名集 private ListGetDirAllFiles(string parentDirPath, bool isFullName = false) { List listFiles = new List (); DirectoryInfo folder = new DirectoryInfo(parentDirPath.Trim()); //指定搜索文件夹 if (folder.Exists)//存在 文件夹 { FileInfo[] listFileInfos = folder.GetFiles();//取得给定文件夹下的文件夹组 if (listFiles != null) { foreach (FileInfo fileInfo in listFileInfos)//遍历 { if (isFullName) { listFiles.Add(fileInfo.FullName); } else { listFiles.Add(fileInfo.Name); } } } DirectoryInfo[] childDirInfos = folder.GetDirectories(); //子目录 foreach (DirectoryInfo dirInfo in childDirInfos) { listFiles.AddRange(GetDirAllFiles(dirInfo.FullName, isFullName)); } } return listFiles; }