jellyfin/MediaBrowser.Common.Implementations/Archiving/ZipClient.cs

191 lines
6.8 KiB
C#
Raw Normal View History

2013-09-24 17:54:51 -07:00
using MediaBrowser.Model.IO;
2013-11-10 11:11:42 -07:00
using SharpCompress.Archive.Rar;
2013-09-24 17:54:51 -07:00
using SharpCompress.Archive.SevenZip;
2013-10-12 20:39:22 -07:00
using SharpCompress.Archive.Tar;
2013-09-24 17:54:51 -07:00
using SharpCompress.Common;
using SharpCompress.Reader;
using SharpCompress.Reader.Zip;
2013-09-24 17:54:51 -07:00
using System.IO;
2015-10-03 21:23:11 -07:00
using CommonIO;
2013-09-24 17:54:51 -07:00
namespace MediaBrowser.Common.Implementations.Archiving
{
/// <summary>
/// Class DotNetZipClient
/// </summary>
public class ZipClient : IZipClient
{
2015-09-13 14:32:02 -07:00
private IFileSystem _fileSystem;
public ZipClient(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
/// <summary>
2013-09-24 17:54:51 -07: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 14:32:02 -07:00
using (var fileStream = _fileSystem.OpenRead(sourceFile))
2013-09-24 17:54:51 -07: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))
{
var options = ExtractOptions.ExtractFullPath;
if (overwriteExistingFiles)
{
options = options | ExtractOptions.Overwrite;
}
reader.WriteAllToDirectory(targetPath, options);
}
}
public void ExtractAllFromZip(Stream source, string targetPath, bool overwriteExistingFiles)
{
using (var reader = ZipReader.Open(source))
{
var options = ExtractOptions.ExtractFullPath;
if (overwriteExistingFiles)
{
options = options | ExtractOptions.Overwrite;
}
reader.WriteAllToDirectory(targetPath, options);
}
}
2013-09-24 17:54:51 -07: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 14:32:02 -07:00
using (var fileStream = _fileSystem.OpenRead(sourceFile))
2013-09-24 17:54:51 -07: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())
{
var options = ExtractOptions.ExtractFullPath;
if (overwriteExistingFiles)
{
options = options | ExtractOptions.Overwrite;
}
reader.WriteAllToDirectory(targetPath, options);
}
}
}
2013-10-12 20:39:22 -07: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 14:32:02 -07:00
using (var fileStream = _fileSystem.OpenRead(sourceFile))
2013-10-12 20:39:22 -07: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())
{
var options = ExtractOptions.ExtractFullPath;
if (overwriteExistingFiles)
{
options = options | ExtractOptions.Overwrite;
}
reader.WriteAllToDirectory(targetPath, options);
}
}
}
2013-11-10 11:11:42 -07: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 14:32:02 -07:00
using (var fileStream = _fileSystem.OpenRead(sourceFile))
2013-11-10 11:11:42 -07: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())
{
var options = ExtractOptions.ExtractFullPath;
if (overwriteExistingFiles)
{
options = options | ExtractOptions.Overwrite;
}
reader.WriteAllToDirectory(targetPath, options);
}
}
}
2013-09-24 17:54:51 -07:00
}
}