ZipClient.cs 6.4 KB

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