ChatGPT解决这个技术问题 Extra ChatGPT

Extracting Nupkg files using command line

Firstly, I do not want to use Visual Studio at all when dealing with the certain .nupkg files.

I know there is a tool called NuGet Package Explorer and this can export nupkg files to a certain file location using a gui, but I'm looking to setup a MSBuild task to run and unpack about 50 .nupkg files, using the command line.

My question is, is there a tool you can use via the command line which will unpack .nupkg files to a specified file location?


C
Calvin Allen

NuPKG files are just zip files, so anything that can process a zip file should be able to process a nupkg file, i.e, 7zip.


There is a proviso with this, which is that NuGet seems to do some sort of encoding of filenames. So, if you use a zip tool, a file you orginally named "A+B.txt" for example will be extracted as "A%2B.txt". This is avoided by using nuget install (as per Andy's answer)
the file encoding issue was fixed in 4.7.0+
github.com/NuGet/Home/issues/9459 Links to some side effects of not encoding the filenames, but also links in the comments to the original issues where it was changed, for reference. Note: The are compatibility issues with Nuget 2.x
K
Keith Pinson

You can also use the NuGet command line, by specifying a local host as part of an install. For example if your package is stored in the current directory

nuget install MyPackage -Source %cd% -OutputDirectory packages

will unpack it into the target directory.


Just a note that MyPackage is actually a Package ID that is specified in the .nuspec file and not a file name.
and you have to execute it from console with admin privileges
Fwiw, in powershell the command looks like: nuget install MyPackage -Source $pwd -OutputDirectory packages
I guess %cd% is referring to the current directory. How can I refer to different path in my local system? it does not work when I use a path. "./example/path/"
I used the absolute path to the file directory to get the -Source to work for me. I didn't include the package name.
S
Shadi Alnamrouti

Rename it to .zip, then extract it.


This is an extremely old question, and if you just change it to a zip that will not answer the question, if you use a zip tool, a file you originally named "A+B.txt" for example will be extracted as "A%2B.txt" - You also don't need to rename it to zip.
U
UsmanShabbir

did the same thing like this:

clear
cd PACKAGE_DIRECTORY

function Expand-ZIPFile($file, $destination)
{
    $shell = New-Object -ComObject Shell.Application
    $zip = $shell.NameSpace($file)
    foreach($item in $zip.items())
    {
        $shell.Namespace($destination).copyhere($item)
    }
}

Dir *.nupkg | rename-item -newname {  $_.name  -replace ".nupkg",".zip"  }

Expand-ZIPFile "Package.1.0.0.zip" “DESTINATION_PATH”

Dosent this suffer from the same problem as Oli Wennell mention above? i.e. "A+B.txt" for example will be extracted as "A%2B.txt"?
M
Mariusz

With PowerShell 5.1 (PackageManagement module)

Install-Package -Name MyPackage -Source (Get-Location).Path -Destination C:\outputdirectory

L
Lumito

This worked for me:

Rename-Item -Path A_Package.nupkg -NewName A_Package.zip

Expand-Archive -Path A_Package.zip -DestinationPath C:\Reference

At least in PS 7.1.2 you can can use Expand-Archive without changing the file extension on MacOS.
@Phatmandrake +1 you are correct and this should be an answer, I just verified myself on Big Sur + PS 7.1.3
D
David Morrow

I've expanded Zamarin.Essentials -version 1.6.1 with 7-zip and nuget package manager is not recognizing this package and I have source set to all. I've tried just my global package source alone too.

Also I've noticed package manager downloads multiple versions to same folder, was wondering if it's ok to put a version folder in a package and copy the package end into it?