2019-10-10 09:24:41 +01:00
|
|
|
|
using System;
|
2018-06-28 11:46:51 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SharpCompress.Common
|
|
|
|
|
|
{
|
|
|
|
|
|
internal static class ExtractionMethods
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Extract to specific directory, retaining filename
|
|
|
|
|
|
/// </summary>
|
2022-12-20 13:45:47 +00:00
|
|
|
|
public static void WriteEntryToDirectory(
|
|
|
|
|
|
IEntry entry,
|
|
|
|
|
|
string destinationDirectory,
|
|
|
|
|
|
ExtractionOptions? options,
|
|
|
|
|
|
Action<string, ExtractionOptions?> write
|
|
|
|
|
|
)
|
2018-06-28 11:46:51 +01:00
|
|
|
|
{
|
|
|
|
|
|
string destinationFileName;
|
|
|
|
|
|
string fullDestinationDirectoryPath = Path.GetFullPath(destinationDirectory);
|
2021-10-02 15:29:03 +01:00
|
|
|
|
|
2021-09-12 08:47:30 +01:00
|
|
|
|
//check for trailing slash.
|
2022-12-20 13:45:47 +00:00
|
|
|
|
if (
|
|
|
|
|
|
fullDestinationDirectoryPath[fullDestinationDirectoryPath.Length - 1]
|
|
|
|
|
|
!= Path.DirectorySeparatorChar
|
|
|
|
|
|
)
|
2021-09-12 08:47:30 +01:00
|
|
|
|
{
|
|
|
|
|
|
fullDestinationDirectoryPath += Path.DirectorySeparatorChar;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(fullDestinationDirectoryPath))
|
|
|
|
|
|
{
|
2022-12-20 13:45:47 +00:00
|
|
|
|
throw new ExtractionException(
|
|
|
|
|
|
$"Directory does not exist to extract to: {fullDestinationDirectoryPath}"
|
|
|
|
|
|
);
|
2021-09-12 08:47:30 +01:00
|
|
|
|
}
|
2018-06-28 11:46:51 +01:00
|
|
|
|
|
2022-12-20 13:45:47 +00:00
|
|
|
|
options ??= new ExtractionOptions() { Overwrite = true };
|
2018-06-28 11:46:51 +01:00
|
|
|
|
|
2021-09-12 08:47:30 +01:00
|
|
|
|
string file = Path.GetFileName(entry.Key);
|
2018-06-28 11:46:51 +01:00
|
|
|
|
if (options.ExtractFullPath)
|
|
|
|
|
|
{
|
2020-11-18 09:43:08 -08:00
|
|
|
|
string folder = Path.GetDirectoryName(entry.Key)!;
|
2022-12-20 13:45:47 +00:00
|
|
|
|
string destdir = Path.GetFullPath(
|
|
|
|
|
|
Path.Combine(fullDestinationDirectoryPath, folder)
|
|
|
|
|
|
);
|
2018-06-28 11:46:51 +01:00
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(destdir))
|
|
|
|
|
|
{
|
2020-11-18 09:12:01 -08:00
|
|
|
|
if (!destdir.StartsWith(fullDestinationDirectoryPath, StringComparison.Ordinal))
|
2018-06-28 11:46:51 +01:00
|
|
|
|
{
|
2022-12-20 13:45:47 +00:00
|
|
|
|
throw new ExtractionException(
|
|
|
|
|
|
"Entry is trying to create a directory outside of the destination directory."
|
|
|
|
|
|
);
|
2018-06-28 11:46:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(destdir);
|
|
|
|
|
|
}
|
|
|
|
|
|
destinationFileName = Path.Combine(destdir, file);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2021-01-09 13:33:34 +00:00
|
|
|
|
{
|
2018-06-28 11:46:51 +01:00
|
|
|
|
destinationFileName = Path.Combine(fullDestinationDirectoryPath, file);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!entry.IsDirectory)
|
|
|
|
|
|
{
|
|
|
|
|
|
destinationFileName = Path.GetFullPath(destinationFileName);
|
|
|
|
|
|
|
2022-12-20 13:45:47 +00:00
|
|
|
|
if (
|
|
|
|
|
|
!destinationFileName.StartsWith(
|
|
|
|
|
|
fullDestinationDirectoryPath,
|
|
|
|
|
|
StringComparison.Ordinal
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2018-06-28 11:46:51 +01:00
|
|
|
|
{
|
2022-12-20 13:45:47 +00:00
|
|
|
|
throw new ExtractionException(
|
|
|
|
|
|
"Entry is trying to write a file outside of the destination directory."
|
|
|
|
|
|
);
|
2018-06-28 11:46:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
write(destinationFileName, options);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (options.ExtractFullPath && !Directory.Exists(destinationFileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(destinationFileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-01-09 13:33:34 +00:00
|
|
|
|
|
2022-12-20 13:45:47 +00:00
|
|
|
|
public static void WriteEntryToFile(
|
|
|
|
|
|
IEntry entry,
|
|
|
|
|
|
string destinationFileName,
|
|
|
|
|
|
ExtractionOptions? options,
|
|
|
|
|
|
Action<string, FileMode> openAndWrite
|
|
|
|
|
|
)
|
2018-06-28 11:46:51 +01:00
|
|
|
|
{
|
2018-11-01 21:48:48 +00:00
|
|
|
|
if (entry.LinkTarget != null)
|
2018-06-28 11:46:51 +01:00
|
|
|
|
{
|
2020-05-23 16:27:55 -07:00
|
|
|
|
if (options?.WriteSymbolicLink is null)
|
2018-11-01 21:48:48 +00:00
|
|
|
|
{
|
2022-12-20 13:45:47 +00:00
|
|
|
|
throw new ExtractionException(
|
|
|
|
|
|
"Entry is a symbolic link but ExtractionOptions.WriteSymbolicLink delegate is null"
|
|
|
|
|
|
);
|
2018-11-01 21:48:48 +00:00
|
|
|
|
}
|
2018-11-03 09:45:09 +00:00
|
|
|
|
options.WriteSymbolicLink(destinationFileName, entry.LinkTarget);
|
2018-06-28 11:46:51 +01:00
|
|
|
|
}
|
2018-11-01 21:48:48 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
FileMode fm = FileMode.Create;
|
2022-12-20 13:45:47 +00:00
|
|
|
|
options ??= new ExtractionOptions() { Overwrite = true };
|
2018-06-28 11:46:51 +01:00
|
|
|
|
|
2018-11-01 21:48:48 +00:00
|
|
|
|
if (!options.Overwrite)
|
|
|
|
|
|
{
|
|
|
|
|
|
fm = FileMode.CreateNew;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
openAndWrite(destinationFileName, fm);
|
|
|
|
|
|
entry.PreserveExtractionOptions(destinationFileName, options);
|
|
|
|
|
|
}
|
2018-06-28 11:46:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-12-20 13:45:47 +00:00
|
|
|
|
}
|