diff options
| author | Luke <luke.pulverenti@gmail.com> | 2017-08-19 16:06:14 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-08-19 16:06:14 -0400 |
| commit | ff2f3108ee62fc8c943b1d83ec6312d6696990d8 (patch) | |
| tree | 80f1823f901bdadad6af4fce8d36d9b73f79c0fb /Emby.Server.Implementations/Archiving/ZipClient.cs | |
| parent | 6b077f3f8467caa26388a25e6acd1990d286b5d0 (diff) | |
| parent | c58db90c950c766321615eca236557d87b8d1b74 (diff) | |
Merge pull request #2832 from MediaBrowser/beta
Beta
Diffstat (limited to 'Emby.Server.Implementations/Archiving/ZipClient.cs')
| -rw-r--r-- | Emby.Server.Implementations/Archiving/ZipClient.cs | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/Emby.Server.Implementations/Archiving/ZipClient.cs b/Emby.Server.Implementations/Archiving/ZipClient.cs new file mode 100644 index 000000000..3218d56c6 --- /dev/null +++ b/Emby.Server.Implementations/Archiving/ZipClient.cs @@ -0,0 +1,193 @@ +using System.IO; +using MediaBrowser.Model.IO; +using SharpCompress.Archives.Rar; +using SharpCompress.Archives.SevenZip; +using SharpCompress.Archives.Tar; +using SharpCompress.Readers; +using SharpCompress.Readers.Zip; + +namespace Emby.Server.Implementations.Archiving +{ + /// <summary> + /// Class DotNetZipClient + /// </summary> + public class ZipClient : IZipClient + { + private readonly IFileSystem _fileSystem; + + public ZipClient(IFileSystem fileSystem) + { + _fileSystem = fileSystem; + } + + /// <summary> + /// Extracts all. + /// </summary> + /// <param name="sourceFile">The source file.</param> + /// <param name="targetPath">The target path.</param> + /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param> + public void ExtractAll(string sourceFile, string targetPath, bool overwriteExistingFiles) + { + using (var fileStream = _fileSystem.OpenRead(sourceFile)) + { + ExtractAll(fileStream, targetPath, overwriteExistingFiles); + } + } + + /// <summary> + /// Extracts all. + /// </summary> + /// <param name="source">The source.</param> + /// <param name="targetPath">The target path.</param> + /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param> + public void ExtractAll(Stream source, string targetPath, bool overwriteExistingFiles) + { + using (var reader = ReaderFactory.Open(source)) + { + var options = new ExtractionOptions(); + options.ExtractFullPath = true; + + if (overwriteExistingFiles) + { + options.Overwrite = true; + } + + reader.WriteAllToDirectory(targetPath, options); + } + } + + public void ExtractAllFromZip(Stream source, string targetPath, bool overwriteExistingFiles) + { + using (var reader = ZipReader.Open(source)) + { + var options = new ExtractionOptions(); + options.ExtractFullPath = true; + + if (overwriteExistingFiles) + { + options.Overwrite = true; + } + + reader.WriteAllToDirectory(targetPath, options); + } + } + + /// <summary> + /// Extracts all from7z. + /// </summary> + /// <param name="sourceFile">The source file.</param> + /// <param name="targetPath">The target path.</param> + /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param> + public void ExtractAllFrom7z(string sourceFile, string targetPath, bool overwriteExistingFiles) + { + using (var fileStream = _fileSystem.OpenRead(sourceFile)) + { + ExtractAllFrom7z(fileStream, targetPath, overwriteExistingFiles); + } + } + + /// <summary> + /// Extracts all from7z. + /// </summary> + /// <param name="source">The source.</param> + /// <param name="targetPath">The target path.</param> + /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param> + public void ExtractAllFrom7z(Stream source, string targetPath, bool overwriteExistingFiles) + { + using (var archive = SevenZipArchive.Open(source)) + { + using (var reader = archive.ExtractAllEntries()) + { + var options = new ExtractionOptions(); + options.ExtractFullPath = true; + + if (overwriteExistingFiles) + { + options.Overwrite = true; + } + + reader.WriteAllToDirectory(targetPath, options); + } + } + } + + + /// <summary> + /// Extracts all from tar. + /// </summary> + /// <param name="sourceFile">The source file.</param> + /// <param name="targetPath">The target path.</param> + /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param> + public void ExtractAllFromTar(string sourceFile, string targetPath, bool overwriteExistingFiles) + { + using (var fileStream = _fileSystem.OpenRead(sourceFile)) + { + ExtractAllFromTar(fileStream, targetPath, overwriteExistingFiles); + } + } + + /// <summary> + /// Extracts all from tar. + /// </summary> + /// <param name="source">The source.</param> + /// <param name="targetPath">The target path.</param> + /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param> + public void ExtractAllFromTar(Stream source, string targetPath, bool overwriteExistingFiles) + { + using (var archive = TarArchive.Open(source)) + { + using (var reader = archive.ExtractAllEntries()) + { + var options = new ExtractionOptions(); + options.ExtractFullPath = true; + + if (overwriteExistingFiles) + { + options.Overwrite = true; + } + + reader.WriteAllToDirectory(targetPath, options); + } + } + } + + /// <summary> + /// Extracts all from rar. + /// </summary> + /// <param name="sourceFile">The source file.</param> + /// <param name="targetPath">The target path.</param> + /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param> + public void ExtractAllFromRar(string sourceFile, string targetPath, bool overwriteExistingFiles) + { + using (var fileStream = _fileSystem.OpenRead(sourceFile)) + { + ExtractAllFromRar(fileStream, targetPath, overwriteExistingFiles); + } + } + + /// <summary> + /// Extracts all from rar. + /// </summary> + /// <param name="source">The source.</param> + /// <param name="targetPath">The target path.</param> + /// <param name="overwriteExistingFiles">if set to <c>true</c> [overwrite existing files].</param> + public void ExtractAllFromRar(Stream source, string targetPath, bool overwriteExistingFiles) + { + using (var archive = RarArchive.Open(source)) + { + using (var reader = archive.ExtractAllEntries()) + { + var options = new ExtractionOptions(); + options.ExtractFullPath = true; + + if (overwriteExistingFiles) + { + options.Overwrite = true; + } + + reader.WriteAllToDirectory(targetPath, options); + } + } + } + } +} |
