diff --git a/7Zip4Powershell/7Zip4Powershell.csproj b/7Zip4Powershell/7Zip4Powershell.csproj index b2597ea..f4e30e4 100644 --- a/7Zip4Powershell/7Zip4Powershell.csproj +++ b/7Zip4Powershell/7Zip4Powershell.csproj @@ -19,20 +19,24 @@ - - C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll - false + + False + ..\Libs\System.Management.Automation.dll - 5.2.4 + 5.3.4 runtime; build; native; contentfiles; analyzers; buildtransitive all + + + + \ No newline at end of file diff --git a/7Zip4Powershell/Compress7Zip.cs b/7Zip4Powershell/Compress7Zip.cs index 2493c63..d0f6686 100644 --- a/7Zip4Powershell/Compress7Zip.cs +++ b/7Zip4Powershell/Compress7Zip.cs @@ -32,6 +32,9 @@ public class Compress7Zip : ThreadedCmdlet { [Parameter(Position = 2, Mandatory = false, HelpMessage = "The filter to be applied if Path points to a directory")] public string Filter { get; set; } = "*"; + [Parameter(HelpMessage = "Output path for a compressed archive")] + public string OutputPath { get; set; } + private List _directoryOrFilesFromPipeline; [Parameter] @@ -183,9 +186,26 @@ public override void Execute() { }; } + // Final path for the archive + var outputPath = !string.IsNullOrEmpty(_cmdlet.OutputPath) + ? _cmdlet.OutputPath + : _cmdlet.SessionState.Path.CurrentFileSystemLocation.Path; + + // 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)) { + throw new ArgumentException("The output path is a file, not a directory"); + } + + // If the directory doesn't exist, create it + if (!Directory.Exists(outputPath)) { + Directory.CreateDirectory(outputPath); + } + var directoryOrFiles = _cmdlet._directoryOrFilesFromPipeline + // Don't put outputPath here, it will break the relative path .Select(path => new FileInfo(System.IO.Path.Combine(_cmdlet.SessionState.Path.CurrentFileSystemLocation.Path, path)).FullName).ToArray(); - var archiveFileName = new FileInfo(System.IO.Path.Combine(_cmdlet.SessionState.Path.CurrentFileSystemLocation.Path, _cmdlet.ArchiveFileName)).FullName; + var archiveFileName = new FileInfo(System.IO.Path.Combine(outputPath, _cmdlet.ArchiveFileName)).FullName; var activity = directoryOrFiles.Length > 1 ? $"Compressing {directoryOrFiles.Length} Files to {archiveFileName}" diff --git a/Libs/System.Management.Automation.dll b/Libs/System.Management.Automation.dll new file mode 100644 index 0000000..7ac6b7b Binary files /dev/null and b/Libs/System.Management.Automation.dll differ diff --git a/README.md b/README.md index 0d7dc67..b85a57b 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,9 @@ A list of all custom parameters can be found [here](https://sevenzip.osdn.jp/chm ### vNext * Replaces *SevenZipSharp.Net45* with *Squid-Box.SevenZipSharp* library. - ([#57](https://github.com/thoemmi/7Zip4Powershell/pull/57), contributed by [@kborowinski](https://github.com/kborowinski)) + ([#57](https://github.com/thoemmi/7Zip4Powershell/pull/57), contributed by [@kborowinski](https://github.com/kborowinski)) +* Adds new a parameter `OutputPath` for `Compress-7Zip` + ([#60](https://github.com/thoemmi/7Zip4Powershell/pull/60), contributed by [@iRebbok](https://github.com/iRebbok)) ### [v1.10](https://github.com/thoemmi/7Zip4Powershell/releases/tag/v1.10)