From a8d065dc9eb7cca1165c7fc4b22b2b1bf4e2b579 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sun, 12 Sep 2021 08:47:30 +0100 Subject: [PATCH] Ensure destination directory exists --- src/SharpCompress/Common/ExtractionMethods.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/SharpCompress/Common/ExtractionMethods.cs b/src/SharpCompress/Common/ExtractionMethods.cs index 738e3f39..8301bceb 100644 --- a/src/SharpCompress/Common/ExtractionMethods.cs +++ b/src/SharpCompress/Common/ExtractionMethods.cs @@ -14,14 +14,25 @@ namespace SharpCompress.Common Action write) { string destinationFileName; - string file = Path.GetFileName(entry.Key); string fullDestinationDirectoryPath = Path.GetFullPath(destinationDirectory); + + //check for trailing slash. + if (fullDestinationDirectoryPath[fullDestinationDirectoryPath.Length - 1] != Path.DirectorySeparatorChar) + { + fullDestinationDirectoryPath += Path.DirectorySeparatorChar; + } + + if (!Directory.Exists(fullDestinationDirectoryPath)) + { + throw new ExtractionException($"Directory does not exist to extract to: {fullDestinationDirectoryPath}"); + } options ??= new ExtractionOptions() { Overwrite = true }; + string file = Path.GetFileName(entry.Key); if (options.ExtractFullPath) { string folder = Path.GetDirectoryName(entry.Key)!;