7

could you please help me? How I can open and extract files the "nupkg" package using the PowerShell. Thanks.

3 Answers 3

10

You can use Expand-Archive (you have to rename the file, see Can I use PowerShell `Expand-Archive` upon a zip file with no extension)

Rename-Item "Newtonsoft.Json.12.0.1.nupkg" "Newtonsoft.Json.12.0.1.nupkg.zip" Expand-Archive "Newtonsoft.Json.12.0.1.nupkg.zip" 
Sign up to request clarification or add additional context in comments.

2 Comments

Package files are no different than a zip file, you can simply pass that file as-is and it will work the same. Expand-Archive -Path Newtonsoft.json.12.0.1.nupkg -Destination .
You may expand .nupkg packages like this with PowerShell Core ( > 5.1), but if you are using Windows PowerShell ( <= 5.1) you still need to rename the .nupkg to .zip before you expand or you will receive an error ".nupkg is not a supported archive file format. .zip is the only supported archive file format"
4

I prefer to use nuget cli, because it also intstalls dependencies. All you need is nuget install yourpackage . It's really just a 5MB executable, you can even download it each time you need to get the package:

$nugetUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" Invoke-WebRequest -Uri $nugetUrl -OutFile ".\nuget.exe" .\nuget.exe install yourpackage 

Comments

0

I would not recommend direct use of Expand-Archive. As mentioned by guys at Squirrel and as you can see in NuGet sources file names in archive are escaped using URI escaping.

If you decide to expand raw archive you should afterwards rename all files and directories containing % in their names using Uri.UnescapeDataString.

If you want more optimised approach in terms of file system writes here's the implementation:

function Expand-NugetArchive { [CmdletBinding()] param ( # File name of the Package [Parameter(Mandatory = $true)] [string] $FileName, # Directory [string] $ExtractDirectory, [boolean] $Overwrite = $false ) # Reference to the knowledge here https://stackoverflow.com/a/72590215/3299257 $extractPath = [System.IO.Path]::GetFullPath($ExtractDirectory); # Ensures that the last character on the extraction path # is the directory separator char. # Without this, a malicious zip file could try to traverse outside of the expected # extraction path. if ( -not $extractPath.EndsWith([System.IO.Path]::DirectorySeparatorChar.ToString(), [StringComparison]::Ordinal)) { $extractPath += [System.IO.Path]::DirectorySeparatorChar; } $archive = [System.IO.Compression.ZipFile]::OpenRead($FileName) try { foreach ($entry in $archive.Entries) { $fullName = $entry.FullName if ($fullName.Contains('%')) { $fullName = [Uri]::UnescapeDataString($fullName) } # Gets the full path to ensure that relative segments are removed. $destinationPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($extractPath, $fullName)) [System.IO.Directory]::CreateDirectory([System.IO.Path]::GetDirectoryName($destinationPath)) | Out-Null # Ordinal match is safest, case-sensitive volumes can be mounted within volumes that # are case-insensitive. if ($destinationPath.StartsWith($extractPath, [StringComparison]::Ordinal)) { [System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $destinationPath, $Overwrite) } } } catch { if ($null -ne $archive) { $archive.Dispose() } throw } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.