'how to get package size with nuget.core?
i am wondering if there is a way to know the size of a package before downloading. I'm using nuget packages for deploying components, and knowing the size of an update would be a nice feature.
Solution 1:[1]
The package size is returned in the response back from the NuGet gallery package source:
<d:PackageSize m:type="Edm.Int64">223865</d:PackageSize>
Unfortunately this PackageSize is not available from the Package object you get back from NuGet.Core.dll.
To get the PackageSize one way is to take the DataServicePackageRepository class and the DataServicePackage class from NuGet's source code and modify them so the PackageSize property is available on the DataServicePackage object.
public long PackageSize { get; set; }
I took these two classes and renamed them to be CustomDataServicePackageRepository and CustomDataServicePackage.
You can include these two classes in your application and use them by casting the IPackage returned to your custom DataServicePackage class and then access the PackageSize property.
var repo = new CustomDataServicePackageRepository(new Uri("https://www.nuget.org/api/v2/"));
var packages = repo.Search(null, false)
.Where(package => package.IsLatestVersion)
.OrderByDescending(package => package.DownloadCount)
.AsBufferedEnumerable(30)
.Where(package => package.IsReleaseVersion())
.Take(10);
foreach (IPackage package in packages) {
var dataServicePackage = package as CustomDataServicePackage;
long size = dataServicePackage == null ? -1 : dataServicePackage.PackageSize;
Console.WriteLine("Package: {0}, Size: {1}", package.ToString(), size);
}
NuGet itself does not use the PackageSize. Instead it starts the download of the NuGet package and looks at the ContentLength returned in the HTTP response to find the size of the NuGet package. This is then used to show the percentage progress in Visual Studio.
Solution 2:[2]
Using NuGet Package Explorer, it is possible to browse the tree and get the DLL size.
Example with FFMpegCore 4.8.0:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | pjpscriv |
Solution 2 | Amessihel |