ZipClient.cs 5.7 KB

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