DataFileUtil.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using System.Text;
  5. using System.Collections;
  6. using UnityEngine.Networking;
  7. using System.Threading.Tasks;
  8. using System.Threading;
  9. using System.Collections.Generic;
  10. namespace XRTool.Util
  11. {
  12. /// <summary>
  13. /// 文件工具类
  14. /// 下载,写入文件,断点续传,边下边存
  15. /// 遍历,拷贝文件
  16. /// 获取文件的MD5等
  17. /// </summary>
  18. public class DataFileUtil
  19. {
  20. /// <summary>
  21. /// 预估的写入文件的速度,与真实速度无关,仅仅是模拟评估,预计写入用时
  22. /// </summary>
  23. public const float WriteSpeed = 1024 * 1024 * 150f;
  24. ///// <summary>
  25. ///// 写入缓存的大小,如果越小,可能会引起卡顿(频繁访问数据)
  26. ///// </summary>
  27. //public const int CacheSave = 1024 * 1024 * 2;
  28. /// <summary>
  29. /// 图片格式
  30. /// </summary>
  31. public static string[] TexturesType = new string[] { "BMP", "EXR", "GIF", "HDR", "IFF", "JPG", "PICT", "PNG", "PSD", "TGA", "TIFF" };
  32. /// <summary>
  33. /// 视频格式
  34. /// </summary>
  35. public static string[] MoviesType = new string[] { "MOV", "MPG", "MP4", "AVI", "ASF" };
  36. /// <summary>
  37. /// 音频格式
  38. /// </summary>
  39. public static string[] AudiosType = new string[] { "ACC", "AIFF", "IT", "MOD", "MPEG", "OGGVORBIS", "S3M",
  40. "AIF", "WAV", "MP3", "OGG","XM","XMA","VAG","AUDIOQUEUE" };
  41. public static int maxTryCount = 5;
  42. public const string Extension = ".downing";
  43. ///// <summary>
  44. ///// 断点下载,异步读写
  45. ///// </summary>
  46. ///// <param name="url"></param>
  47. ///// <param name="path"></param>
  48. ///// <param name="completeData"></param>
  49. ///// <param name="hander"></param>
  50. ///// <param name="downLoadProcess"></param>
  51. ///// <returns></returns>
  52. //public static IEnumerator DownLoadDataAsync(string url, string path,
  53. // Action<string, UnityWebRequest> completeData, DownloadHandler hander = null, Action<float, float> downLoadProcess = null)
  54. //{
  55. // yield return 0;
  56. //}
  57. /// <summary>
  58. /// 下载数据到本地,并将下载的数据返回给到上层应用
  59. /// 此下载仅应用于可缓存的资源,图片文本等下载,大文件的下载请使用断点下载
  60. /// </summary>
  61. /// <param name="url"></param>
  62. /// <param name="path"></param>
  63. /// <param name="downLoadProcess"></param>
  64. /// <param name="completeData"></param>
  65. /// <param name="isAsync"></param>
  66. /// <returns></returns>
  67. public static IEnumerator DownLoadData(string url, string path,
  68. Action<string, UnityWebRequest> completeData, DownloadHandler hander = null, Action<float, float> downLoadProcess = null, bool isAsync = false)
  69. {
  70. UnityWebRequest web = null;
  71. //DownloadHandlerTexture
  72. yield return RequestDownData(url, (string error, UnityWebRequest req) =>
  73. {
  74. ///下载成功后开始拷贝数据到本地
  75. if (string.IsNullOrEmpty(error))
  76. {
  77. ///同步存储方法
  78. if (!isAsync)
  79. {
  80. SaveDataToFile(path, req.downloadHandler.data);
  81. downLoadProcess?.Invoke(req.downloadHandler.data.Length, 1);
  82. completeData?.Invoke(path, req);
  83. return;
  84. }
  85. else
  86. {
  87. web = req;
  88. ///此时,已下载到内存中,但是仍未缓存至内存中,内存数据可用
  89. completeData?.Invoke(null, req);
  90. }
  91. }
  92. else
  93. {
  94. ///下载出错
  95. completeData?.Invoke(null, null);
  96. //UnityLog.LogError(error);
  97. }
  98. }, hander, (float allLeng, float process) =>
  99. {
  100. ///下载的进度,1/1.2,给存储留进度条显示,约等于0.89
  101. downLoadProcess?.Invoke(allLeng, process / 1.12f);
  102. });
  103. ///等待下载完成,此下载是下载到内存中
  104. if (isAsync && web != null)
  105. {
  106. //completeData?.Invoke(true, data);
  107. yield return SaveDataToFile(path, web.downloadHandler.data, (float saveProcess) =>
  108. {
  109. ///更新存储文件的进度
  110. downLoadProcess?.Invoke(web.downloadHandler.data.Length, saveProcess * 0.1f + 0.9f);
  111. }, (bool isSuccess, string filePath) =>
  112. {
  113. completeData?.Invoke(filePath, web);
  114. });
  115. }
  116. web.Dispose();
  117. }
  118. /// <summary>
  119. /// 请求后台数据接口
  120. /// </summary>
  121. /// <returns></returns>
  122. public static IEnumerator RequestData(string url, Action<string> reqComplete, bool isPost = true,
  123. Dictionary<string, string> header = null, Dictionary<string, string> form = null, Action complete = null)
  124. {
  125. ///表单
  126. using (UnityWebRequest web = isPost ? UnityWebRequest.Post(url, form) : UnityWebRequest.Get(url))
  127. {
  128. if (header != null)
  129. {
  130. foreach (var item in header)
  131. {
  132. web.SetRequestHeader(item.Key, item.Value);
  133. }
  134. }
  135. yield return web.SendWebRequest();
  136. if (web.isNetworkError || web.isHttpError)
  137. {
  138. UnityLog.LogError("RequestData:" + url + web.error);
  139. reqComplete?.Invoke(null);
  140. complete?.Invoke();
  141. yield break;
  142. }
  143. else
  144. {
  145. reqComplete?.Invoke(web.downloadHandler.text);
  146. complete?.Invoke();
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// 获取资源的大小
  152. /// </summary>
  153. /// <param name="url"></param>
  154. /// <param name="getLen"></param>
  155. /// <returns></returns>
  156. public static IEnumerator GetDataSize(string url, Action<long> getLen)
  157. {
  158. var headRequest = UnityWebRequest.Head(url);
  159. yield return headRequest.SendWebRequest();
  160. getLen?.Invoke(long.Parse(headRequest.GetResponseHeader("Content-Length")));
  161. headRequest.Dispose();
  162. }
  163. /// <summary>
  164. /// 断点续传
  165. /// 下载大文件请使用此函数
  166. /// 注意,请自行检查是否已下载过此文件,此函数不再检查,直接执行下载
  167. /// </summary>
  168. /// <param name="url"></param>
  169. /// <param name="path"></param>
  170. /// <param name="completeData"></param>
  171. /// <param name="hander"></param>
  172. /// <param name="downLoadProcess"></param>
  173. /// <returns></returns>
  174. public static IEnumerator DownLoadDataAsync(string url, string path,
  175. Action<string, UnityWebRequest> completeData, DownloadHandler hander = null, Action<float, float> downLoadProcess = null)
  176. {
  177. ///获取文件的大小
  178. FileStream fs = null;
  179. long curLen = 0;
  180. string tmpPath = path + Extension;
  181. ///文件读写异常,返回错误
  182. try
  183. {
  184. string dir = Path.GetDirectoryName(tmpPath);
  185. if (!Directory.Exists(dir))
  186. {
  187. Directory.CreateDirectory(dir);
  188. }
  189. fs = new FileStream(tmpPath, FileMode.OpenOrCreate);
  190. curLen = fs.Length;
  191. UnityLog.Log("当前文件大小" + curLen, 2);
  192. }
  193. catch (Exception ex)
  194. {
  195. if (fs != null)
  196. {
  197. fs.Dispose();
  198. }
  199. fs = null;
  200. UnityLog.LogError(tmpPath + ex.ToString());
  201. downLoadProcess.Invoke(curLen, 0);
  202. completeData?.Invoke(null, null);
  203. yield break;
  204. }
  205. ///请求获取文件的大小
  206. ///已下载完成,无需再次下载
  207. long totalLength = 0;
  208. yield return GetDataSize(url, (size) =>
  209. {
  210. totalLength = size;
  211. });
  212. ///当前文件的大小和云端文件大小一致
  213. ///理论上不会出现这个问题
  214. if (curLen == totalLength)
  215. {
  216. fs.Dispose();
  217. fs.Close();
  218. downLoadProcess.Invoke(totalLength, 1);
  219. ReNameFile(tmpPath,path);
  220. completeData?.Invoke(path, null);
  221. yield break;
  222. }
  223. ///文件异常,删除文件,重新下载
  224. ///理论上不会出现这个问题
  225. else if (curLen > totalLength)
  226. {
  227. UnityLog.LogError("文件大小异常" + curLen + "__" + totalLength);
  228. fs.Dispose();
  229. fs.Close();
  230. File.Delete(tmpPath);
  231. fs = new FileStream(tmpPath, FileMode.OpenOrCreate, FileAccess.Write);
  232. curLen = fs.Length;
  233. }
  234. fs.Seek(curLen, SeekOrigin.Begin);
  235. bool isSuccess = true;
  236. yield return RequestDownData(url, (byte[] data, int len) =>
  237. {
  238. if (isSuccess && data.Length > 0)
  239. {
  240. try
  241. {
  242. fs.Write(data, 0, len);
  243. fs.Flush();
  244. curLen += len;
  245. downLoadProcess?.Invoke(curLen, curLen * 1f / totalLength);
  246. }
  247. catch (Exception ex)
  248. {
  249. UnityLog.LogError(ex.ToString());
  250. isSuccess = false;
  251. return;
  252. }
  253. }
  254. else
  255. {
  256. ///理论上不会出现这个问题,除非出现异常情况如内存不足等
  257. //complete?.Invoke(isSuccess, path);
  258. UnityLog.LogError("Down Error:" + url);
  259. }
  260. }, totalLength, curLen, (web) =>
  261. {
  262. fs.Dispose();
  263. fs.Close();
  264. downLoadProcess.Invoke(totalLength, 1);
  265. ReNameFile(tmpPath,path);
  266. completeData?.Invoke(path, web);
  267. });
  268. }
  269. /// <summary>
  270. /// Get请求下载数据,断点下载,边下载边返回数据进行存储
  271. /// </summary>
  272. /// <param name="url">url</param>
  273. /// <param name="downData">返回获取的数据</param>
  274. /// <param name="allLen">文件的总大小</param>
  275. /// <param name="downedLength">已下载的数据</param>
  276. /// <returns></returns>
  277. public static IEnumerator RequestDownData(string url, Action<byte[], int> downData, long allLen, long downedLength = 0,
  278. Action<UnityWebRequest> complete = null)
  279. {
  280. //float downTime = Time.time;
  281. using (UnityWebRequest web = UnityWebRequest.Get(url))
  282. {
  283. web.disposeDownloadHandlerOnDispose = true;
  284. DataDownLoadHandler ddh = new DataDownLoadHandler();
  285. web.downloadHandler = ddh;
  286. ddh.OnReceiveData += downData;
  287. ///请求指定的长度
  288. web.SetRequestHeader("Range", "bytes=" + downedLength + "-" + allLen);
  289. yield return web.SendWebRequest();
  290. if (web.isNetworkError || web.isHttpError)
  291. {
  292. UnityLog.LogError("RequestDownData" + url);
  293. complete?.Invoke(null);
  294. }
  295. else
  296. {
  297. complete?.Invoke(web);
  298. }
  299. }
  300. }
  301. /// <summary>
  302. /// 从URl下载指定的资源
  303. /// </summary>
  304. /// <param name="url">资源的地址,可以是网络路径,也可以是本地文件路径</param>
  305. /// <param name="updateProcess">下载的进度,当前已下载文件大小,以及进度</param>
  306. /// <param name="complete"></param>
  307. /// <param name="form">请求的参数,如果为空代表Get请求,不为空代表Post请求</param>
  308. /// <returns></returns>
  309. public static IEnumerator RequestDownData(string url, Action<string, UnityWebRequest> complete, DownloadHandler hander = null, Action<float, float> updateProcess = null, WWWForm form = null)
  310. {
  311. UnityWebRequest web;
  312. Debug.Log("RequestDownData" + url);
  313. if (form == null)
  314. {
  315. web = UnityWebRequest.Get(url);
  316. }
  317. else
  318. {
  319. web = UnityWebRequest.Post(url, form);
  320. }
  321. if (hander != null)
  322. {
  323. web.downloadHandler = hander;
  324. }
  325. web.SendWebRequest();
  326. while (!web.isDone)
  327. {
  328. updateProcess?.Invoke(web.downloadedBytes, web.downloadProgress);
  329. yield return 0;
  330. }
  331. if (web.isDone)
  332. {
  333. updateProcess?.Invoke(web.downloadedBytes, 1);
  334. }
  335. if (web.isNetworkError || web.isHttpError)
  336. {
  337. complete?.Invoke(web.error, web);
  338. Debug.LogError(web.error + url);
  339. }
  340. else
  341. {
  342. complete?.Invoke(null, web);
  343. }
  344. web.Dispose();
  345. }
  346. /// <summary>
  347. /// 上传数据到服务端,并得到服务器返回的结果
  348. /// 与下面的RequestDownData类似,但是更新上传进度,适用于文件上传
  349. /// </summary>
  350. /// <param name="url"></param>
  351. /// <param name="form"></param>
  352. /// <param name="updateProcess"></param>
  353. /// <param name="complete"></param>
  354. /// <returns></returns>
  355. public static IEnumerator RequestUpLoadData(string url, WWWForm form, Action<float, float> updateProcess, Action<string, UnityWebRequest> complete)
  356. {
  357. UnityWebRequest web = UnityWebRequest.Post(url, form);
  358. web.SendWebRequest();
  359. while (!web.isDone)
  360. {
  361. updateProcess?.Invoke(web.uploadedBytes, web.uploadProgress);
  362. yield return 0;
  363. }
  364. if (web.isDone)
  365. {
  366. updateProcess?.Invoke(web.uploadedBytes, 1);
  367. }
  368. if (web.isNetworkError || web.isHttpError)
  369. {
  370. complete?.Invoke(web.error, web);
  371. }
  372. else
  373. {
  374. complete?.Invoke(null, web);
  375. }
  376. web.Dispose();
  377. }
  378. /// <summary>
  379. /// 是否是忽略的文件类型
  380. /// </summary>
  381. /// <param name="sug"></param>
  382. /// <returns></returns>
  383. public static bool isIgnoreExtension(string ignoreExtension, string suffix)
  384. {
  385. string[] igs = ignoreExtension.Split('|', ' ', ',');
  386. for (int i = 0; i < igs.Length; i++)
  387. {
  388. if (igs[i] == suffix)
  389. {
  390. return true;
  391. }
  392. }
  393. return false;
  394. }
  395. /// <summary>
  396. /// 保存文本文件到本地
  397. /// 同步方法
  398. /// </summary>
  399. public static bool SaveDataToFile(string path, string txt)
  400. {
  401. try
  402. {
  403. if (!string.IsNullOrEmpty(txt))
  404. {
  405. File.WriteAllText(path, txt);
  406. return true;
  407. }
  408. }
  409. catch (Exception ex)
  410. {
  411. UnityLog.LogError(path + ex.ToString());
  412. }
  413. return false;
  414. }
  415. /// <summary>
  416. /// 保存二进制字节文件到本地,同步方法
  417. /// </summary>
  418. /// <param name="path"></param>
  419. /// <param name="data"></param>
  420. public static bool SaveDataToFile(string path, byte[] data)
  421. {
  422. try
  423. {
  424. string dir = Path.GetDirectoryName(path);
  425. if (!Directory.Exists(dir))
  426. {
  427. Directory.CreateDirectory(dir);
  428. }
  429. FileStream fs = new FileStream(path, FileMode.Create);
  430. fs.Write(data, 0, data.Length);
  431. fs.Dispose();
  432. fs.Close();
  433. return true;
  434. }
  435. catch (Exception ex)
  436. {
  437. UnityLog.LogError(path + ex.ToString());
  438. }
  439. return false;
  440. }
  441. /// <summary>
  442. /// 异步保存文本文件
  443. /// </summary>
  444. /// <param name="path"></param>
  445. /// <param name="txt"></param>
  446. /// <param name="saveProcess"></param>
  447. /// <returns></returns>
  448. public static IEnumerator SaveDataToFile(string path, string txt, Action<float> saveProcess, Action<bool, string> onComplete)
  449. {
  450. yield return SaveDataToFile(path, Encoding.Default.GetBytes(txt), saveProcess, onComplete);
  451. }
  452. /// <summary>
  453. /// 保存二进制字节文件到本地,异步方法
  454. /// </summary>
  455. /// <param name="path"></param>
  456. /// <param name="data"></param>
  457. private static IEnumerator SaveDataToFile(string path, byte[] data, Action<float> saveProcess, Action<bool, string> onComplete)
  458. {
  459. bool isSuccess = false;
  460. if (data != null && data.Length > 0)
  461. {
  462. Task task = null;
  463. FileStream fs = null;
  464. float allData = data.Length;
  465. try
  466. {
  467. fs = new FileStream(path, FileMode.Create);
  468. task = fs.WriteAsync(data, 0, data.Length);
  469. isSuccess = true;
  470. }
  471. catch (Exception ex)
  472. {
  473. UnityLog.LogError(path + ex.ToString());
  474. path = ex.ToString();
  475. isSuccess = false;
  476. }
  477. float tmp = 0;
  478. float allTime = data.Length / WriteSpeed;
  479. ///预计存储完成的时间
  480. while (task != null && !task.IsCompleted)
  481. {
  482. if (tmp < allTime)
  483. {
  484. tmp += Time.deltaTime;
  485. }
  486. saveProcess?.Invoke(tmp / allTime);
  487. yield return 0;
  488. }
  489. saveProcess(1);
  490. fs.Dispose();
  491. fs.Close();
  492. }
  493. onComplete?.Invoke(isSuccess, path);
  494. }
  495. /// <summary>
  496. /// 广度优先,遍历文件
  497. /// </summary>
  498. /// <param name="path"></param>
  499. /// <param name="callBack"></param>
  500. public static void FindFileBreadth(string path, Action<string> callBack)
  501. {
  502. try
  503. {
  504. DirectoryInfo di = new DirectoryInfo(path);
  505. FileInfo[] fis = di.GetFiles();
  506. for (int i = 0; i < fis.Length; i++)
  507. {
  508. callBack?.Invoke(fis[i].FullName);
  509. }
  510. DirectoryInfo[] dis = di.GetDirectories();
  511. for (int j = 0; j < dis.Length; j++)
  512. {
  513. FindFileBreadth(dis[j].FullName, callBack);
  514. }
  515. }
  516. catch (Exception ex)
  517. {
  518. UnityLog.LogError(ex.ToString());
  519. }
  520. }
  521. /// <summary>
  522. /// 深度优先,遍历文件
  523. /// </summary>
  524. /// <param name="dir"></param>
  525. public static void FindFileByDepth(string dir, Action<string> callBack)
  526. {
  527. try
  528. {
  529. DirectoryInfo d = new DirectoryInfo(dir);
  530. FileSystemInfo[] fsinfos = d.GetFileSystemInfos();
  531. foreach (FileSystemInfo fsinfo in fsinfos)
  532. {
  533. if (fsinfo is DirectoryInfo)
  534. {
  535. FindFileByDepth(fsinfo.FullName, callBack);
  536. }
  537. else
  538. {
  539. callBack?.Invoke(fsinfo.FullName);
  540. }
  541. }
  542. }
  543. catch (Exception ex)
  544. {
  545. UnityLog.LogError(ex.ToString());
  546. }
  547. }
  548. /// <summary>
  549. /// 获取文件MD5值
  550. /// </summary>
  551. /// <param name="fileName">文件绝对路径</param>
  552. /// <returns>MD5值</returns>
  553. public static string GetMD5HashFromFile(string fileName)
  554. {
  555. try
  556. {
  557. FileStream file = new FileStream(fileName, FileMode.Open);
  558. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  559. byte[] retVal = md5.ComputeHash(file);
  560. file.Close();
  561. StringBuilder sb = new StringBuilder();
  562. for (int i = 0; i < retVal.Length; i++)
  563. {
  564. sb.Append(retVal[i].ToString("x2"));
  565. }
  566. return sb.ToString();
  567. }
  568. catch (Exception ex)
  569. {
  570. throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
  571. }
  572. }
  573. /// <summary>
  574. /// 重命名文件
  575. /// </summary>
  576. /// <param name="file"></param>
  577. /// <param name="newFile"></param>
  578. public static void ReNameFile(string file, string newFile)
  579. {
  580. if (File.Exists(file))
  581. {
  582. try
  583. {
  584. string dir = Path.GetDirectoryName(newFile);
  585. if (!Directory.Exists(dir))
  586. {
  587. Directory.CreateDirectory(dir);
  588. }
  589. if (File.Exists(newFile))
  590. {
  591. File.Delete(newFile);
  592. }
  593. FileInfo info = new FileInfo(file);
  594. info.MoveTo(newFile);
  595. }
  596. catch (Exception ex)
  597. {
  598. UnityLog.LogError(file + " is error" + ex.ToString());
  599. }
  600. }
  601. }
  602. /// <summary>
  603. /// 删除文件
  604. /// </summary>
  605. /// <param name="file"></param>
  606. /// <param name="newFile"></param>
  607. public static void DelFile(string file)
  608. {
  609. UnityLog.Log(file + " is Del", 3);
  610. if (File.Exists(file))
  611. {
  612. try
  613. {
  614. File.Delete(file);
  615. }
  616. catch (Exception ex)
  617. {
  618. UnityLog.LogError(file + " is error" + ex.ToString());
  619. }
  620. }
  621. }
  622. /// <summary>
  623. /// 拷贝文件
  624. /// </summary>
  625. /// <param name="SourcePath"></param>
  626. /// <param name="DestinationPath"></param>
  627. /// <param name="overwriteexisting">是否覆盖</param>
  628. /// <returns></returns>
  629. public static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
  630. {
  631. bool ret = false;
  632. try
  633. {
  634. SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
  635. DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";
  636. if (Directory.Exists(SourcePath))
  637. {
  638. if (Directory.Exists(DestinationPath) == false)
  639. Directory.CreateDirectory(DestinationPath);
  640. foreach (string fls in Directory.GetFiles(SourcePath))
  641. {
  642. FileInfo flinfo = new FileInfo(fls);
  643. flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
  644. }
  645. foreach (string drs in Directory.GetDirectories(SourcePath))
  646. {
  647. DirectoryInfo drinfo = new DirectoryInfo(drs);
  648. if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
  649. ret = false;
  650. }
  651. }
  652. ret = true;
  653. }
  654. catch (Exception ex)
  655. {
  656. ret = false;
  657. UnityLog.LogError(ex.ToString());
  658. }
  659. return ret;
  660. }
  661. /// <summary>
  662. /// 文件是否是图片
  663. /// </summary>
  664. /// <param name="fileName"></param>
  665. /// <returns></returns>
  666. public static bool IsMoviesFile(string fileName)
  667. {
  668. if (!string.IsNullOrEmpty(fileName))
  669. {
  670. string exten = Path.GetExtension(fileName);
  671. return IsMediaType(exten.ToUpper(), MoviesType);
  672. }
  673. return false;
  674. }
  675. public static bool IsTextureFile(string fileName)
  676. {
  677. if (!string.IsNullOrEmpty(fileName))
  678. {
  679. string exten = Path.GetExtension(fileName);
  680. return IsMediaType(exten.ToUpper(), TexturesType);
  681. }
  682. return false;
  683. }
  684. /// <summary>
  685. /// 判断是否是音频文件
  686. /// MP3,WAV等
  687. /// </summary>
  688. /// <param name="fileName"></param>
  689. /// <returns></returns>
  690. public static bool IsAudioFile(string fileName)
  691. {
  692. if (!string.IsNullOrEmpty(fileName))
  693. {
  694. string exten = Path.GetExtension(fileName);
  695. return IsMediaType(exten.ToUpper(), AudiosType);
  696. }
  697. return false;
  698. }
  699. /// <summary>
  700. /// 是否是多媒体文件:图片,视频,音频
  701. /// </summary>
  702. /// <param name="fileName"></param>
  703. /// <param name="types"></param>
  704. /// <returns></returns>
  705. public static bool IsMediaType(string fileName, string[] types)
  706. {
  707. if (!string.IsNullOrEmpty(fileName) && types != null)
  708. {
  709. for (int i = 0; i < types.Length; i++)
  710. {
  711. if (TexturesType[i] == fileName)
  712. {
  713. return true;
  714. }
  715. }
  716. }
  717. return false;
  718. }
  719. /// <summary>
  720. /// 是否多媒体文件
  721. /// 音频,视频,图片
  722. /// </summary>
  723. /// <param name="fileName"></param>
  724. /// <returns></returns>
  725. public static bool IsMediaType(string fileName)
  726. {
  727. if (!string.IsNullOrEmpty(fileName))
  728. {
  729. string exten = Path.GetExtension(fileName);
  730. exten = exten.ToUpper();
  731. return IsMediaType(exten, TexturesType) || IsMediaType(exten, MoviesType) || IsMediaType(exten, AudiosType);
  732. }
  733. return false;
  734. }
  735. /// <summary>
  736. /// 是否存在某文件
  737. /// </summary>
  738. /// <param name="file"></param>
  739. /// <returns></returns>
  740. public static bool Exists(string file)
  741. {
  742. return File.Exists(file);
  743. }
  744. }
  745. }