From 8d6f1014f08bf7e1d28510ae92c0c253fabedb73 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 5 May 2026 17:02:28 +0100 Subject: [PATCH] consolidate path comparsion --- src/SharpCompress/Archives/IArchiveExtensions.cs | 11 +---------- .../Archives/IAsyncArchiveExtensions.cs | 11 +---------- .../Common/ExtractionMethods.Async.cs | 4 ++-- src/SharpCompress/Common/ExtractionMethods.cs | 14 ++------------ src/SharpCompress/Utility.cs | 10 ++++++++++ 5 files changed, 16 insertions(+), 34 deletions(-) diff --git a/src/SharpCompress/Archives/IArchiveExtensions.cs b/src/SharpCompress/Archives/IArchiveExtensions.cs index a1ac2226..17a87962 100644 --- a/src/SharpCompress/Archives/IArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IArchiveExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Runtime.InteropServices; using SharpCompress.Common; using SharpCompress.Readers; @@ -9,14 +8,6 @@ namespace SharpCompress.Archives; public static class IArchiveExtensions { - /// - /// Gets the appropriate StringComparison for path checks based on the file system. - /// Windows uses case-insensitive file systems, while Unix-like systems use case-sensitive file systems. - /// - private static StringComparison PathComparison => - RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? StringComparison.OrdinalIgnoreCase - : StringComparison.Ordinal; extension(IArchive archive) { /// @@ -83,7 +74,7 @@ public static class IArchiveExtensions if (!Directory.Exists(destdir) && seenDirectories.Add(destdir)) { - if (!destdir.StartsWith(fullDestinationDirectoryPath, PathComparison)) + if (!destdir.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison)) { throw new ExtractionException( "Entry is trying to create a directory outside of the destination directory." diff --git a/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs b/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs index 51e8f3f2..26facb6b 100644 --- a/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using SharpCompress.Common; @@ -11,14 +10,6 @@ namespace SharpCompress.Archives; public static class IAsyncArchiveExtensions { - /// - /// Gets the appropriate StringComparison for path checks based on the file system. - /// Windows uses case-insensitive file systems, while Unix-like systems use case-sensitive file systems. - /// - private static StringComparison PathComparison => - RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? StringComparison.OrdinalIgnoreCase - : StringComparison.Ordinal; extension(IAsyncArchive archive) { /// @@ -105,7 +96,7 @@ public static class IAsyncArchiveExtensions if (!Directory.Exists(destdir) && seenDirectories.Add(destdir)) { - if (!destdir.StartsWith(fullDestinationDirectoryPath, PathComparison)) + if (!destdir.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison)) { throw new ExtractionException( "Entry is trying to create a directory outside of the destination directory." diff --git a/src/SharpCompress/Common/ExtractionMethods.Async.cs b/src/SharpCompress/Common/ExtractionMethods.Async.cs index 8792e06b..86347310 100644 --- a/src/SharpCompress/Common/ExtractionMethods.Async.cs +++ b/src/SharpCompress/Common/ExtractionMethods.Async.cs @@ -45,7 +45,7 @@ internal static partial class ExtractionMethods if (!Directory.Exists(destdir)) { - if (!destdir.StartsWith(fullDestinationDirectoryPath, PathComparison)) + if (!destdir.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison)) { throw new ExtractionException( "Entry is trying to create a directory outside of the destination directory." @@ -65,7 +65,7 @@ internal static partial class ExtractionMethods { destinationFileName = Path.GetFullPath(destinationFileName); - if (!destinationFileName.StartsWith(fullDestinationDirectoryPath, PathComparison)) + if (!destinationFileName.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison)) { throw new ExtractionException( "Entry is trying to write a file outside of the destination directory." diff --git a/src/SharpCompress/Common/ExtractionMethods.cs b/src/SharpCompress/Common/ExtractionMethods.cs index 526c6e26..bde397d4 100644 --- a/src/SharpCompress/Common/ExtractionMethods.cs +++ b/src/SharpCompress/Common/ExtractionMethods.cs @@ -1,20 +1,10 @@ using System; using System.IO; -using System.Runtime.InteropServices; namespace SharpCompress.Common; internal static partial class ExtractionMethods { - /// - /// Gets the appropriate StringComparison for path checks based on the file system. - /// Windows uses case-insensitive file systems, while Unix-like systems use case-sensitive file systems. - /// - private static StringComparison PathComparison => - RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? StringComparison.OrdinalIgnoreCase - : StringComparison.Ordinal; - /// /// Extract to specific directory, retaining filename /// @@ -55,7 +45,7 @@ internal static partial class ExtractionMethods if (!Directory.Exists(destdir)) { - if (!destdir.StartsWith(fullDestinationDirectoryPath, PathComparison)) + if (!destdir.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison)) { throw new ExtractionException( "Entry is trying to create a directory outside of the destination directory." @@ -75,7 +65,7 @@ internal static partial class ExtractionMethods { destinationFileName = Path.GetFullPath(destinationFileName); - if (!destinationFileName.StartsWith(fullDestinationDirectoryPath, PathComparison)) + if (!destinationFileName.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison)) { throw new ExtractionException( "Entry is trying to write a file outside of the destination directory." diff --git a/src/SharpCompress/Utility.cs b/src/SharpCompress/Utility.cs index af8572d7..017bc965 100644 --- a/src/SharpCompress/Utility.cs +++ b/src/SharpCompress/Utility.cs @@ -3,6 +3,7 @@ using System.Buffers; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; +using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -13,6 +14,15 @@ namespace SharpCompress; internal static partial class Utility { + /// + /// Gets the appropriate StringComparison for path checks based on the file system. + /// Windows uses case-insensitive file systems, while Unix-like systems use case-sensitive file systems. + /// + internal static StringComparison PathComparison => + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + ? StringComparison.OrdinalIgnoreCase + : StringComparison.Ordinal; + public static bool UseSyncOverAsyncDispose() { var useSyncOverAsync = false;