Change approach to only extract files that match criteria

This commit is contained in:
Drew Scoggins 2023-01-26 16:04:14 -08:00
parent 0f4680522b
commit c1a4771218
2 changed files with 40 additions and 5 deletions

View file

@ -32,6 +32,11 @@ namespace Microsoft.DotNet.Build.Tasks
/// </summary>
public bool CleanDestination { get; set; }
/// <summary>
/// A list of directories, semicolon deliminated, relative to the root of the archive to include. If empty all directories will be copied.
/// </summary>
public string DirectoriesToCopy { get; set; }
protected override bool ValidateParameters()
{
base.ValidateParameters();
@ -73,13 +78,41 @@ namespace Microsoft.DotNet.Build.Tasks
{
if (ValidateParameters())
{
if (DirectoriesToCopy != null && DirectoriesToCopy.Length != 0)
{
var zip = new ZipArchive(File.OpenRead(SourceArchive));
string loc = DestinationDirectory;
foreach (var entry in zip.Entries)
{
foreach (var directory in DirectoriesToCopy.Split(';'))
{
if (entry.FullName.Contains(directory))
{
string dirBuilder = loc;
foreach (var pathPart in Path.GetDirectoryName(entry.FullName).Split('/'))
{
if (!Directory.Exists(Path.Combine(dirBuilder, pathPart)))
{
Directory.CreateDirectory(Path.Combine(dirBuilder, pathPart));
}
dirBuilder = Path.Combine(dirBuilder, pathPart);
}
Log.LogMessage(Path.GetDirectoryName(entry.FullName));
entry.ExtractToFile(Path.Combine(loc, entry.FullName));
}
}
}
}
else
{
#if NETFRAMEWORK
// .NET Framework doesn't have overload to overwrite files
ZipFile.ExtractToDirectory(SourceArchive, DestinationDirectory);
// .NET Framework doesn't have overload to overwrite files
ZipFile.ExtractToDirectory(SourceArchive, DestinationDirectory);
#else
ZipFile.ExtractToDirectory(SourceArchive, DestinationDirectory, overwriteFiles: true);
#endif
ZipFile.ExtractToDirectory(SourceArchive, DestinationDirectory, overwriteFiles: true);
#endif
}
}
else
{

View file

@ -267,6 +267,7 @@
<BaseUrl Condition=" '$(DotNetBuildFromSource)' == 'true' AND '$(DotNetBuildFromSourceFlavor)' == 'Product' ">$(AspNetCoreSharedFxRootUrl)</BaseUrl>
<DownloadFileName>$(AspNetCoreSharedFxArchiveFileName)</DownloadFileName>
<RelativeLayoutPath></RelativeLayoutPath>
<DirectoriesToCopy>shared/Microsoft.AspNetCore.App</DirectoriesToCopy>
</BundledLayoutComponent>
<!-- The AspNet does not provide native MacOS PKG installers at this time; we use AspNet archives.
@ -449,7 +450,8 @@
<Target Name="LayoutBundledComponents">
<ExtractArchiveToDirectory SourceArchive="%(BundledLayoutComponent.DownloadDestination)"
DestinationDirectory="$(RedistLayoutPath)/%(BundledLayoutComponent.RelativeLayoutPath)" />
DestinationDirectory="$(RedistLayoutPath)/%(BundledLayoutComponent.RelativeLayoutPath)"
DirectoriesToCopy="%(BundledLayoutComponent.DirectoriesToCopy)"/>
<Copy SourceFiles="@(BundledLayoutPackageDownloadFilesWithDestination)"
DestinationFiles="@(BundledLayoutPackageDownloadFilesWithDestination->'%(DestinationPath)')"