Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -65,6 +66,9 @@
<Compile Include="Commands\GetChocolateyPathCommand.cs" />
<Compile Include="Commands\AssertValidChecksumCommand.cs" />
<Compile Include="Helpers\ConfigHelper.cs" />
<Compile Include="Commands\ExpandChocolateyArchiveCommand.cs" />
<Compile Include="Shared\SevenZipException.cs" />
<Compile Include="Helpers\ExtractArchiveHelper.cs" />
<Compile Include="Shared\ChecksumExeNotFoundException.cs" />
<Compile Include="Shared\ChecksumVerificationFailedException.cs" />
<Compile Include="Shared\ChecksumMissingException.cs" />
Expand All @@ -75,6 +79,7 @@
<Compile Include="Commands\TestProcessAdminRightsCommand.cs" />
<Compile Include="Commands\UninstallChocolateyPathCommand.cs" />
<Compile Include="Commands\UpdateSessionEnvironmentCommand.cs" />
<Compile Include="Helpers\ArchitectureWidth.cs" />
<Compile Include="Helpers\ChecksumValidator.cs" />
<Compile Include="Helpers\Elevation.cs" />
<Compile Include="Helpers\EnvironmentHelper.cs" />
Expand All @@ -90,6 +95,7 @@
<Compile Include="Shared\ChocolateyPathType.cs" />
<Compile Include="Shared\EnvironmentNames.cs" />
<Compile Include="Shared\EnvironmentVariables.cs" />
<Compile Include="Shared\ProcessHandler.cs" />
<Compile Include="Win32\NativeMethods.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Chocolatey.PowerShell.Helpers;
using Chocolatey.PowerShell.Shared;
using System;
using System.IO;
using System.Management.Automation;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static chocolatey.StringResources.EnvironmentVariables;

namespace Chocolatey.PowerShell.Commands
{
[Cmdlet(VerbsData.Expand, "ChocolateyArchive", DefaultParameterSetName = "Path", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType(typeof(string))]
public class ExpandChocolateyArchiveCommand : ChocolateyCmdlet
{
[Alias("File", "FileFullPath")]
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "Path")]
[Parameter(Mandatory = true, ParameterSetName = "BothPaths")]
public string Path { get; set; } = string.Empty;

[Alias("UnzipLocation")]
[Parameter(Mandatory = true, Position = 1)]
public string Destination { get; set; } = string.Empty;

[Parameter(Position = 2)]
[Alias("SpecificFolder")]
public string FilesToExtract { get; set; }

[Parameter(Position = 3)]
public string PackageName { get; set; }

[Alias("File64", "FileFullPath64")]
[Parameter(Mandatory = true, ParameterSetName = "Path64")]
[Parameter(Mandatory = true, ParameterSetName = "BothPaths")]
public string Path64 { get; set; }

[Parameter]
public SwitchParameter DisableLogging { get; set; }

[Parameter]
public SwitchParameter UseBuiltinCompression { get; set; } = EnvironmentHelper.GetVariable(Package.ChocolateyUseBuiltinCompression) == "true";

protected override void End()
{
var helper = new ExtractArchiveHelper(this, PipelineStopToken);
try
{
helper.ExtractFiles(Path, Path64, PackageName, Destination, FilesToExtract, UseBuiltinCompression, DisableLogging);

WriteObject(Destination);
}
catch (FileNotFoundException error)
{
ThrowTerminatingError(new ErrorRecord(
error,
$"{ErrorId}.FileNotFound",
ErrorCategory.ObjectNotFound,
string.IsNullOrEmpty(Path) ? Path64 : Path));
}
catch (InvalidOperationException error)
{
ThrowTerminatingError(new ErrorRecord(
error,
$"{ErrorId}.ApplicationMissing",
ErrorCategory.InvalidOperation,
targetObject: null));
}
catch (NotSupportedException error)
{
ThrowTerminatingError(new ErrorRecord(
error,
$"{ErrorId}.UnsupportedArchitecture",
ErrorCategory.NotImplemented,
string.IsNullOrEmpty(Path) ? Path64 : Path));
}
catch (SevenZipException error)
{
ThrowTerminatingError(new ErrorRecord(
error,
$"{ErrorId}.ExtractionFailed",
ErrorCategory.InvalidResult,
string.IsNullOrEmpty(Path) ? Path64 : Path));
}
catch (Exception error)
{
ThrowTerminatingError(new ErrorRecord(
error,
$"{ErrorId}.Unknown",
ErrorCategory.NotSpecified,
string.IsNullOrEmpty(Path) ? Path64 : Path));
}
}
}
}
31 changes: 31 additions & 0 deletions src/Chocolatey.PowerShell/Helpers/ArchitectureWidth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Chocolatey.PowerShell.Helpers
{
/// <summary>
/// Provides information on the current running architecture width of the process.
/// </summary>
internal static class ArchitectureWidth
{
/// <summary>
/// Returns either 64 or 32, depending on whether the current process environment is 64-bit.
/// </summary>
/// <returns>The current architecture width as an integer.</returns>
internal static int Get()
{
return Environment.Is64BitProcess ? 64 : 32;
}

/// <summary>
/// Compares the current architecture to the expected value.
/// </summary>
/// <param name="compareTo">The architecture width to compare to.</param>
/// <returns>True if the provided value matches the current architecture width, otherwise false.</returns>
internal static bool Matches(int compareTo)
{
return Get() == compareTo;
}
}
}
Loading