程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#從指定目錄批量壓縮文件

c#從指定目錄批量壓縮文件

編輯:C#入門知識

public static string GetFoldAll(string Path)
        {
            string str = "";
            DirectoryInfo thisOne = new DirectoryInfo(Path);
            DirectoryInfo[] subDirectories = thisOne.GetDirectories();//獲得目錄
            for (int i = 0; i < subDirectories.Length; i++)
            {
                str = ListTreeShow(subDirectories[i], 0, str);
            }
            return str;
        }
        /// <summary>
        /// 獲取指定文件夾下所有子目錄及文件函數
        /// </summary>
        /// <param name="theDir">指定目錄</param>
        /// <param name="nLevel">默認起始值,調用時,一般為0</param>
        /// <param name="Rn">用於迭加的傳入值,一般為空</param>
        /// <returns></returns>
        public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)//遞歸目錄 文件
        {
            try
            {
                DirectoryInfo[] subDirectories = theDir.GetDirectories();//獲得目錄
                foreach (DirectoryInfo dirinfo in subDirectories)
                {
                    FileInfo[] fileInfo = dirinfo.GetFiles(); //目錄下的文件
                    foreach (FileInfo fInfo in fileInfo)
                    {
                            string filepath = dirinfo.FullName + "\\" + fInfo.Name;
                            //時間部分判斷 文件是否刪除
                            string suffix = "";
                            DateTime dt = DateTime.Now;
                            string[] fielName = fInfo.Name.Split('.');
                            string linedate = fielName[0].Split('_')[fielName[0].Split('_').Length - 1];//獲取文件名稱中的時間部分
                            if (fielName.Length > 2)
                            {
                                suffix = fielName[2];
                            }
                            else
                            {
                                suffix = fielName[1];
                            }
                            if (linedate == "rev")
                            {
                                dt = DateTime.ParseExact(fielName[0].Split('_')[fielName[0].Split('_').Length - 2], "yyyyMMdd", null);
                            }
                            else
                            {
                                dt = DateTime.ParseExact(linedate, "yyyyMMdd", null);
                            }
                            DateTime time = DateTime.Now;
                            if (dt < time.AddYears(-1))//以當前時間為基礎,早於一年的報表刪掉
                            {
                                DeleteFile(filepath);
                            }
                            if (dt.AddDays(1) < time)
                            {
                                if (suffix == "txt" || suffix == "csv")
                                {
                                    ZipIt(filepath);
                                    DeleteFile(filepath);
                                }
                            }
                        }
                        Rn = ListTreeShow(dirinfo, nLevel + 1, Rn);
                }
            }
            catch (Exception ex)
            {
                log.Info(ex);
            }
            return Rn;
        }

        private static String ZipIt(String filename)
        {
            ZipFile zipfile = new ZipFile(System.Text.Encoding.Default);
            string zipname = "";
            try
            {
                //壓縮文件
                zipfile.AddFile(filename, "");
                zipfile.Save(filename + ".zip");
                Console.WriteLine("zip the file: {0} success!", filename);
                string[] fname = zipfile.Name.Split('\\');
                zipname = fname[fname.Length - 1];
            }
            catch (Exception ex)
            {
                log.Info(ex);
                Console.WriteLine("error occurred! ex={0}", ex);
                //throw ex;
            }
            log.Info("文件壓縮成功,文件名為:" + filename);
            return zipname;
        }
        private static void DeleteFile(string txtedfolder)
        {
            //foreach (string file in Directory.GetFiles(zipedfolder))
            //{
            try
            {
                File.Delete(txtedfolder);
                log.Info("成功刪除" + txtedfolder);
            }        www.2cto.com
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                log.Info("刪除文件失敗:" + ex);
            }
            //
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved