SevenZipCompressor.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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="httpClient"></param>
  28. public SevenZipCompressor(HttpClient httpClient)
  29. {
  30. _httpClient = httpClient;
  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. var ms = new MemoryStream();
  42. archive.SaveTo(ms, new WriterOptions(CompressionType.Deflate)
  43. {
  44. LeaveStreamOpen = true,
  45. ArchiveEncoding = new ArchiveEncoding()
  46. {
  47. Default = Encoding.UTF8
  48. }
  49. });
  50. return ms;
  51. }
  52. /// <summary>
  53. /// 压缩多个文件
  54. /// </summary>
  55. /// <param name="files">多个文件路径,文件或文件夹</param>
  56. /// <param name="zipFile">压缩到...</param>
  57. /// <param name="rootdir">压缩包内部根文件夹</param>
  58. public void Zip(List<string> files, string zipFile, string rootdir = "")
  59. {
  60. using var archive = CreateZipArchive(files, rootdir);
  61. archive.SaveTo(zipFile, new WriterOptions(CompressionType.Deflate)
  62. {
  63. LeaveStreamOpen = true,
  64. ArchiveEncoding = new ArchiveEncoding()
  65. {
  66. Default = Encoding.UTF8
  67. }
  68. });
  69. }
  70. /// <summary>
  71. /// 解压rar文件
  72. /// </summary>
  73. /// <param name="rar">rar文件</param>
  74. /// <param name="dir">解压到...</param>
  75. /// <param name="ignoreEmptyDir">忽略空文件夹</param>
  76. public void UnRar(string rar, string dir = "", bool ignoreEmptyDir = true)
  77. {
  78. if (string.IsNullOrEmpty(dir))
  79. {
  80. dir = Path.GetDirectoryName(rar);
  81. }
  82. using var archive = RarArchive.Open(rar);
  83. var entries = ignoreEmptyDir ? archive.Entries.Where(entry => !entry.IsDirectory) : archive.Entries;
  84. foreach (var entry in entries)
  85. {
  86. entry.WriteToDirectory(dir, new ExtractionOptions()
  87. {
  88. ExtractFullPath = true,
  89. Overwrite = true
  90. });
  91. }
  92. }
  93. /// <summary>
  94. /// 解压文件,自动检测压缩包类型
  95. /// </summary>
  96. /// <param name="compressedFile">rar文件</param>
  97. /// <param name="dir">解压到...</param>
  98. /// <param name="ignoreEmptyDir">忽略空文件夹</param>
  99. public void Extract(string compressedFile, string dir = "", bool ignoreEmptyDir = true) => Decompress(compressedFile, dir, ignoreEmptyDir);
  100. /// <summary>
  101. /// 解压文件,自动检测压缩包类型
  102. /// </summary>
  103. /// <param name="compressedFile">rar文件</param>
  104. /// <param name="dir">解压到...</param>
  105. /// <param name="ignoreEmptyDir">忽略空文件夹</param>
  106. public void Decompress(string compressedFile, string dir = "", bool ignoreEmptyDir = true)
  107. {
  108. if (string.IsNullOrEmpty(dir))
  109. {
  110. dir = Path.GetDirectoryName(compressedFile);
  111. }
  112. using Stream stream = File.OpenRead(compressedFile);
  113. using var reader = ReaderFactory.Open(stream);
  114. while (reader.MoveToNextEntry())
  115. {
  116. if (ignoreEmptyDir)
  117. {
  118. reader.WriteEntryToDirectory(dir, new ExtractionOptions()
  119. {
  120. ExtractFullPath = true,
  121. Overwrite = true
  122. });
  123. }
  124. else
  125. {
  126. if (!reader.Entry.IsDirectory)
  127. {
  128. reader.WriteEntryToDirectory(dir, new ExtractionOptions()
  129. {
  130. ExtractFullPath = true,
  131. Overwrite = true
  132. });
  133. }
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// 创建zip包
  139. /// </summary>
  140. /// <param name="files"></param>
  141. /// <param name="rootdir"></param>
  142. /// <returns></returns>
  143. private ZipArchive CreateZipArchive(List<string> files, string rootdir)
  144. {
  145. var archive = ZipArchive.Create();
  146. var dic = GetFileEntryMaps(files);
  147. var remoteUrls = files.Distinct().Where(s => s.StartsWith("http")).Select(s =>
  148. {
  149. try
  150. {
  151. return new Uri(s);
  152. }
  153. catch (UriFormatException)
  154. {
  155. return null;
  156. }
  157. }).Where(u => u != null).ToList();
  158. foreach (var pair in dic)
  159. {
  160. archive.AddEntry(Path.Combine(rootdir, pair.Value), pair.Key);
  161. }
  162. if (remoteUrls.Any())
  163. {
  164. var streams = new ConcurrentDictionary<string, Stream>();
  165. Parallel.ForEach(remoteUrls, url =>
  166. {
  167. _httpClient.GetAsync(url).ContinueWith(async t =>
  168. {
  169. if (t.IsCompleted)
  170. {
  171. var res = await t;
  172. if (res.IsSuccessStatusCode)
  173. {
  174. Stream stream = await res.Content.ReadAsStreamAsync();
  175. streams[Path.Combine(rootdir, Path.GetFileName(HttpUtility.UrlDecode(url.AbsolutePath)))] = stream;
  176. }
  177. }
  178. }).Wait();
  179. });
  180. foreach (var pair in streams)
  181. {
  182. archive.AddEntry(pair.Key, pair.Value);
  183. }
  184. }
  185. return archive;
  186. }
  187. /// <summary>
  188. /// 获取文件路径和zip-entry的映射
  189. /// </summary>
  190. /// <param name="files"></param>
  191. /// <returns></returns>
  192. private Dictionary<string, string> GetFileEntryMaps(List<string> files)
  193. {
  194. var fileList = new List<string>();
  195. void GetFilesRecurs(string path)
  196. {
  197. //遍历目标文件夹的所有文件
  198. fileList.AddRange(Directory.GetFiles(path));
  199. //遍历目标文件夹的所有文件夹
  200. foreach (var directory in Directory.GetDirectories(path))
  201. {
  202. GetFilesRecurs(directory);
  203. }
  204. }
  205. files
  206. .Where(s => !s.StartsWith("http"))
  207. .ForEach(s =>
  208. {
  209. if (Directory.Exists(s))
  210. {
  211. GetFilesRecurs(s);
  212. }
  213. else
  214. {
  215. fileList.Add(s);
  216. }
  217. });
  218. if (!fileList.Any())
  219. {
  220. return new Dictionary<string, string>();
  221. }
  222. var dirname = new string(fileList.First().Substring(0, fileList.Min(s => s.Length)).TakeWhile((c, i) => fileList.All(s => s[i] == c)).ToArray());
  223. if (!Directory.Exists(dirname))
  224. {
  225. dirname = Directory.GetParent(dirname).FullName;
  226. }
  227. var dic = fileList.ToDictionary(s => s, s => s.Substring(dirname.Length));
  228. return dic;
  229. }
  230. }
  231. }