FilePackageTool.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.6.10 - 2017.6.10
  5. // desc: 文件打包工具
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Resources;
  14. using System.Runtime.Serialization.Formatters.Binary;
  15. using System.Text;
  16. namespace Y.Utils.IOUtils.FileUtils
  17. {
  18. /// <summary>
  19. /// 文件打包工具
  20. /// </summary>
  21. public class FilePackageTool
  22. {
  23. private static string FileTypeDesc = "FilePackage [文件打包]";
  24. private static int FileBuffer = 1024 * 1024;
  25. #region 类型单一,文件处理复杂,加载占用超大内存(这都是辣鸡)
  26. /// <summary>
  27. /// 批量打包任意对象到资源文件
  28. /// </summary>
  29. /// <param name="objCollection">被打包对象的列表。键值对中键为其在资源文件中的唯一标示名。</param>
  30. /// <param name="targetFilePath">目标资源文件。默认参数为当前目录下的"MyRes.pck"文件。</param>
  31. /// <param name="overwrite">是否覆盖已存在的目标文件。默认=True</param>
  32. public static void ResourcePackage(IDictionary<string, object> objCollection, string targetFilePath, bool overwrite = true)
  33. {
  34. if (overwrite) File.Delete(targetFilePath);
  35. using (ResourceWriter rw = new ResourceWriter(targetFilePath))
  36. {
  37. foreach (KeyValuePair<string, object> pair in objCollection)
  38. //为了防传进来的资源名有数字开头,资源名都加了前缀_
  39. rw.AddResource("_" + pair.Key, pair.Value);
  40. rw.Generate();
  41. rw.Close();
  42. }
  43. }
  44. /// <summary>
  45. /// 解包资源文件,返回所有资源及其资源名
  46. /// </summary>
  47. /// <param name="targetFilePath">要解包的资源文件。默认为当前目录下的"MyRes.pck"</param>
  48. /// <returns>资源字典,键值为资源唯一标示名。若无资源返回空集合。</returns>
  49. public static Dictionary<string, object> ResourceUnpack(string targetFilePath)
  50. {
  51. Dictionary<string, object> rtn = new Dictionary<string, object>();
  52. using (ResourceReader rr = new ResourceReader(targetFilePath))
  53. {
  54. foreach (DictionaryEntry entry in rr)
  55. rtn.Add(((string)entry.Key).Substring(1), entry.Value);
  56. }
  57. return rtn;
  58. }
  59. /// <summary>
  60. /// 根据资源名在指定的资源文件中检索资源
  61. /// </summary>
  62. /// <param name="resName">资源名</param>
  63. /// <param name="targetFilePath">要在其中检索的资源文件名,默认为"MyRes.pck"</param>
  64. /// <returns>资源名对应的资源</returns>
  65. public static object ResourceSearch(string resName, string targetFilePath)
  66. {
  67. object rtn = null;
  68. using (ResourceReader rr = new ResourceReader(targetFilePath))
  69. {
  70. foreach (DictionaryEntry entry in rr)
  71. if ((string)entry.Key == '_' + resName)
  72. {
  73. rtn = entry.Value;
  74. break;
  75. }
  76. }
  77. return rtn;
  78. }
  79. /// <summary>
  80. /// 将对象序列化
  81. /// </summary>
  82. /// <param name="FilePath">文件(支持绝大多数数据类型)</param>
  83. /// <param name="obj">要序列化的对象(如哈希表,数组等等)</param>
  84. public static void FileSerialize(string FilePath, object obj)
  85. {
  86. if (File.Exists(FilePath))
  87. {
  88. try
  89. {
  90. FileStream fs = new FileStream(FilePath, FileMode.Create);
  91. BinaryFormatter sl = new BinaryFormatter();
  92. sl.Serialize(fs, obj);
  93. fs.Close();
  94. }
  95. catch
  96. {
  97. //序列化存储失败!
  98. }
  99. }
  100. else
  101. {
  102. //您读取的文件对象不存在
  103. }
  104. }
  105. /// <summary>
  106. /// 将文件反序列化
  107. /// </summary>
  108. /// <param name="FilePath">文件路径(必须是经过当前序列化后的文件)</param>
  109. /// <returns>返回 null 表示序列反解失败或者目标文件不存在</returns>
  110. public static object FileDeSerialize(string FilePath)
  111. {
  112. if (System.IO.File.Exists(FilePath))
  113. {
  114. try
  115. {
  116. FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  117. BinaryFormatter sl = new BinaryFormatter();
  118. object obg = sl.Deserialize(fs);
  119. fs.Close();
  120. return obg;
  121. }
  122. catch
  123. {
  124. return null;
  125. }
  126. }
  127. else
  128. {
  129. return null;
  130. }
  131. }
  132. #endregion
  133. /// <summary>
  134. /// 打包
  135. /// </summary>
  136. /// <returns></returns>
  137. public static int Pack(string srcPath, string dstFile, bool overwrite = true)
  138. {
  139. DateTime beginTime = DateTime.Now;
  140. return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功
  141. }
  142. /// <summary>
  143. /// 解包
  144. /// </summary>
  145. /// <returns></returns>
  146. public static int UnPack(string srcFile, string dstPath, bool overwrite = true)
  147. {
  148. DateTime beginTime = DateTime.Now;
  149. return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功
  150. }
  151. }
  152. }