C#:文件夹匹配

//文件夹匹配:对比文件夹,相同的目录结构、所有文件名称小写相同,制定文件外的MD5值相同 ,则两个文件夹匹配成功!

        /// <summary>
        /// 批量匹配书籍H5资源包
        /// </summary>
        /// <param name="strPlateDirPath">平台书籍H5资源所在路径</param>
        /// <param name="strSourDirPath">原加工书籍包所在路径</param>
        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);
            }
        }

        /// <summary>
        /// 匹配书籍文件夹
        /// </summary>
        /// <param name="plateBookDirPath">平台书籍文件夹路径</param>
        /// <param name="srcBookDirPath">原加工书籍文件夹路径</param>
        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();
                List<string> lstSrcMathcH5Res = new List<string>();
                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;
        }

        /// <summary>
        /// 匹配H5资源目录结构
        /// </summary>
        /// <param name="plateBookResPath">平台书籍文件夹</param>
        /// <param name="srcBookResPath">原加工书籍文件夹</param>
        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));
                List<string> lstPlateH5ResDir = new List<string>();
                lstPlateH5ResDir.AddRange(arryPlateH5ResDir);
                //原加工书籍H5资源子文件夹路径集
                string[] arrySrcH5ResPath = Directory.GetDirectories(srcBookResPath);
                var arrySrcH5ResDir = arrySrcH5ResPath.Select(o => o.Replace(srcBookResPath, string.Empty));
                List<string> lstSrcH5ResDir = new List<string>();
                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;
        }

        /// <summary>
        /// 匹配H5资源
        /// </summary>
        /// <param name="plateBookResPath">平台书籍文件夹</param>
        /// <param name="srcBookResPath">原加工书籍文件夹</param>
        private bool MatchH5Reses(string plateBookResPath, string srcBookResPath)
        {
            bool isMatch = false;
            try
            {
                //平台书籍H5资源子文件路径集
                List<string> lstPlateH5ResFile = GetDirAllFiles(plateBookResPath,true);     //解决H5资源内子文件同名问题
                var lstPlateH5ResPathFile = lstPlateH5ResFile.Select(o => o.Replace(plateBookResPath, string.Empty).ToLower());
                //原加工书籍H5资源子文件夹路径集
                List<string> 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;
        }

        /// <summary>
        /// 同步书籍H5资源内文件名称
        /// </summary>
        /// <param name="plateBookDirPath">平台书籍文件夹路径</param>
        /// <param name="srcBookDirPath">原加工书籍文件夹路径</param>
        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;
        }

        /// <summary>
        /// 同步H5资源名称
        /// </summary>
        /// <param name="plateH5ResPath">平台H5文件夹</param>
        /// <param name="srcH5ResPath">原加工H5文件夹</param>
        private bool SyncH5FilesName(string plateH5ResPath, string srcH5ResPath)
        {
            bool isMatch = false;
            try
            {
                //平台书籍H5资源子文件路径集
                List<string> lstPlateH5ResFile = GetDirAllFiles(plateH5ResPath, true);     //解决H5资源内子文件同名问题
                var lstPlateH5ResPathFile = lstPlateH5ResFile.Select(o => o.Replace(plateH5ResPath, string.Empty).ToLower());
                //原加工书籍H5资源子文件夹路径集
                List<string> 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;
        }

        /// <summary>
        /// 加密、压缩书籍内的H5资源包
        /// </summary>
        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);
                }
            }
        }

        /// <summary>
        /// 加密、压缩H5资源包
        /// </summary>
        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;
        }
View Code

//文件 操作

        /// <summary>
        /// 内容保存至本地文件
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="fileFullName"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 获取指定目录下的子文件名集
        /// </summary>
        /// <param name="parentDirPath">指定目录</param>
        /// <returns>子目录名集</returns>
        private List<string> GetDirAllFiles(string parentDirPath, bool isFullName = false)
        {
            List<string> listFiles = new List<string>();
            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;
        }
View Code

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值