FilePackageTool.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.6.10 - 2017.6.15
  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. using Y.Utils.DataUtils.Collections;
  17. using Y.Utils.IOUtils.PathUtils;
  18. namespace Y.Utils.IOUtils.FileUtils
  19. {
  20. /// <summary>
  21. /// 文件打包工具
  22. /// </summary>
  23. public class FilePackageTool
  24. {
  25. const string FileType = "Y.Utils.FilePackage";//文件类型 禁止修改长度(19位)
  26. const string FileVersion = "100001";//类型的版本 禁止修改长度(6位)
  27. private static int FileBuffer = 1024 * 1024;
  28. #region 类型单一,文件处理复杂,加载占用超大内存(这都是辣鸡)
  29. /// <summary>
  30. /// 批量打包任意对象到资源文件
  31. /// </summary>
  32. /// <param name="objCollection">被打包对象的列表。键值对中键为其在资源文件中的唯一标示名。</param>
  33. /// <param name="targetFilePath">目标资源文件。默认参数为当前目录下的"MyRes.pck"文件。</param>
  34. /// <param name="overwrite">是否覆盖已存在的目标文件。默认=True</param>
  35. public static void ResourcePackage(IDictionary<string, object> objCollection, string targetFilePath, bool overwrite = true)
  36. {
  37. if (overwrite) File.Delete(targetFilePath);
  38. using (ResourceWriter rw = new ResourceWriter(targetFilePath))
  39. {
  40. foreach (KeyValuePair<string, object> pair in objCollection)
  41. //为了防传进来的资源名有数字开头,资源名都加了前缀_
  42. rw.AddResource("_" + pair.Key, pair.Value);
  43. rw.Generate();
  44. rw.Close();
  45. }
  46. }
  47. /// <summary>
  48. /// 解包资源文件,返回所有资源及其资源名
  49. /// </summary>
  50. /// <param name="targetFilePath">要解包的资源文件。默认为当前目录下的"MyRes.pck"</param>
  51. /// <returns>资源字典,键值为资源唯一标示名。若无资源返回空集合。</returns>
  52. public static Dictionary<string, object> ResourceUnpack(string targetFilePath)
  53. {
  54. Dictionary<string, object> rtn = new Dictionary<string, object>();
  55. using (ResourceReader rr = new ResourceReader(targetFilePath))
  56. {
  57. foreach (DictionaryEntry entry in rr)
  58. rtn.Add(((string)entry.Key).Substring(1), entry.Value);
  59. }
  60. return rtn;
  61. }
  62. /// <summary>
  63. /// 根据资源名在指定的资源文件中检索资源
  64. /// </summary>
  65. /// <param name="resName">资源名</param>
  66. /// <param name="targetFilePath">要在其中检索的资源文件名,默认为"MyRes.pck"</param>
  67. /// <returns>资源名对应的资源</returns>
  68. public static object ResourceSearch(string resName, string targetFilePath)
  69. {
  70. object rtn = null;
  71. using (ResourceReader rr = new ResourceReader(targetFilePath))
  72. {
  73. foreach (DictionaryEntry entry in rr)
  74. if ((string)entry.Key == '_' + resName)
  75. {
  76. rtn = entry.Value;
  77. break;
  78. }
  79. }
  80. return rtn;
  81. }
  82. /// <summary>
  83. /// 将对象序列化
  84. /// </summary>
  85. /// <param name="FilePath">文件(支持绝大多数数据类型)</param>
  86. /// <param name="obj">要序列化的对象(如哈希表,数组等等)</param>
  87. public static void FileSerialize(string FilePath, object obj)
  88. {
  89. if (File.Exists(FilePath))
  90. {
  91. try
  92. {
  93. FileStream fs = new FileStream(FilePath, FileMode.Create);
  94. BinaryFormatter sl = new BinaryFormatter();
  95. sl.Serialize(fs, obj);
  96. fs.Close();
  97. }
  98. catch
  99. {
  100. //序列化存储失败!
  101. }
  102. }
  103. else
  104. {
  105. //您读取的文件对象不存在
  106. }
  107. }
  108. /// <summary>
  109. /// 将文件反序列化
  110. /// </summary>
  111. /// <param name="FilePath">文件路径(必须是经过当前序列化后的文件)</param>
  112. /// <returns>返回 null 表示序列反解失败或者目标文件不存在</returns>
  113. public static object FileDeSerialize(string FilePath)
  114. {
  115. if (System.IO.File.Exists(FilePath))
  116. {
  117. try
  118. {
  119. FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  120. BinaryFormatter sl = new BinaryFormatter();
  121. object obg = sl.Deserialize(fs);
  122. fs.Close();
  123. return obg;
  124. }
  125. catch
  126. {
  127. return null;
  128. }
  129. }
  130. else
  131. {
  132. return null;
  133. }
  134. }
  135. #endregion
  136. /// <summary>
  137. /// 打包
  138. /// </summary>
  139. /// <returns>
  140. /// -11;//要打包的路径不存在
  141. /// -12;//打包后的目标文件已存在
  142. /// -13;//要打包的路径中没有文件
  143. /// -404;//未知错误,操作失败
  144. /// </returns>
  145. public static int Pack(string srcPath, string dstFile, bool overwrite = true)
  146. {
  147. DateTime beginTime = DateTime.Now;
  148. if (!Directory.Exists(srcPath)) return -11;//要打包的路径不存在
  149. if (File.Exists(dstFile) && !overwrite) return -12;//打包后的目标文件已存在
  150. List<string> tempfiles = FileTool.GetAllFile(srcPath);
  151. List<FilePackageModel> files = new List<FilePackageModel>();
  152. if (ListTool.HasElements(tempfiles))
  153. {
  154. //汇总所有文件
  155. tempfiles.ForEach(x =>
  156. {
  157. files.Add(new FilePackageModel()
  158. {
  159. Name = Path.GetFileName(x),
  160. Path = DirTool.GetFilePath(x).Substring(srcPath.Count()),
  161. Size = FileTool.Size(x),
  162. MD5 = FileTool.GetMD5(x),
  163. });
  164. });
  165. long allfilesize = files.Sum(x => x.Size);
  166. using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
  167. {
  168. //写入文件类型标识和版本号
  169. byte[] filetypeandversion = Encoding.Default.GetBytes(FileType + FileVersion);
  170. fsWrite.Write(filetypeandversion, 0, filetypeandversion.Length);
  171. //写入头部总长度
  172. int headl = files.Sum(x => x.AllByteLength);
  173. byte[] headlength = BitConverter.GetBytes(headl);
  174. fsWrite.Write(headlength, 0, headlength.Length);
  175. //循环写入文件信息
  176. files.ForEach(x =>
  177. {
  178. fsWrite.Write(x.NameLengthByte, 0, x.NameLengthByte.Length);
  179. fsWrite.Write(x.NameByte, 0, x.NameByte.Length);
  180. fsWrite.Write(x.PathLengthByte, 0, x.PathLengthByte.Length);
  181. fsWrite.Write(x.PathByte, 0, x.PathByte.Length);
  182. fsWrite.Write(x.SizeLengthByte, 0, x.SizeLengthByte.Length);
  183. fsWrite.Write(x.SizeByte, 0, x.SizeByte.Length);
  184. fsWrite.Write(x.MD5LengthByte, 0, x.MD5LengthByte.Length);
  185. fsWrite.Write(x.MD5Byte, 0, x.MD5Byte.Length);
  186. });
  187. //循环写入文件
  188. files.ForEach(x =>
  189. {
  190. using (FileStream fsRead = new FileStream(DirTool.Combine(srcPath, x.Path, x.Name), FileMode.Open))
  191. {
  192. int readCount = 0;
  193. byte[] buffer = new byte[FileBuffer];
  194. while ((readCount = fsRead.Read(buffer, 0, buffer.Length)) > 0)
  195. {
  196. fsWrite.Write(buffer, 0, readCount);
  197. allfilesize -= readCount;
  198. }
  199. fsRead.Close();
  200. }
  201. });
  202. fsWrite.Close();
  203. }
  204. if (allfilesize == 0)
  205. {
  206. return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功
  207. }
  208. }
  209. else
  210. {
  211. return -13;//要打包的路径中没有文件
  212. }
  213. //打包失败后,删除打包后的文件
  214. try { File.Delete(dstFile); } catch (Exception e) { }
  215. return -404;//未知错误,操作失败
  216. }
  217. /// <summary>
  218. /// 解包
  219. /// </summary>
  220. /// <returns></returns>
  221. public static int Unpack(string srcFile, string dstPath, bool overwrite = true)
  222. {
  223. DateTime beginTime = DateTime.Now;
  224. if (!File.Exists(srcFile)) return -11; //要解包的文件不存在
  225. if (Directory.Exists(dstPath) && !overwrite) return -12;//要解包的目标文件夹已存在
  226. using (FileStream fsRead = new FileStream(srcFile, FileMode.Open))
  227. {
  228. string version = GetFileVersion(fsRead);
  229. if (version == null) return -20;// 文件类型不匹配
  230. //读取头部总长度
  231. byte[] headl = new byte[4];
  232. int headlength = 0;
  233. fsRead.Read(headl, 0, headl.Length);
  234. headlength = BitConverter.ToInt32(headl, 0);
  235. if (headlength > 0)
  236. {
  237. //读取文件列表信息
  238. byte[] headdata = new byte[headlength];
  239. fsRead.Read(headdata, 0, headlength);
  240. List<FilePackageModel> files = GetFilePackageModel(headdata);
  241. if (ListTool.HasElements(files))
  242. {
  243. files.ForEach(x =>
  244. {
  245. if (DirTool.Create(DirTool.Combine(dstPath, x.Path)))
  246. {
  247. using (FileStream fsWrite = new FileStream(DirTool.Combine(dstPath, x.Path, x.Name), FileMode.Create))
  248. {
  249. long size = x.Size;
  250. int readCount = 0;
  251. byte[] buffer = new byte[FileBuffer];
  252. while (size > FileBuffer)
  253. {
  254. readCount = fsRead.Read(buffer, 0, buffer.Length);
  255. fsWrite.Write(buffer, 0, readCount);
  256. size -= readCount;
  257. }
  258. if (size <= FileBuffer)
  259. {
  260. readCount = fsRead.Read(buffer, 0, (int)size);
  261. fsWrite.Write(buffer, 0, readCount);
  262. }
  263. fsWrite.Close();
  264. }
  265. }
  266. });
  267. }
  268. int a = 111;
  269. }
  270. fsRead.Close();
  271. }
  272. return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功
  273. }
  274. /// <summary>
  275. /// 获取文件类型的类型版本
  276. /// </summary>
  277. /// <param name="fs"></param>
  278. /// <returns>
  279. /// 如果文件类型不匹配,则返回null
  280. /// </returns>
  281. private static string GetFileVersion(FileStream fs)
  282. {
  283. string result = null;
  284. //读取文件类型标识和版本号
  285. byte[] filetype = Encoding.Default.GetBytes(FileType);
  286. fs.Read(filetype, 0, filetype.Length);
  287. string filetypestr = Encoding.Default.GetString(filetype);
  288. byte[] fileversion = Encoding.Default.GetBytes(FileVersion);
  289. fs.Read(fileversion, 0, fileversion.Length);
  290. string fileversionstr = Encoding.Default.GetString(fileversion);
  291. //如果文件类型匹配,则返回版本号
  292. if (filetypestr == FileType) result = fileversionstr;
  293. return result;
  294. }
  295. private static List<FilePackageModel> GetFilePackageModel(byte[] headdata)
  296. {
  297. List<FilePackageModel> files = new List<FilePackageModel>();
  298. int index = 0;
  299. try
  300. {
  301. while (index < headdata.Length)
  302. {
  303. #region 读取文件名长度和内容
  304. //文件名长度
  305. byte[] namelengthbyte = new byte[4];
  306. Buffer.BlockCopy(headdata, index, namelengthbyte, 0, namelengthbyte.Length);
  307. int namelength = BitConverter.ToInt32(namelengthbyte, 0);
  308. index += namelengthbyte.Length;
  309. //文件名内容
  310. byte[] namebyte = new byte[namelength];
  311. Buffer.BlockCopy(headdata, index, namebyte, 0, namelength);
  312. string name = Encoding.Default.GetString(namebyte);
  313. index += namebyte.Length;
  314. #endregion
  315. #region 读取路径长度和内容
  316. //路径长度
  317. byte[] pathlengthbyte = new byte[4];
  318. Buffer.BlockCopy(headdata, index, pathlengthbyte, 0, pathlengthbyte.Length);
  319. int pathlength = BitConverter.ToInt32(pathlengthbyte, 0);
  320. index += pathlengthbyte.Length;
  321. //路径内容
  322. byte[] pathbyte = new byte[pathlength];
  323. Buffer.BlockCopy(headdata, index, pathbyte, 0, pathlength);
  324. string path = Encoding.Default.GetString(pathbyte);
  325. index += pathbyte.Length;
  326. #endregion
  327. #region 读取文件大小长度和内容
  328. //文件大小长度
  329. byte[] sizelengthbyte = new byte[4];
  330. Buffer.BlockCopy(headdata, index, sizelengthbyte, 0, sizelengthbyte.Length);
  331. int sizelength = BitConverter.ToInt32(sizelengthbyte, 0);
  332. index += sizelengthbyte.Length;
  333. //文件大小
  334. byte[] sizebyte = new byte[sizelength];
  335. Buffer.BlockCopy(headdata, index, sizebyte, 0, sizelength);
  336. long size = BitConverter.ToInt64(sizebyte, 0);
  337. index += sizebyte.Length;
  338. #endregion
  339. #region 读取文件MD5码长度和内容
  340. //文件大小长度
  341. byte[] md5lengthbyte = new byte[4];
  342. Buffer.BlockCopy(headdata, index, md5lengthbyte, 0, md5lengthbyte.Length);
  343. int md5length = BitConverter.ToInt32(md5lengthbyte, 0);
  344. index += md5lengthbyte.Length;
  345. //文件大小
  346. byte[] md5byte = new byte[md5length];
  347. Buffer.BlockCopy(headdata, index, md5byte, 0, md5length);
  348. string md5 = Encoding.Default.GetString(md5byte);
  349. index += md5byte.Length;
  350. #endregion
  351. files.Add(new FilePackageModel()
  352. {
  353. Name = name,
  354. Path = path,
  355. Size = size,
  356. MD5 = md5,
  357. });
  358. }
  359. return files;
  360. }
  361. catch (Exception e) { return null; }
  362. }
  363. }
  364. }