FileHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. /// <summary>
  8. /// 文件帮助类
  9. /// </summary>
  10. public static class FileHelper
  11. {
  12. #region 私有属性
  13. /// <summary>
  14. /// 缓存/缓冲区
  15. /// </summary>
  16. private static byte[] m_CacheBuffer = new byte[4096];
  17. #endregion
  18. #region 公有方法
  19. /// <summary>
  20. /// 获取所有数据
  21. /// </summary>
  22. /// <param name="_filePath"></param>
  23. /// <returns></returns>
  24. public static string GetAllDataInfo(string _filePath)
  25. {
  26. if (string.IsNullOrEmpty(_filePath))
  27. {
  28. return String.Empty;
  29. }
  30. return File.ReadAllText(_filePath);
  31. }
  32. /// <summary>
  33. /// 从StreamingAssets文件夹下获取数据信息(IO模式)
  34. /// </summary>
  35. /// <param name="_path">路径集合,不需要填写streamingAssetsPath</param>
  36. /// <returns></returns>
  37. public static string GetAllDataInfoForStreamingAssets(params string[] _filePath)
  38. {
  39. if (_filePath.Length <= 0)
  40. {
  41. return string.Empty;
  42. }
  43. string result = String.Empty;
  44. result = GetAllDataInfo(Application.streamingAssetsPath + "/" + PathHelper.CombineFilePath(_filePath));
  45. return result;
  46. }
  47. /// <summary>
  48. /// 将对应文件转换为二进制数组
  49. /// </summary>
  50. /// <param name="_filePath"></param>
  51. /// <returns></returns>
  52. public static byte[] GetAllBytes(string _filePath)
  53. {
  54. return File.ReadAllBytes(_filePath);
  55. }
  56. /// <summary>
  57. /// 获取指定文件夹下所有指定类型的文件
  58. /// </summary>
  59. /// <param name="_searchPattern">文件后缀,带点号</param>
  60. /// <param name="_paths"></param>
  61. /// <returns></returns>
  62. public static FileInfo[] GetFileOfSpecifiedType(string _searchPattern, params string[] _paths)
  63. {
  64. string folderPath = PathHelper.CombineFilePath(_paths).Replace('/', '\\');
  65. return GetAllFileInfo(_searchPattern, folderPath);
  66. }
  67. /// <summary>
  68. /// 获取指定文件夹下所有指定类型的文件
  69. /// </summary>
  70. /// <param name="_searchPattern">文件后缀,带点号</param>
  71. /// <param name="_folderPath"></param>
  72. /// <returns></returns>
  73. public static FileInfo[] GetFileOfSpecifiedType(string _searchPattern, string _folderPath)
  74. {
  75. return GetAllFileInfo(_searchPattern, _folderPath);
  76. }
  77. /// <summary>
  78. /// 保存文本文件到本地
  79. /// </summary>
  80. /// <param name="_targetContent"></param>
  81. /// <param name="_paths"></param>
  82. public static void SaveTextToFile(string _targetContent, params string[] _paths)
  83. {
  84. if (string.IsNullOrEmpty(_targetContent) || _paths.Length <= 0)
  85. {
  86. return;
  87. }
  88. // 合并路径
  89. string savePath = PathHelper.CombineFilePath(_paths);
  90. // 获取文件夹路径
  91. string foldrePath = Path.GetDirectoryName(savePath);
  92. // 判断文件夹是否存在
  93. if (!Directory.Exists(foldrePath))
  94. {
  95. // 创建文件夹
  96. Directory.CreateDirectory(foldrePath);
  97. }
  98. // 写入文件
  99. File.WriteAllText(savePath,_targetContent);
  100. }
  101. /// <summary>
  102. /// 写入Json数据
  103. /// </summary>
  104. /// <param name="_t"></param>
  105. /// <param name="_paths"></param>
  106. /// <typeparam name="T"></typeparam>
  107. public static void WriteJsonData<T>(T _t, params string[] _paths)
  108. {
  109. if (_t == null || _paths.Length <= 0)
  110. {
  111. return;
  112. }
  113. // 合并路径
  114. string filePath = PathHelper.CombineFilePath(_paths);
  115. if (!File.Exists(filePath))
  116. {
  117. return;
  118. }
  119. string tmpContent = JsonUtility.ToJson(_t).Replace(@"\\r\\n",@"\r\n");
  120. using (StreamWriter streamWriter = new StreamWriter(filePath))
  121. {
  122. streamWriter.Write(tmpContent);
  123. }
  124. }
  125. /// <summary>
  126. /// 拷贝文件到对应位置
  127. /// </summary>
  128. /// <param name="_sourceFilePath">源文件路径</param>
  129. /// <param name="_destinationPath">目标文件夹路径</param>
  130. /// <returns>返回拷贝文件的名称</returns>
  131. public static string CopeFileTocorrespondlocation(string _sourceFilePath, string _destinationPath)
  132. {
  133. if (string.IsNullOrEmpty(_sourceFilePath) || string.IsNullOrEmpty(_destinationPath))
  134. {
  135. return String.Empty;
  136. }
  137. // 调用删除方法
  138. DeleteFilewithSameName(_sourceFilePath, _destinationPath);
  139. string tmpFileName = GetSourceFileName(_sourceFilePath);
  140. string tmpCompletePath = GetDestinationFilePath(tmpFileName, _destinationPath);
  141. // 拷贝文件
  142. File.Copy(_sourceFilePath,tmpCompletePath,true);
  143. return tmpFileName;
  144. }
  145. /// <summary>
  146. /// 通过协程的形式上传文件
  147. /// </summary>
  148. /// <param name="_sourceFilePath"></param>
  149. /// <param name="_destinationPath"></param>
  150. /// <param name="_action">返回音频文件名</param>
  151. /// <param name="_progressBar"></param>
  152. /// <returns></returns>
  153. public static IEnumerator UploadFileCoroutine(string _sourceFilePath, string _destinationPath,Action<string> _action,Action<float> _progressBar)
  154. {
  155. if (string.IsNullOrEmpty(_sourceFilePath) || string.IsNullOrEmpty(_destinationPath))
  156. {
  157. yield return null;
  158. }
  159. DeleteFilewithSameName(_sourceFilePath, _destinationPath);
  160. string tmpFileName = GetSourceFileName(_sourceFilePath);
  161. string tmpCompletePath = GetDestinationFilePath(tmpFileName, _destinationPath);
  162. if (_action != null)
  163. {
  164. _action.Invoke(tmpFileName);
  165. }
  166. if (string.IsNullOrEmpty(_sourceFilePath))
  167. {
  168. yield return null;
  169. }
  170. // 文件拷贝操作
  171. using (FileStream sourceFile = File.OpenRead(_sourceFilePath))
  172. {
  173. using (FileStream destinationFile=File.Create(tmpCompletePath))
  174. {
  175. // 获取源文件大小
  176. long totalSourceLength = sourceFile.Length;
  177. // 记录拷贝数值
  178. long bytesCopied = 0;
  179. while (bytesCopied < totalSourceLength)
  180. {
  181. // 读取源文件中的数据到缓冲区
  182. int bytesRead = sourceFile.Read(m_CacheBuffer, 0, m_CacheBuffer.Length);
  183. // 写入缓冲区数据到目标文件
  184. destinationFile.Write(m_CacheBuffer, 0, bytesRead);
  185. // 修订拷贝长度
  186. bytesCopied += bytesRead;
  187. // 计算加载进度
  188. if (_progressBar != null)
  189. {
  190. _progressBar.Invoke((float)bytesCopied / (float)totalSourceLength);
  191. }
  192. yield return null;
  193. }
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// 删除目标文件夹下与传入文件名相同的文件
  199. /// </summary>
  200. /// <param name="_sourceFilePath">源文件路径</param>
  201. /// <param name="_folderPath">目标文件夹</param>
  202. public static void DeleteFilewithSameName(string _sourceFilePath,string _folderPath)
  203. {
  204. if (string.IsNullOrEmpty(_sourceFilePath) || string.IsNullOrEmpty(_folderPath))
  205. {
  206. return;
  207. }
  208. // 获取源文件名
  209. string sourceFileName = Path.GetFileName(_sourceFilePath);
  210. // 获取指定文件夹下所有的文件
  211. FileInfo[] fileInfos = GetFileOfSpecifiedType(sourceFileName.Split('.')[1], _folderPath);
  212. // 目标文件夹无数据
  213. if (fileInfos.Length <= 0)
  214. {
  215. return;
  216. }
  217. for (int i = 0; i < fileInfos.Length; i++)
  218. {
  219. // 获取文件名 如果相同,则直接删除
  220. if (sourceFileName.Equals(Path.GetFileName(fileInfos[i].FullName)))
  221. {
  222. DeleteLocalFile(fileInfos[i].FullName);
  223. return;
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// 删除文件
  229. /// </summary>
  230. /// <param name="_folderPath"></param>
  231. /// <param name="_fileName">带后缀文件名</param>
  232. public static void DeleteFile(string _folderPath,string _fileName)
  233. {
  234. if (string.IsNullOrEmpty(_folderPath) || string.IsNullOrEmpty(_fileName))
  235. {
  236. return;
  237. }
  238. string filePath = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath, _folderPath, _fileName);
  239. DeleteLocalFile(filePath);
  240. }
  241. #endregion
  242. #region 私有方法
  243. /// <summary>
  244. /// 本地是否存在指定文件
  245. /// </summary>
  246. /// <param name="_filePath"></param>
  247. /// <returns></returns>
  248. private static bool LoaclIsExistFile(string _filePath)
  249. {
  250. return File.Exists(_filePath);
  251. }
  252. /// <summary>
  253. /// 删除本地指定文件
  254. /// </summary>
  255. /// <param name="_filePath"></param>
  256. private static void DeleteLocalFile(string _filePath)
  257. {
  258. if (LoaclIsExistFile(_filePath))
  259. {
  260. File.Delete(_filePath);
  261. }
  262. }
  263. /// <summary>
  264. /// 获取指定文件夹下的所有文件信息
  265. /// </summary>
  266. /// <param name="_searchPattern"></param>
  267. /// <param name="_folderPath"></param>
  268. /// <returns></returns>
  269. private static FileInfo[] GetAllFileInfo(string _searchPattern, string _folderPath)
  270. {
  271. // 获取指定文件夹信息
  272. DirectoryInfo tmpDirectoryInfo = new DirectoryInfo(_folderPath);
  273. // 获取文件夹下所有的文件信息
  274. FileInfo[] tmpFileInfos = tmpDirectoryInfo.GetFiles("*" + _searchPattern, SearchOption.AllDirectories);
  275. return tmpFileInfos;
  276. }
  277. /// <summary>
  278. /// 获取构建目标文件完整路径
  279. /// </summary>
  280. /// <param name="_sourceFilePath"></param>
  281. /// <param name="_destinationPath"></param>
  282. /// <returns>返回源文件文件名和构建后的目标我呢见完整路径</returns>
  283. private static string GetDestinationFilePath(string _sourceFileName, string _destinationPath)
  284. {
  285. // 构建目标文件完整路径
  286. string destinationFilePath = PathHelper.CombineFilePath(_destinationPath, _sourceFileName);
  287. return PathHelper.CombineFilePath(_destinationPath, _sourceFileName);
  288. }
  289. /// <summary>
  290. /// 获取源文件文件名
  291. /// </summary>
  292. /// <param name="_sourceFilePath"></param>
  293. /// <returns></returns>
  294. private static string GetSourceFileName(string _sourceFilePath)
  295. {
  296. return Path.GetFileName(_sourceFilePath);
  297. }
  298. #endregion
  299. }