SevenZipCompressor.cs 10 KB

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