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