how to get package size with nuget.core?

前端 未结 1 1097
无人共我
无人共我 2021-01-22 01:44

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

相关标签:
1条回答
  • 2021-01-22 02:23

    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.

    0 讨论(0)
提交回复
热议问题