jellyfin-server/Emby.Common.Implementations/Archiving/ZipClient.cs

195 lines
6.9 KiB
C#
Raw Normal View History

2016-10-28 18:35:17 +00:00
using System.IO;
using MediaBrowser.Model.IO;
2016-11-11 04:25:21 +00:00
using SharpCompress.Archives.Rar;
using SharpCompress.Archives.SevenZip;
using SharpCompress.Archives.Tar;
2013-09-25 00:54:51 +00:00
using SharpCompress.Common;
2016-11-11 04:25:21 +00:00
using SharpCompress.Readers;
using SharpCompress.Readers.Zip;
2013-09-25 00:54:51 +00:00
2016-11-11 04:25:21 +00:00
namespace Emby.Common.Implementations.Archiving
2013-09-25 00:54:51 +00:00
{
/// <summary>
/// Class DotNetZipClient
/// </summary>
public class ZipClient : IZipClient
{
2016-10-28 03:11:21 +00:00
private readonly IFileSystem _fileSystem;
2015-09-13 21:32:02 +00:00
public ZipClient(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
/// <summary>
2013-09-25 00:54:51 +00:00
/// 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)
{
2015-09-13 21:32:02 +00:00
using (var fileStream = _fileSystem.OpenRead(sourceFile))
2013-09-25 00:54:51 +00:00
{
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))
{
2016-11-11 04:25:21 +00:00
var options = new ExtractionOptions();
options.ExtractFullPath = true;
2013-09-25 00:54:51 +00:00
if (overwriteExistingFiles)
{
2016-11-11 04:25:21 +00:00
options.Overwrite = true;
2013-09-25 00:54:51 +00:00
}
reader.WriteAllToDirectory(targetPath, options);
}
}
public void ExtractAllFromZip(Stream source, string targetPath, bool overwriteExistingFiles)
{
using (var reader = ZipReader.Open(source))
{
2016-11-11 04:25:21 +00:00
var options = new ExtractionOptions();
options.ExtractFullPath = true;
if (overwriteExistingFiles)
{
2016-11-11 04:25:21 +00:00
options.Overwrite = true;
}
reader.WriteAllToDirectory(targetPath, options);
}
}
2013-09-25 00:54:51 +00:00
/// <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)
{
2015-09-13 21:32:02 +00:00
using (var fileStream = _fileSystem.OpenRead(sourceFile))
2013-09-25 00:54:51 +00:00
{
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())
{
2016-11-11 04:25:21 +00:00
var options = new ExtractionOptions();
options.ExtractFullPath = true;
2013-09-25 00:54:51 +00:00
if (overwriteExistingFiles)
{
2016-11-11 04:25:21 +00:00
options.Overwrite = true;
2013-09-25 00:54:51 +00:00
}
reader.WriteAllToDirectory(targetPath, options);
}
}
}
2013-10-13 03:39:22 +00:00
/// <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)
{
2015-09-13 21:32:02 +00:00
using (var fileStream = _fileSystem.OpenRead(sourceFile))
2013-10-13 03:39:22 +00:00
{
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())
{
2016-11-11 04:25:21 +00:00
var options = new ExtractionOptions();
options.ExtractFullPath = true;
2013-10-13 03:39:22 +00:00
if (overwriteExistingFiles)
{
2016-11-11 04:25:21 +00:00
options.Overwrite = true;
2013-10-13 03:39:22 +00:00
}
reader.WriteAllToDirectory(targetPath, options);
}
}
}
2013-11-10 18:11:42 +00:00
/// <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)
{
2015-09-13 21:32:02 +00:00
using (var fileStream = _fileSystem.OpenRead(sourceFile))
2013-11-10 18:11:42 +00:00
{
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())
{
2016-11-11 04:25:21 +00:00
var options = new ExtractionOptions();
options.ExtractFullPath = true;
2013-11-10 18:11:42 +00:00
if (overwriteExistingFiles)
{
2016-11-11 04:25:21 +00:00
options.Overwrite = true;
2013-11-10 18:11:42 +00:00
}
reader.WriteAllToDirectory(targetPath, options);
}
}
}
2013-09-25 00:54:51 +00:00
}
}