ZipClient.cs 6.4 KB

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