檔案操作類FileUtil

乐 乐——1128發表於2024-06-24
  1 public class FileUtil
  2 {
  3     /// <summary>
  4     /// 按時間來建立資料夾
  5     /// </summary>
  6     /// <param name="path"></param>
  7     /// <returns>eg: /{yourPath}/2020/11/3/</returns>
  8     public static string GetdirPath(string path = "")
  9     {
 10         DateTime date = DateTime.Now;
 11         string timeDir = date.ToString("yyyyMMdd");// date.ToString("yyyyMM/dd/HH/");
 12 
 13         if (!string.IsNullOrEmpty(path))
 14         {
 15             timeDir = Path.Combine(path, timeDir);
 16         }
 17         return timeDir;
 18     }
 19 
 20     /// <summary>
 21     /// 取檔名的MD5值(16位)
 22     /// </summary>
 23     /// <param name="str">檔名,不包括副檔名</param>
 24     /// <returns></returns>
 25     public static string HashFileName(string str = null)
 26     {
 27         if (string.IsNullOrEmpty(str))
 28         {
 29             str = Guid.NewGuid().ToString();
 30         }
 31         MD5 md5 = MD5.Create();
 32         return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
 33     }
 34 
 35     /// <summary>
 36     /// 刪除指定目錄下的所有檔案及資料夾(保留目錄)
 37     /// </summary>
 38     /// <param name="file">檔案目錄</param>
 39     public static void DeleteDirectory(string file)
 40     {
 41         try
 42         {
 43             //判斷資料夾是否還存在
 44             if (Directory.Exists(file))
 45             {
 46                 DirectoryInfo fileInfo = new DirectoryInfo(file);
 47                 //去除資料夾的只讀屬性
 48                 fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
 49                 foreach (string f in Directory.GetFileSystemEntries(file))
 50                 {
 51                     if (File.Exists(f))
 52                     {
 53                         //去除檔案的只讀屬性
 54                         File.SetAttributes(file, FileAttributes.Normal);
 55                         //如果有子檔案刪除檔案
 56                         File.Delete(f);
 57                     }
 58                     else
 59                     {
 60                         //迴圈遞迴刪除子資料夾
 61                         DeleteDirectory(f);
 62                     }
 63                 }
 64                 //刪除空資料夾
 65                 Directory.Delete(file);
 66             }
 67 
 68         }
 69         catch (Exception ex) // 異常處理
 70         {
 71             Console.WriteLine("程式碼生成異常" + ex.Message);
 72         }
 73     }
 74 
 75     /// <summary>
 76     /// 壓縮程式碼
 77     /// </summary>
 78     /// <param name="zipPath"></param>
 79     /// <param name="genCodePath"></param>
 80     /// <param name="zipFileName">壓縮後的檔名</param>
 81     /// <returns></returns>
 82     public static bool ZipGenCode(string zipPath, string genCodePath, string zipFileName)
 83     {
 84         if (string.IsNullOrEmpty(zipPath)) return false;
 85         try
 86         {
 87             CreateDirectory(genCodePath);
 88             string zipFileFullName = Path.Combine(zipPath, zipFileName);
 89             if (File.Exists(zipFileFullName))
 90             {
 91                 File.Delete(zipFileFullName);
 92             }
 93 
 94             ZipFile.CreateFromDirectory(genCodePath, zipFileFullName);
 95             DeleteDirectory(genCodePath);
 96 
 97             return true;
 98         }
 99         catch (Exception ex)
100         {
101             Console.WriteLine("壓縮檔案出錯。" + ex.Message);
102             return false;
103         }
104     }
105 
106     /// <summary>
107     /// 建立資料夾
108     /// </summary>
109     /// <param name="path"></param>
110     /// <returns></returns>
111     public static bool CreateDirectory(string path)
112     {
113         if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
114         {
115             path = path.Replace("\\", "/").Replace("//", "/");
116         }
117         try
118         {
119             if (!Directory.Exists(path))
120             {
121                 DirectoryInfo info = Directory.CreateDirectory(path);
122                 Console.WriteLine("不存在建立資料夾" + info);
123             }
124         }
125         catch (Exception ex)
126         {
127             Console.WriteLine($"建立資料夾出錯了,{ex.Message}");
128             return false;
129         }
130         return true;
131     }
132 
133     /// <summary>
134     /// 寫檔案
135     /// </summary>
136     /// <param name="path">完整路徑帶副檔名的</param>
137     /// <param name="content"></param>
138     public static void WriteAndSave(string path, string content)
139     {
140         if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
141         {
142             path = path.Replace("\\", "/").Replace("//", "/");
143         }
144         if (!Directory.Exists(Path.GetDirectoryName(path)))
145         {
146             Directory.CreateDirectory(Path.GetDirectoryName(path));
147         }
148         Console.WriteLine("開始寫入檔案,Path=" + path);
149         try
150         {
151             //例項化一個檔案流--->與寫入檔案相關聯
152             using var fs = new FileStream(path, FileMode.Create, FileAccess.Write);
153             //例項化一個StreamWriter-->與fs相關聯
154             using var sw = new StreamWriter(fs);
155             //開始寫入
156             sw.Write(content);
157             //清空緩衝區
158             sw.Flush();
159             //關閉流
160             sw.Close();
161             fs.Close();
162         }
163         catch (Exception ex)
164         {
165             Console.WriteLine("寫入檔案出錯了:" + ex.Message);
166         }
167     }
168 }

上面只是一部分,主要的看重的檔案壓縮方法,後續會繼續增加

相關文章