public static async Task <IEnumerable <string> > ExtractPackageAsync( Stream packageStream, PackageIdentity packageIdentity, PackagePathResolver packagePathResolver, PackageExtractionContext packageExtractionContext, PackageSaveModes packageSaveMode, CancellationToken token) { var filesAdded = new List <string>(); if (packageStream == null) { throw new ArgumentNullException(nameof(packageStream)); } if (!packageStream.CanSeek) { throw new ArgumentException(Strings.PackageStreamShouldBeSeekable); } if (packageIdentity == null) { throw new ArgumentNullException(nameof(packageIdentity)); } if (packagePathResolver == null) { throw new ArgumentNullException(nameof(packagePathResolver)); } // TODO: Need to handle PackageSaveMode // TODO: Support overwriting files also? var zipArchive = new ZipArchive(packageStream); var packageReader = new PackageReader(zipArchive); var nuspecReader = new NuspecReader(packageReader.GetNuspec()); packageIdentity = nuspecReader.GetIdentity(); var packageDirectoryInfo = Directory.CreateDirectory(packagePathResolver.GetInstallPath(packageIdentity)); var packageDirectory = packageDirectoryInfo.FullName; filesAdded.AddRange(await PackageHelper.CreatePackageFiles(zipArchive.Entries, packageDirectory, packageSaveMode, token)); var nupkgFilePath = Path.Combine(packageDirectory, packagePathResolver.GetPackageFileName(packageIdentity)); if (packageSaveMode.HasFlag(PackageSaveModes.Nupkg)) { // During package extraction, nupkg is the last file to be created // Since all the packages are already created, the package stream is likely positioned at its end // Reset it packageStream.Seek(0, SeekOrigin.Begin); filesAdded.Add(await PackageHelper.CreatePackageFile(nupkgFilePath, packageStream, token)); } // Now, copy satellite files unless requested to not copy them if (packageExtractionContext == null || packageExtractionContext.CopySatelliteFiles) { filesAdded.AddRange(await CopySatelliteFilesAsync(packageIdentity, packagePathResolver, packageSaveMode, token)); } return(filesAdded); }
public static async Task <IEnumerable <string> > ExtractPackageAsync( PackageReaderBase packageReader, Stream packageStream, PackageIdentity packageIdentity, PackagePathResolver packagePathResolver, PackageExtractionContext packageExtractionContext, PackageSaveModes packageSaveMode, CancellationToken token) { if (packageStream == null) { throw new ArgumentNullException(nameof(packageStream)); } if (packageIdentity == null) { throw new ArgumentNullException(nameof(packageIdentity)); } if (packagePathResolver == null) { throw new ArgumentNullException(nameof(packagePathResolver)); } // TODO: Need to handle PackageSaveMode // TODO: Support overwriting files also? var filesAdded = new List <string>(); var nuspecReader = new NuspecReader(packageReader.GetNuspec()); packageIdentity = nuspecReader.GetIdentity(); var packageDirectoryInfo = Directory.CreateDirectory(packagePathResolver.GetInstallPath(packageIdentity)); var packageDirectory = packageDirectoryInfo.FullName; foreach (var file in packageReader.GetFiles().Where(file => PackageHelper.IsPackageFile(file, packageSaveMode))) { token.ThrowIfCancellationRequested(); var targetPath = Path.Combine(packageDirectory, file); Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); using (var sourceStream = packageReader.GetStream(file)) { using (var targetStream = new FileStream(targetPath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 1024, useAsync: true)) { await sourceStream.CopyToAsync(targetStream); } } filesAdded.Add(file); } var nupkgFilePath = Path.Combine(packageDirectory, packagePathResolver.GetPackageFileName(packageIdentity)); if (packageSaveMode.HasFlag(PackageSaveModes.Nupkg)) { // During package extraction, nupkg is the last file to be created // Since all the packages are already created, the package stream is likely positioned at its end // Reset it if (packageStream.Position != 0) { if (!packageStream.CanSeek) { throw new ArgumentException(Strings.PackageStreamShouldBeSeekable); } packageStream.Position = 0; } filesAdded.Add(await PackageHelper.CreatePackageFile(nupkgFilePath, packageStream, token)); } // Now, copy satellite files unless requested to not copy them if (packageExtractionContext == null || packageExtractionContext.CopySatelliteFiles) { PackageIdentity runtimeIdentity; string packageLanguage; var isSatellitePackage = PackageHelper.IsSatellitePackage(nuspecReader, out runtimeIdentity, out packageLanguage); // Short-circuit this if the package is not a satellite package. if (isSatellitePackage) { filesAdded.AddRange(await CopySatelliteFilesAsync(packageIdentity, packagePathResolver, packageSaveMode, token)); } } return(filesAdded); }
public static async Task <IEnumerable <string> > ExtractPackageAsync( string source, Stream packageStream, PackagePathResolver packagePathResolver, PackageExtractionContext packageExtractionContext, CancellationToken token, Guid parentId = default(Guid)) { if (packageStream == null) { throw new ArgumentNullException(nameof(packageStream)); } if (!packageStream.CanSeek) { throw new ArgumentException(Strings.PackageStreamShouldBeSeekable); } if (packagePathResolver == null) { throw new ArgumentNullException(nameof(packagePathResolver)); } if (packageExtractionContext == null) { throw new ArgumentNullException(nameof(packageExtractionContext)); } var packageSaveMode = packageExtractionContext.PackageSaveMode; var filesAdded = new List <string>(); var nupkgStartPosition = packageStream.Position; using (var telemetry = TelemetryActivity.CreateTelemetryActivityWithNewOperationId(parentId)) { using (var packageReader = new PackageArchiveReader(packageStream, leaveStreamOpen: true)) { var packageIdentityFromNuspec = await packageReader.GetIdentityAsync(CancellationToken.None); var installPath = packagePathResolver.GetInstallPath(packageIdentityFromNuspec); var packageDirectoryInfo = Directory.CreateDirectory(installPath); var packageDirectory = packageDirectoryInfo.FullName; try { telemetry.StartIntervalMeasure(); await VerifyPackageSignatureAsync( source, telemetry.OperationId, packageIdentityFromNuspec, packageExtractionContext, packageReader, token); telemetry.EndIntervalMeasure(PackagingConstants.PackageVerifyDurationName); } catch (SignatureException) { telemetry.TelemetryEvent = new PackageExtractionTelemetryEvent( packageExtractionContext.PackageSaveMode, NuGetOperationStatus.Failed, ExtractionSource.NuGetFolderProject, packageIdentityFromNuspec); throw; } var packageFiles = await packageReader.GetPackageFilesAsync(packageSaveMode, token); if ((packageSaveMode & PackageSaveMode.Nuspec) == PackageSaveMode.Nuspec) { var sourceNuspecFile = packageFiles.Single(p => PackageHelper.IsManifest(p)); var targetNuspecPath = Path.Combine( packageDirectory, packagePathResolver.GetManifestFileName(packageIdentityFromNuspec)); // Extract the .nuspec file with a well known file name. filesAdded.Add(packageReader.ExtractFile( sourceNuspecFile, targetNuspecPath, packageExtractionContext.Logger)); packageFiles = packageFiles.Except(new[] { sourceNuspecFile }); } var packageFileExtractor = new PackageFileExtractor(packageFiles, packageExtractionContext.XmlDocFileSaveMode); filesAdded.AddRange(await packageReader.CopyFilesAsync( packageDirectory, packageFiles, packageFileExtractor.ExtractPackageFile, packageExtractionContext.Logger, token)); if ((packageSaveMode & PackageSaveMode.Nupkg) == PackageSaveMode.Nupkg) { // During package extraction, nupkg is the last file to be created // Since all the packages are already created, the package stream is likely positioned at its end // Reset it to the nupkgStartPosition packageStream.Seek(nupkgStartPosition, SeekOrigin.Begin); var nupkgFilePath = Path.Combine( packageDirectory, packagePathResolver.GetPackageFileName(packageIdentityFromNuspec)); filesAdded.Add(packageStream.CopyToFile(nupkgFilePath)); } // Now, copy satellite files unless requested to not copy them if (packageExtractionContext.CopySatelliteFiles) { filesAdded.AddRange(await CopySatelliteFilesAsync( packageReader, packagePathResolver, packageSaveMode, packageExtractionContext, token)); } telemetry.TelemetryEvent = new PackageExtractionTelemetryEvent( packageExtractionContext.PackageSaveMode, NuGetOperationStatus.Succeeded, ExtractionSource.NuGetFolderProject, packageIdentityFromNuspec); } return(filesAdded); } }