ZipClient.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System.IO;
  2. using MediaBrowser.Model.IO;
  3. using SharpCompress.Archives.SevenZip;
  4. using SharpCompress.Archives.Tar;
  5. using SharpCompress.Common;
  6. using SharpCompress.Readers;
  7. using SharpCompress.Readers.GZip;
  8. using SharpCompress.Readers.Zip;
  9. namespace Emby.Server.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 = File.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. public void ExtractAllFromGz(Stream source, string targetPath, bool overwriteExistingFiles)
  67. {
  68. using (var reader = GZipReader.Open(source))
  69. {
  70. var options = new ExtractionOptions();
  71. options.ExtractFullPath = true;
  72. if (overwriteExistingFiles)
  73. {
  74. options.Overwrite = true;
  75. }
  76. reader.WriteAllToDirectory(targetPath, options);
  77. }
  78. }
  79. public void ExtractFirstFileFromGz(Stream source, string targetPath, string defaultFileName)
  80. {
  81. using (var reader = GZipReader.Open(source))
  82. {
  83. if (reader.MoveToNextEntry())
  84. {
  85. var entry = reader.Entry;
  86. var filename = entry.Key;
  87. if (string.IsNullOrWhiteSpace(filename))
  88. {
  89. filename = defaultFileName;
  90. }
  91. reader.WriteEntryToFile(Path.Combine(targetPath, filename));
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Extracts all from7z.
  97. /// </summary>
  98. /// <param name="sourceFile">The source file.</param>
  99. /// <param name="targetPath">The target path.</param>
  100. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  101. public void ExtractAllFrom7z(string sourceFile, string targetPath, bool overwriteExistingFiles)
  102. {
  103. using (var fileStream = File.OpenRead(sourceFile))
  104. {
  105. ExtractAllFrom7z(fileStream, targetPath, overwriteExistingFiles);
  106. }
  107. }
  108. /// <summary>
  109. /// Extracts all from7z.
  110. /// </summary>
  111. /// <param name="source">The source.</param>
  112. /// <param name="targetPath">The target path.</param>
  113. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  114. public void ExtractAllFrom7z(Stream source, string targetPath, bool overwriteExistingFiles)
  115. {
  116. using (var archive = SevenZipArchive.Open(source))
  117. {
  118. using (var reader = archive.ExtractAllEntries())
  119. {
  120. var options = new ExtractionOptions();
  121. options.ExtractFullPath = true;
  122. if (overwriteExistingFiles)
  123. {
  124. options.Overwrite = true;
  125. }
  126. reader.WriteAllToDirectory(targetPath, options);
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Extracts all from tar.
  132. /// </summary>
  133. /// <param name="sourceFile">The source file.</param>
  134. /// <param name="targetPath">The target path.</param>
  135. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  136. public void ExtractAllFromTar(string sourceFile, string targetPath, bool overwriteExistingFiles)
  137. {
  138. using (var fileStream = File.OpenRead(sourceFile))
  139. {
  140. ExtractAllFromTar(fileStream, targetPath, overwriteExistingFiles);
  141. }
  142. }
  143. /// <summary>
  144. /// Extracts all from tar.
  145. /// </summary>
  146. /// <param name="source">The source.</param>
  147. /// <param name="targetPath">The target path.</param>
  148. /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param>
  149. public void ExtractAllFromTar(Stream source, string targetPath, bool overwriteExistingFiles)
  150. {
  151. using (var archive = TarArchive.Open(source))
  152. {
  153. using (var reader = archive.ExtractAllEntries())
  154. {
  155. var options = new ExtractionOptions();
  156. options.ExtractFullPath = true;
  157. if (overwriteExistingFiles)
  158. {
  159. options.Overwrite = true;
  160. }
  161. reader.WriteAllToDirectory(targetPath, options);
  162. }
  163. }
  164. }
  165. }
  166. }