SevenZipCompressor.cs 6.6 KB

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