ZipCompessHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.IO.Compression;
  6. using System.Linq;
  7. using System.Runtime.Serialization;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. //----------------------------------------------------------------
  12. //Module Name: $safeprojectname$
  13. //Purpose:
  14. //CopyRight: Muchinfo
  15. //History:
  16. //----------------------------------------------------------------
  17. //DateTime 2015/12/29 16:39:22
  18. //Author
  19. //Description Create
  20. //----------------------------------------------------------------
  21. using System.Xml.Serialization;
  22. namespace Muchinfo.Client.Update.UpdateListFactory
  23. {
  24. /// <summary>
  25. /// Zip压缩
  26. /// </summary>
  27. public class ZipCompessHelper
  28. {
  29. /// 对目标文件夹进行压缩,将压缩结果保存为指定文件
  30. /// </summary>
  31. /// <param name="dirPath">目标文件夹</param>
  32. /// <param name="fileName">压缩文件</param>
  33. public static void Compress(string dirPath, string fileName)
  34. {
  35. ArrayList list = new ArrayList();
  36. foreach (string f in Directory.GetFiles(dirPath))
  37. {
  38. byte[] destBuffer = File.ReadAllBytes(f);
  39. SerializeFileInfo sfi = new SerializeFileInfo(f, destBuffer);
  40. list.Add(sfi);
  41. }
  42. IFormatter formatter = new BinaryFormatter();
  43. using (Stream s = new MemoryStream())
  44. {
  45. formatter.Serialize(s, list);
  46. s.Position = 0;
  47. CreateCompressFile(s, fileName);
  48. }
  49. }
  50. /// <summary>
  51. /// 对目标压缩文件解压缩,将内容解压缩到指定文件夹
  52. /// </summary>
  53. /// <param name="fileName">压缩文件</param>
  54. /// <param name="dirPath">解压缩目录</param>
  55. public static void DeCompress(string fileName, string dirPath)
  56. {
  57. using (Stream source = File.OpenRead(fileName))
  58. {
  59. using (Stream destination = new MemoryStream())
  60. {
  61. using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true))
  62. {
  63. byte[] bytes = new byte[4096];
  64. int n;
  65. while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
  66. {
  67. destination.Write(bytes, 0, n);
  68. }
  69. }
  70. destination.Flush();
  71. destination.Position = 0;
  72. DeSerializeFiles(destination, dirPath);
  73. }
  74. }
  75. }
  76. private static void DeSerializeFiles(Stream s, string dirPath)
  77. {
  78. BinaryFormatter b = new BinaryFormatter();
  79. ArrayList list = (ArrayList)b.Deserialize(s);
  80. foreach (SerializeFileInfo f in list)
  81. {
  82. string newName = dirPath + Path.GetFileName(f.FileName);
  83. using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write))
  84. {
  85. fs.Write(f.FileBuffer, 0, f.FileBuffer.Length);
  86. fs.Close();
  87. }
  88. }
  89. }
  90. public static void CreateCompressFile(Stream source, string destinationName)
  91. {
  92. using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write))
  93. {
  94. using (GZipStream output = new GZipStream(destination, CompressionMode.Compress))
  95. {
  96. byte[] bytes = new byte[4096];
  97. int n;
  98. while ((n = source.Read(bytes, 0, bytes.Length)) != 0)
  99. {
  100. output.Write(bytes, 0, n);
  101. }
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// 生成更新文件的XML
  107. /// </summary>
  108. /// <param name="updateInfo">更新配置</param>
  109. /// <param name="filePath">保存路径</param>
  110. public static void SaveUpdateXml(UpdateInfo updateInfo,string filePath)
  111. {
  112. var xs = new XmlSerializer(updateInfo.GetType());
  113. using (var stream = new FileStream(filePath,FileMode.Create))
  114. {
  115. xs.Serialize(stream, updateInfo);
  116. stream.Close();
  117. }
  118. }
  119. }
  120. [Serializable]
  121. public class SerializeFileInfo
  122. {
  123. public SerializeFileInfo(string name, byte[] buffer)
  124. {
  125. fileName = name;
  126. fileBuffer = buffer;
  127. }
  128. string fileName;
  129. public string FileName
  130. {
  131. get
  132. {
  133. return fileName;
  134. }
  135. }
  136. byte[] fileBuffer;
  137. public byte[] FileBuffer
  138. {
  139. get
  140. {
  141. return fileBuffer;
  142. }
  143. }
  144. }
  145. }