ZipClient.cs 6.9 KB

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