ZipClient.cs 6.5 KB

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