Skip to content

Commit

Permalink
fix the conflict between paths for outputpath & archivefilename
Browse files Browse the repository at this point in the history
  • Loading branch information
iRebbok committed Jul 22, 2020
1 parent 54c2a31 commit 8717ebd
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions 7Zip4Powershell/Compress7Zip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,26 @@ public override void Execute() {
};
}

// true -> cmdlet assigned
// false -> cmdlet not assigned
var outputPathIsNotEmptyOrNull = !string.IsNullOrEmpty(_cmdlet.OutputPath);
// Final path for the archive
var outputPath = !string.IsNullOrEmpty(_cmdlet.OutputPath)
? _cmdlet.OutputPath
var outputPath = outputPathIsNotEmptyOrNull
? _cmdlet.OutputPath
: _cmdlet.SessionState.Path.CurrentFileSystemLocation.Path;

// If the `OutputPath` parameter is not assigned
// and there is an absolute or relative directory in the `ArchiveFileName`,
// then use it instead
var archiveDirectory = System.IO.Path.GetDirectoryName(_cmdlet.ArchiveFileName);
if (!string.IsNullOrEmpty(archiveDirectory) && !outputPathIsNotEmptyOrNull)
{
if (System.IO.Path.IsPathRooted(archiveDirectory))
outputPath = archiveDirectory;
else // If the path isn't absolute, then combine it with the path from which the script was called
outputPath = System.IO.Path.Combine(outputPath, archiveDirectory);
}

// Check whether the output path is a path to the file
// The folder and file name cannot be the same in the same folder
if (File.Exists(outputPath)) {
Expand All @@ -203,7 +218,6 @@ public override void Execute() {
}

var directoryOrFiles = _cmdlet._directoryOrFilesFromPipeline
// Don't put outputPath here, it will break the relative path
.Select(System.IO.Path.GetFullPath).ToArray();
var archiveFileName = System.IO.Path.GetFullPath(System.IO.Path.Combine(outputPath, System.IO.Path.GetFileName(_cmdlet.ArchiveFileName)));

Expand Down

0 comments on commit 8717ebd

Please sign in to comment.