You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
8.3 KiB

3 weeks ago
  1. using ICSharpCode.SharpZipLib.Zip;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. namespace UpdateServiceLibrary.Helper
  7. {
  8. public class ZipHelper
  9. {
  10. /// <summary>
  11. /// 缓冲区大小
  12. /// </summary>
  13. public static int ByteSize { get; set; } = 4096;
  14. /// <summary>
  15. /// 压缩等级 0-9 越大压缩率越高
  16. /// </summary>
  17. public static int ZipLevel { get; set; } = 6;
  18. /// <summary>
  19. /// 解压压缩包到指定文件目录
  20. /// </summary>
  21. /// <param name="zipFile"></param>
  22. /// <param name="unzipfolder"></param>
  23. /// <param name="password"></param>
  24. /// <returns></returns>
  25. public static ResultModel Unzip(FileInfo zipFile, DirectoryInfo unzipfolder, string password = null)
  26. {
  27. ResultModel resultModel = new ResultModel { IsSuccess = true };
  28. try
  29. {
  30. //待解压文件是否存在
  31. if (!zipFile.Exists)
  32. throw new Exception("待解压文件不存在");
  33. //创建解压目录
  34. if (!unzipfolder.Exists)
  35. unzipfolder.Create();
  36. ZipEntry ent;
  37. string fileName;
  38. //打开文件流
  39. using (var fs = zipFile.OpenRead())
  40. {
  41. //创建解压文件流
  42. var zipStream = new ZipInputStream(fs);
  43. if (password != null)
  44. zipStream.Password = password;
  45. //解压文件
  46. while ((ent = zipStream.GetNextEntry()) != null)
  47. {
  48. if (string.IsNullOrEmpty(ent.Name))
  49. continue;
  50. fileName = Path.Combine(unzipfolder.FullName, ent.Name);
  51. //判断是否为文件夹
  52. if (ent.IsDirectory)
  53. {
  54. if (!Directory.Exists(fileName))
  55. Directory.CreateDirectory(fileName);
  56. continue;
  57. }
  58. var folder = Path.GetDirectoryName(fileName);
  59. if (!Directory.Exists(folder))
  60. Directory.CreateDirectory(folder);
  61. //创建解压文件
  62. int byteSize = ByteSize;
  63. byte[] buff = new byte[byteSize];
  64. using (var fw = File.Create(fileName))
  65. {
  66. while ((byteSize = zipStream.Read(buff, 0, buff.Length)) > 0)
  67. {
  68. fw.Write(buff, 0, byteSize);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. catch(Exception ex)
  75. {
  76. resultModel.IsSuccess = false;
  77. resultModel.Message = ex.Message;
  78. }
  79. return resultModel;
  80. }
  81. /// <summary>
  82. /// 压缩文件夹到指定文件
  83. /// </summary>
  84. /// <param name="folder"></param>
  85. /// <param name="fileInfo"></param>
  86. /// <param name="password"></param>
  87. /// <returns></returns>
  88. public static bool ZipFile(DirectoryInfo folder, FileInfo fileInfo, bool createRootFolder = false, string password = null)
  89. {
  90. //需要压缩的文件夹不存在时退出
  91. if (!folder.Exists)
  92. return false;
  93. //创建并设定压缩流
  94. using (ZipOutputStream zipOutputStream = new ZipOutputStream(fileInfo.Create()))
  95. {
  96. zipOutputStream.SetLevel(ZipLevel);
  97. if (password != null)
  98. zipOutputStream.Password = password;
  99. //递归压缩文件夹
  100. if (!ZipFile(folder, zipOutputStream, "", !createRootFolder))
  101. return false;
  102. //保存
  103. zipOutputStream.Finish();
  104. zipOutputStream.Close();
  105. }
  106. return true;
  107. }
  108. /// <summary>
  109. /// 递归压缩文件夹
  110. /// </summary>
  111. /// <param name="folder"></param>
  112. /// <param name="zipOutputStream"></param>
  113. /// <param name="parentFolderName"></param>
  114. /// <returns></returns>
  115. private static bool ZipFile(DirectoryInfo folder, ZipOutputStream zipOutputStream, string parentFolderName, bool rootFolder = false)
  116. {
  117. try
  118. {
  119. //创建目录
  120. ZipEntry ent;
  121. if (!rootFolder)
  122. {
  123. ent = new ZipEntry(Path.Combine(parentFolderName, folder.Name + "/"));
  124. zipOutputStream.PutNextEntry(ent);
  125. zipOutputStream.Flush();
  126. }
  127. //初始化缓冲区
  128. int byteSize = ByteSize;
  129. byte[] buffer = new byte[byteSize];
  130. //创建文件
  131. foreach (var item in folder.GetFiles())
  132. {
  133. using (FileStream fs = item.OpenRead())
  134. {
  135. string entPath = string.Empty;
  136. if (rootFolder)
  137. entPath = Path.Combine(parentFolderName, item.Name);
  138. else
  139. entPath = Path.Combine(parentFolderName, folder.Name + "/" + item.Name);
  140. ent = new ZipEntry(entPath);
  141. ent.DateTime = DateTime.Now;
  142. ent.Size = item.Length;
  143. zipOutputStream.PutNextEntry(ent);
  144. while ((byteSize = fs.Read(buffer, 0, buffer.Length)) > 0)
  145. {
  146. zipOutputStream.Write(buffer, 0, byteSize);
  147. }
  148. }
  149. }
  150. //递归压缩文件夹
  151. if (!rootFolder)
  152. parentFolderName = Path.Combine(parentFolderName, folder.Name);
  153. foreach (var item in folder.GetDirectories())
  154. {
  155. if (!ZipFile(item, zipOutputStream, parentFolderName))
  156. return false;
  157. }
  158. return true;
  159. }
  160. catch (Exception ex)
  161. {
  162. return false;
  163. }
  164. }
  165. /// <summary>
  166. /// 压缩单个文件
  167. /// </summary>
  168. /// <param name="sourceFile"></param>
  169. /// <param name="targetFile"></param>
  170. /// <param name="password"></param>
  171. /// <returns></returns>
  172. public static bool ZipFile(FileInfo sourceFile, FileInfo targetFile, string password = null)
  173. {
  174. //需要压缩的文件不存在时退出
  175. if (!sourceFile.Exists)
  176. return false;
  177. try
  178. {
  179. //创建压缩流
  180. using (ZipOutputStream zipOutputStream = new ZipOutputStream(targetFile.Create()))
  181. {
  182. //设定压缩级别与密码
  183. zipOutputStream.SetLevel(ZipLevel);
  184. if (password != null)
  185. zipOutputStream.Password = password;
  186. //初始化缓冲区
  187. int byteSize = ByteSize;
  188. byte[] buffer = new byte[byteSize];
  189. //初始化压缩实体
  190. ZipEntry entry = new ZipEntry(sourceFile.Name);
  191. entry.DateTime = DateTime.Now;
  192. entry.Size = sourceFile.Length;
  193. //压缩
  194. zipOutputStream.PutNextEntry(entry);
  195. using (var fs = sourceFile.OpenRead())
  196. {
  197. while ((byteSize = fs.Read(buffer, 0, buffer.Length)) > 0)
  198. {
  199. zipOutputStream.Write(buffer, 0, byteSize);
  200. }
  201. }
  202. //保存关闭
  203. zipOutputStream.Finish();
  204. zipOutputStream.Close();
  205. }
  206. return true;
  207. }
  208. catch (Exception ex)
  209. {
  210. return false;
  211. }
  212. }
  213. }
  214. }