Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Added new a parameter OutputPath for Compress-7Zip #60

Merged
merged 11 commits into from
Jun 6, 2020
8 changes: 6 additions & 2 deletions 7Zip4Powershell/7Zip4Powershell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@
<ItemGroup>
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll</HintPath>
<HintPath>..\Libs\System.Management.Automation.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="GitVersionTask">
<Version>5.2.4</Version>
<Version>5.3.4</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="SevenZipSharp.Net45" Version="1.0.19" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Squid-Box.SevenZipSharp" Version="1.2.265" />
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="SevenZipSharp.Net45" Version="1.0.19" />
<PackageReference Include="System.Management" Version="4.7.0" />
</ItemGroup>
Expand Down
34 changes: 29 additions & 5 deletions 7Zip4Powershell/Compress7Zip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public class Compress7Zip : ThreadedCmdlet {
public string Path { get; set; }

[Parameter(Position = 2, Mandatory = false, HelpMessage = "The filter to be applied if Path points to a directory")]
public string Filter { get; set; }
public string Filter { get; set; } = "*";

[Parameter(HelpMessage = "Output path for a compressed archive")]
public string OutputPath { get; set; }

private List<string> _directoryOrFilesFromPipeline;

Expand Down Expand Up @@ -64,6 +67,9 @@ public class Compress7Zip : ThreadedCmdlet {
[Parameter(HelpMessage = "Disables preservation of empty directories")]
public SwitchParameter SkipEmptyDirectories { get; set; }

[Parameter(HelpMessage = "Preserves directory root")]
public SwitchParameter PreserveDirectoryRoot { get; set; }

[Parameter(HelpMessage = "Disables recursive files search")]
public SwitchParameter DisableRecursion { get; set; }

Expand Down Expand Up @@ -168,6 +174,7 @@ public override void Execute() {
EncryptHeaders = _cmdlet.EncryptFilenames.IsPresent,
DirectoryStructure = !_cmdlet.FlattenDirectoryStructure.IsPresent,
IncludeEmptyDirectories = !_cmdlet.SkipEmptyDirectories.IsPresent,
PreserveDirectoryRoot = _cmdlet.PreserveDirectoryRoot.IsPresent,
CompressionMode = _cmdlet.Append.IsPresent ? CompressionMode.Append : CompressionMode.Create
};

Expand All @@ -179,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}"
Expand Down Expand Up @@ -217,13 +241,13 @@ public override void Execute() {
if (HasPassword) {
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName, _cmdlet._password, _cmdlet.Filter, recursion);
} else {
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName, _cmdlet.Filter, recursion);
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName, null, _cmdlet.Filter, recursion);
}
} else {
if (HasPassword) {
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName, recursion, _cmdlet._password);
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName, _cmdlet._password, null, recursion);
} else {
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName, recursion);
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName, null, null, recursion);
}
}
}
Expand Down
Binary file added Libs/System.Management.Automation.dll
Binary file not shown.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Compress-7Zip
[-SkipEmptyDirectories]
[-DisableRecursion]
[-Append]
[-PreserveDirectoryRoot]
[<CommonParameters>]

Get-7Zip
Expand Down Expand Up @@ -84,6 +85,13 @@ A list of all custom parameters can be found [here](https://sevenzip.osdn.jp/chm

## Changelog

### vNext

* Replaces *SevenZipSharp.Net45* with *Squid-Box.SevenZipSharp* library.
([#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)

* Updated 7-Zip dlls to 19.00 ([#56](https://github.com/thoemmi/7Zip4Powershell/pull/56), contributed by [@kborowinski](https://github.com/kborowinski))
Expand Down