2
0

ZipClient.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System.IO;
  2. using MediaBrowser.Model.IO;
  3. using SharpCompress.Archives.Rar;
  4. using SharpCompress.Archives.SevenZip;
  5. using SharpCompress.Archives.Tar;
  6. using SharpCompress.Common;
  7. using SharpCompress.Readers;
  8. using SharpCompress.Readers.Zip;
  9. namespace Emby.Common.Implementations.Archiving
  10. {
  11. /// <summary>
  12. /// Class DotNetZipClient
  13. /// </summary>
  14. public class ZipClient : IZipClient
  15. {
  16. private readonly IFileSystem _fileSystem;
  17. public ZipClient(IFileSystem fileSystem)
  18. {
  19. _fileSystem = fileSystem;
  20. }
  21. /// <summary>
  22. /// Extracts all.
  23. /// </summary>
  24. /// <param name="sourceFile">The source file.</param>
  25. /// <param name="targetPath">The target path.</param>
  26. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  27. public void ExtractAll(string sourceFile, string targetPath, bool overwriteExistingFiles)
  28. {
  29. using (var fileStream = _fileSystem.OpenRead(sourceFile))
  30. {
  31. ExtractAll(fileStream, targetPath, overwriteExistingFiles);
  32. }
  33. }
  34. /// <summary>
  35. /// Extracts all.
  36. /// </summary>
  37. /// <param name="source">The source.</param>
  38. /// <param name="targetPath">The target path.</param>
  39. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  40. public void ExtractAll(Stream source, string targetPath, bool overwriteExistingFiles)
  41. {
  42. using (var reader = ReaderFactory.Open(source))
  43. {
  44. var options = new ExtractionOptions();
  45. options.ExtractFullPath = true;
  46. if (overwriteExistingFiles)
  47. {
  48. options.Overwrite = true;
  49. }
  50. reader.WriteAllToDirectory(targetPath, options);
  51. }
  52. }
  53. public void ExtractAllFromZip(Stream source, string targetPath, bool overwriteExistingFiles)
  54. {
  55. using (var reader = ZipReader.Open(source))
  56. {
  57. var options = new ExtractionOptions();
  58. options.ExtractFullPath = true;
  59. if (overwriteExistingFiles)
  60. {
  61. options.Overwrite = true;
  62. }
  63. reader.WriteAllToDirectory(targetPath, options);
  64. }
  65. }
  66. /// <summary>
  67. /// Extracts all from7z.
  68. /// </summary>
  69. /// <param name="sourceFile">The source file.</param>
  70. /// <param name="targetPath">The target path.</param>
  71. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  72. public void ExtractAllFrom7z(string sourceFile, string targetPath, bool overwriteExistingFiles)
  73. {
  74. using (var fileStream = _fileSystem.OpenRead(sourceFile))
  75. {
  76. ExtractAllFrom7z(fileStream, targetPath, overwriteExistingFiles);
  77. }
  78. }
  79. /// <summary>
  80. /// Extracts all from7z.
  81. /// </summary>
  82. /// <param name="source">The source.</param>
  83. /// <param name="targetPath">The target path.</param>
  84. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  85. public void ExtractAllFrom7z(Stream source, string targetPath, bool overwriteExistingFiles)
  86. {
  87. using (var archive = SevenZipArchive.Open(source))
  88. {
  89. using (var reader = archive.ExtractAllEntries())
  90. {
  91. var options = new ExtractionOptions();
  92. options.ExtractFullPath = true;
  93. if (overwriteExistingFiles)
  94. {
  95. options.Overwrite = true;
  96. }
  97. reader.WriteAllToDirectory(targetPath, options);
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// Extracts all from tar.
  103. /// </summary>
  104. /// <param name="sourceFile">The source file.</param>
  105. /// <param name="targetPath">The target path.</param>
  106. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  107. public void ExtractAllFromTar(string sourceFile, string targetPath, bool overwriteExistingFiles)
  108. {
  109. using (var fileStream = _fileSystem.OpenRead(sourceFile))
  110. {
  111. ExtractAllFromTar(fileStream, targetPath, overwriteExistingFiles);
  112. }
  113. }
  114. /// <summary>
  115. /// Extracts all from tar.
  116. /// </summary>
  117. /// <param name="source">The source.</param>
  118. /// <param name="targetPath">The target path.</param>
  119. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  120. public void ExtractAllFromTar(Stream source, string targetPath, bool overwriteExistingFiles)
  121. {
  122. using (var archive = TarArchive.Open(source))
  123. {
  124. using (var reader = archive.ExtractAllEntries())
  125. {
  126. var options = new ExtractionOptions();
  127. options.ExtractFullPath = true;
  128. if (overwriteExistingFiles)
  129. {
  130. options.Overwrite = true;
  131. }
  132. reader.WriteAllToDirectory(targetPath, options);
  133. }
  134. }
  135. }
  136. /// <summary>
  137. /// Extracts all from rar.
  138. /// </summary>
  139. /// <param name="sourceFile">The source file.</param>
  140. /// <param name="targetPath">The target path.</param>
  141. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  142. public void ExtractAllFromRar(string sourceFile, string targetPath, bool overwriteExistingFiles)
  143. {
  144. using (var fileStream = _fileSystem.OpenRead(sourceFile))
  145. {
  146. ExtractAllFromRar(fileStream, targetPath, overwriteExistingFiles);
  147. }
  148. }
  149. /// <summary>
  150. /// Extracts all from rar.
  151. /// </summary>
  152. /// <param name="source">The source.</param>
  153. /// <param name="targetPath">The target path.</param>
  154. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  155. public void ExtractAllFromRar(Stream source, string targetPath, bool overwriteExistingFiles)
  156. {
  157. using (var archive = RarArchive.Open(source))
  158. {
  159. using (var reader = archive.ExtractAllEntries())
  160. {
  161. var options = new ExtractionOptions();
  162. options.ExtractFullPath = true;
  163. if (overwriteExistingFiles)
  164. {
  165. options.Overwrite = true;
  166. }
  167. reader.WriteAllToDirectory(targetPath, options);
  168. }
  169. }
  170. }
  171. }
  172. }