2019-01-13 20:54:44 +01:00
|
|
|
using System.IO;
|
2016-10-28 20:35:17 +02:00
|
|
|
using MediaBrowser.Model.IO;
|
2016-11-11 05:25:21 +01:00
|
|
|
using SharpCompress.Archives.SevenZip;
|
|
|
|
using SharpCompress.Archives.Tar;
|
2018-09-12 19:26:21 +02:00
|
|
|
using SharpCompress.Common;
|
2016-11-11 05:25:21 +01:00
|
|
|
using SharpCompress.Readers;
|
2017-09-25 07:06:15 +02:00
|
|
|
using SharpCompress.Readers.GZip;
|
2016-11-11 05:25:21 +01:00
|
|
|
using SharpCompress.Readers.Zip;
|
2013-09-25 02:54:51 +02:00
|
|
|
|
2017-08-16 08:43:41 +02:00
|
|
|
namespace Emby.Server.Implementations.Archiving
|
2013-09-25 02:54:51 +02:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class DotNetZipClient
|
|
|
|
/// </summary>
|
|
|
|
public class ZipClient : IZipClient
|
|
|
|
{
|
2019-01-08 00:24:34 +01:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2015-09-13 23:32:02 +02:00
|
|
|
|
2019-01-08 00:24:34 +01:00
|
|
|
public ZipClient(IFileSystem fileSystem)
|
|
|
|
{
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
}
|
2015-09-13 23:32:02 +02:00
|
|
|
|
2019-01-08 00:24:34 +01:00
|
|
|
/// <summary>
|
2013-09-25 02:54:51 +02: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)
|
|
|
|
{
|
2019-01-26 22:31:59 +01:00
|
|
|
using (var fileStream = File.OpenRead(sourceFile))
|
2013-09-25 02:54:51 +02: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 05:25:21 +01:00
|
|
|
var options = new ExtractionOptions();
|
|
|
|
options.ExtractFullPath = true;
|
2013-09-25 02:54:51 +02:00
|
|
|
|
|
|
|
if (overwriteExistingFiles)
|
|
|
|
{
|
2016-11-11 05:25:21 +01:00
|
|
|
options.Overwrite = true;
|
2013-09-25 02:54:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reader.WriteAllToDirectory(targetPath, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 03:50:02 +01:00
|
|
|
public void ExtractAllFromZip(Stream source, string targetPath, bool overwriteExistingFiles)
|
|
|
|
{
|
|
|
|
using (var reader = ZipReader.Open(source))
|
|
|
|
{
|
2016-11-11 05:25:21 +01:00
|
|
|
var options = new ExtractionOptions();
|
|
|
|
options.ExtractFullPath = true;
|
2014-12-29 03:50:02 +01:00
|
|
|
|
|
|
|
if (overwriteExistingFiles)
|
|
|
|
{
|
2016-11-11 05:25:21 +01:00
|
|
|
options.Overwrite = true;
|
2014-12-29 03:50:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
reader.WriteAllToDirectory(targetPath, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 07:06:15 +02:00
|
|
|
public void ExtractAllFromGz(Stream source, string targetPath, bool overwriteExistingFiles)
|
|
|
|
{
|
|
|
|
using (var reader = GZipReader.Open(source))
|
|
|
|
{
|
|
|
|
var options = new ExtractionOptions();
|
|
|
|
options.ExtractFullPath = true;
|
|
|
|
|
|
|
|
if (overwriteExistingFiles)
|
|
|
|
{
|
|
|
|
options.Overwrite = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.WriteAllToDirectory(targetPath, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 23:11:04 +01:00
|
|
|
public void ExtractFirstFileFromGz(Stream source, string targetPath, string defaultFileName)
|
|
|
|
{
|
|
|
|
using (var reader = GZipReader.Open(source))
|
|
|
|
{
|
|
|
|
if (reader.MoveToNextEntry())
|
|
|
|
{
|
|
|
|
var entry = reader.Entry;
|
|
|
|
|
|
|
|
var filename = entry.Key;
|
|
|
|
if (string.IsNullOrWhiteSpace(filename))
|
|
|
|
{
|
|
|
|
filename = defaultFileName;
|
|
|
|
}
|
|
|
|
reader.WriteEntryToFile(Path.Combine(targetPath, filename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 02:54:51 +02: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)
|
|
|
|
{
|
2019-01-26 22:31:59 +01:00
|
|
|
using (var fileStream = File.OpenRead(sourceFile))
|
2013-09-25 02:54:51 +02: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 05:25:21 +01:00
|
|
|
var options = new ExtractionOptions();
|
|
|
|
options.ExtractFullPath = true;
|
2013-09-25 02:54:51 +02:00
|
|
|
|
|
|
|
if (overwriteExistingFiles)
|
|
|
|
{
|
2016-11-11 05:25:21 +01:00
|
|
|
options.Overwrite = true;
|
2013-09-25 02:54:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reader.WriteAllToDirectory(targetPath, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-13 05:39:22 +02: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)
|
|
|
|
{
|
2019-01-26 22:31:59 +01:00
|
|
|
using (var fileStream = File.OpenRead(sourceFile))
|
2013-10-13 05:39:22 +02: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 05:25:21 +01:00
|
|
|
var options = new ExtractionOptions();
|
|
|
|
options.ExtractFullPath = true;
|
2013-10-13 05:39:22 +02:00
|
|
|
|
|
|
|
if (overwriteExistingFiles)
|
|
|
|
{
|
2016-11-11 05:25:21 +01:00
|
|
|
options.Overwrite = true;
|
2013-10-13 05:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reader.WriteAllToDirectory(targetPath, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-25 02:54:51 +02:00
|
|
|
}
|
|
|
|
}
|