SevenZipCompressor.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using SharpCompress.Archives;
  2. using SharpCompress.Archives.Rar;
  3. using SharpCompress.Archives.Zip;
  4. using SharpCompress.Common;
  5. using SharpCompress.Readers;
  6. using SharpCompress.Writers;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net.Http;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Web;
  16. namespace Masuit.Tools.Files
  17. {
  18. /// <summary>
  19. /// 7z压缩
  20. /// </summary>
  21. public class SevenZipCompressor : ISevenZipCompressor
  22. {
  23. private readonly HttpClient _httpClient;
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <param name="httpClientFactory"></param>
  28. public SevenZipCompressor(IHttpClientFactory httpClientFactory)
  29. {
  30. _httpClient = httpClientFactory.CreateClient();
  31. }
  32. /// <summary>
  33. /// 将多个文件压缩到一个文件流中,可保存为zip文件,方便于web方式下载
  34. /// </summary>
  35. /// <param name="files">多个文件路径,文件或文件夹,或网络路径http/https</param>
  36. /// <param name="rootdir"></param>
  37. /// <returns>文件流</returns>
  38. public MemoryStream ZipStream(List<string> files, string rootdir = "")
  39. {
  40. using (var archive = CreateZipArchive(files, rootdir))
  41. {
  42. var ms = new MemoryStream();
  43. archive.SaveTo(ms, new WriterOptions(CompressionType.Deflate)
  44. {
  45. LeaveStreamOpen = true,
  46. ArchiveEncoding = new ArchiveEncoding()
  47. {
  48. Default = Encoding.UTF8
  49. }
  50. });
  51. return ms;
  52. }
  53. }
  54. /// <summary>
  55. /// 压缩多个文件
  56. /// </summary>
  57. /// <param name="files">多个文件路径,文件或文件夹</param>
  58. /// <param name="zipFile">压缩到...</param>
  59. /// <param name="rootdir">压缩包内部根文件夹</param>
  60. public void Zip(List<string> files, string zipFile, string rootdir = "")
  61. {
  62. using (var archive = CreateZipArchive(files, rootdir))
  63. {
  64. archive.SaveTo(zipFile, new WriterOptions(CompressionType.Deflate)
  65. {
  66. LeaveStreamOpen = true,
  67. ArchiveEncoding = new ArchiveEncoding()
  68. {
  69. Default = Encoding.UTF8
  70. }
  71. });
  72. }
  73. }
  74. /// <summary>
  75. /// 解压rar文件
  76. /// </summary>
  77. /// <param name="rar">rar文件</param>
  78. /// <param name="dir">解压到...</param>
  79. /// <param name="ignoreEmptyDir">忽略空文件夹</param>
  80. public void UnRar(string rar, string dir = "", bool ignoreEmptyDir = true)
  81. {
  82. if (string.IsNullOrEmpty(dir))
  83. {
  84. dir = Path.GetDirectoryName(rar);
  85. }
  86. using (var archive = RarArchive.Open(rar))
  87. {
  88. var entries = ignoreEmptyDir ? archive.Entries.Where(entry => !entry.IsDirectory) : archive.Entries;
  89. foreach (var entry in entries)
  90. {
  91. entry.WriteToDirectory(dir, new ExtractionOptions()
  92. {
  93. ExtractFullPath = true,
  94. Overwrite = true
  95. });
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// 解压文件,自动检测压缩包类型
  101. /// </summary>
  102. /// <param name="compressedFile">rar文件</param>
  103. /// <param name="dir">解压到...</param>
  104. /// <param name="ignoreEmptyDir">忽略空文件夹</param>
  105. public void Extract(string compressedFile, string dir = "", bool ignoreEmptyDir = true) => Decompress(compressedFile, dir, ignoreEmptyDir);
  106. /// <summary>
  107. /// 解压文件,自动检测压缩包类型
  108. /// </summary>
  109. /// <param name="compressedFile">rar文件</param>
  110. /// <param name="dir">解压到...</param>
  111. /// <param name="ignoreEmptyDir">忽略空文件夹</param>
  112. public void Decompress(string compressedFile, string dir = "", bool ignoreEmptyDir = true)
  113. {
  114. if (string.IsNullOrEmpty(dir))
  115. {
  116. dir = Path.GetDirectoryName(compressedFile);
  117. }
  118. using (Stream stream = File.OpenRead(compressedFile))
  119. {
  120. using (var reader = ReaderFactory.Open(stream))
  121. {
  122. while (reader.MoveToNextEntry())
  123. {
  124. if (ignoreEmptyDir)
  125. {
  126. reader.WriteEntryToDirectory(dir, new ExtractionOptions()
  127. {
  128. ExtractFullPath = true,
  129. Overwrite = true
  130. });
  131. }
  132. else
  133. {
  134. if (!reader.Entry.IsDirectory)
  135. {
  136. reader.WriteEntryToDirectory(dir, new ExtractionOptions()
  137. {
  138. ExtractFullPath = true,
  139. Overwrite = true
  140. });
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// 创建zip包
  149. /// </summary>
  150. /// <param name="files"></param>
  151. /// <param name="rootdir"></param>
  152. /// <returns></returns>
  153. private ZipArchive CreateZipArchive(List<string> files, string rootdir)
  154. {
  155. var archive = ZipArchive.Create();
  156. var dic = GetFileEntryMaps(files);
  157. var remoteUrls = files.Distinct().Where(s => s.StartsWith("http")).Select(s =>
  158. {
  159. try
  160. {
  161. return new Uri(s);
  162. }
  163. catch (UriFormatException)
  164. {
  165. return null;
  166. }
  167. }).Where(u => u != null).ToList();
  168. foreach (var fileEntry in dic)
  169. {
  170. archive.AddEntry(Path.Combine(rootdir, fileEntry.Value), fileEntry.Key);
  171. }
  172. if (remoteUrls.Any())
  173. {
  174. var streams = new ConcurrentDictionary<string, Stream>();
  175. Parallel.ForEach(remoteUrls, url =>
  176. {
  177. _httpClient.GetAsync(url).ContinueWith(async t =>
  178. {
  179. if (t.IsCompleted)
  180. {
  181. var res = await t;
  182. if (res.IsSuccessStatusCode)
  183. {
  184. Stream stream = await res.Content.ReadAsStreamAsync();
  185. streams[Path.Combine(rootdir, Path.GetFileName(HttpUtility.UrlDecode(url.AbsolutePath)))] = stream;
  186. }
  187. }
  188. }).Wait();
  189. });
  190. foreach (var kv in streams)
  191. {
  192. archive.AddEntry(kv.Key, kv.Value);
  193. }
  194. }
  195. return archive;
  196. }
  197. /// <summary>
  198. /// 获取文件路径和zip-entry的映射
  199. /// </summary>
  200. /// <param name="files"></param>
  201. /// <returns></returns>
  202. private Dictionary<string, string> GetFileEntryMaps(List<string> files)
  203. {
  204. List<string> fileList = new List<string>();
  205. void GetFilesRecurs(string path)
  206. {
  207. //遍历目标文件夹的所有文件
  208. foreach (string fileName in Directory.GetFiles(path))
  209. {
  210. fileList.Add(fileName);
  211. }
  212. //遍历目标文件夹的所有文件夹
  213. foreach (string directory in Directory.GetDirectories(path))
  214. {
  215. GetFilesRecurs(directory);
  216. }
  217. }
  218. files.Where(s => !s.StartsWith("http")).ForEach(s =>
  219. {
  220. if (Directory.Exists(s))
  221. {
  222. GetFilesRecurs(s);
  223. }
  224. else
  225. {
  226. fileList.Add(s);
  227. }
  228. });
  229. if (!fileList.Any())
  230. {
  231. return new Dictionary<string, string>();
  232. }
  233. string dirname = new string(fileList.First().Substring(0, fileList.Min(s => s.Length)).TakeWhile((c, i) => fileList.All(s => s[i] == c)).ToArray());
  234. Dictionary<string, string> dic = fileList.ToDictionary(s => s, s => s.Substring(dirname.Length));
  235. return dic;
  236. }
  237. }
  238. }