FileToolkit.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using SFB;
  6. public class FileToolkit
  7. {
  8. public static ExtensionFilter extensionFilter = new ExtensionFilter("图像/视频文件", "wmv", "mp4", "avi", "mov", "WMV", "MP4", "AVI", "MOV", "jpg", "png", "JPG", "PNG", "jpeg", "JPEG");
  9. public static List<ExtensionFilter> GetExtensionFilters()
  10. {
  11. List<ExtensionFilter> extensionFilters = new List<ExtensionFilter>();
  12. extensionFilters.Add(extensionFilter);
  13. return extensionFilters;
  14. }
  15. /// <summary>
  16. /// 读取本地数据并保存到指定位置
  17. /// </summary>
  18. /// <param name="fliePath"></param>
  19. /// <param name="targetFilePath"></param>
  20. public static void ReadLocalFileDataAndSaveToTargetPath(string fliePath, string targetFilePath)
  21. {
  22. byte[] buffer = GetLoaclFileData(fliePath);
  23. FileLocalSave(targetFilePath,buffer);
  24. }
  25. /// <summary>
  26. /// 获取本地文件数据
  27. /// </summary>
  28. /// <param name="filePath"></param>
  29. /// <returns></returns>
  30. public static byte[] GetLoaclFileData(string filePath)
  31. {
  32. using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
  33. {
  34. byte[] buffer = new byte[fs.Length];
  35. fs.Read(buffer, 0, buffer.Length);
  36. fs.Close();
  37. return buffer;
  38. }
  39. }
  40. /// <summary>
  41. /// 文件本地保存
  42. /// </summary>
  43. public static void FileLocalSave(string savePath,byte[] data)
  44. {
  45. Debug.Log(data.Length);
  46. using (FileStream fs = new FileStream(savePath,FileMode.OpenOrCreate,FileAccess.ReadWrite))
  47. {
  48. fs.Write(data, 0, data.Length);
  49. fs.Flush();
  50. }
  51. }
  52. /// <summary>
  53. /// 删除本地文件
  54. /// </summary>
  55. public static void DeleteLocalFile(string filePath)
  56. {
  57. if (File.Exists(filePath)) File.Delete(filePath);
  58. }
  59. /// <summary>
  60. /// 本地是否包含文件
  61. /// </summary>
  62. public static bool LoaclIsContainFile(string filePath)
  63. {
  64. return File.Exists(filePath);
  65. }
  66. /// <summary>
  67. /// 通过WinForm获取上传文件路径
  68. /// </summary>
  69. /// <param name="extensions"></param>
  70. /// <returns></returns>
  71. public static string[] GetAssetPathsByWinForm(List<ExtensionFilter> extensions)
  72. {
  73. string[] paths = StandaloneFileBrowser.OpenFilePanel(" 上传文件", "", extensions.ToArray(), true);
  74. return paths;
  75. }
  76. }