diff --git a/src/SharpCompress/Archives/AbstractWritableArchive.cs b/src/SharpCompress/Archives/AbstractWritableArchive.cs index 67f822fd..d5f59301 100644 --- a/src/SharpCompress/Archives/AbstractWritableArchive.cs +++ b/src/SharpCompress/Archives/AbstractWritableArchive.cs @@ -83,8 +83,7 @@ namespace SharpCompress.Archives public TEntry AddEntry(string key, Stream source, bool closeStream, long size = 0, DateTime? modified = null) { - if (key.StartsWith("/") - || key.StartsWith("\\")) + if (key.Length > 0 && key[0] is '/' or '\\') { key = key.Substring(1); } @@ -103,7 +102,7 @@ namespace SharpCompress.Archives foreach (var path in Entries.Select(x => x.Key)) { var p = path.Replace('/', '\\'); - if (p.StartsWith("\\")) + if (p.Length > 0 && p[0] == '\\') { p = p.Substring(1); } diff --git a/src/SharpCompress/Common/ExtractionMethods.cs b/src/SharpCompress/Common/ExtractionMethods.cs index 801661b0..81be96f5 100644 --- a/src/SharpCompress/Common/ExtractionMethods.cs +++ b/src/SharpCompress/Common/ExtractionMethods.cs @@ -25,13 +25,11 @@ namespace SharpCompress.Common if (options.ExtractFullPath) { string folder = Path.GetDirectoryName(entry.Key); - string destdir = Path.GetFullPath( - Path.Combine(fullDestinationDirectoryPath, folder) - ); + string destdir = Path.GetFullPath(Path.Combine(fullDestinationDirectoryPath, folder)); if (!Directory.Exists(destdir)) { - if (!destdir.StartsWith(fullDestinationDirectoryPath)) + if (!destdir.StartsWith(fullDestinationDirectoryPath, StringComparison.Ordinal)) { throw new ExtractionException("Entry is trying to create a directory outside of the destination directory."); } @@ -50,7 +48,7 @@ namespace SharpCompress.Common { destinationFileName = Path.GetFullPath(destinationFileName); - if (!destinationFileName.StartsWith(fullDestinationDirectoryPath)) + if (!destinationFileName.StartsWith(fullDestinationDirectoryPath, StringComparison.Ordinal)) { throw new ExtractionException("Entry is trying to write a file outside of the destination directory."); }