Skip to content

Commit

Permalink
ZipArchive: Improve performance of removing extra fields (#108665)
Browse files Browse the repository at this point in the history
This improves the performance of removing extra fields in the ZipArchive by optimizing the removal process.

`src/libraries/System.IO.Compression/src/System/IO/Compression/ZipBlocks.cs`: Replaced multiple iterations and list operations with a single `RemoveAll` call to enhance efficiency.
  • Loading branch information
ericstj authored Oct 8, 2024
1 parent afad60c commit 10760a2
Showing 1 changed file with 5 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,12 @@ public static Zip64ExtraField GetAndRemoveZip64Block(List<ZipGenericExtraField>
zip64Field._localHeaderOffset = null;
zip64Field._startDiskNumber = null;

List<ZipGenericExtraField> markedForDelete = new List<ZipGenericExtraField>();
bool zip64FieldFound = false;

foreach (ZipGenericExtraField ef in extraFields)
extraFields.RemoveAll(ef =>
{
if (ef.Tag == TagConstant)
{
markedForDelete.Add(ef);
if (!zip64FieldFound)
{
if (TryGetZip64BlockFromGenericExtraField(ef, readUncompressedSize, readCompressedSize,
Expand All @@ -285,24 +283,18 @@ public static Zip64ExtraField GetAndRemoveZip64Block(List<ZipGenericExtraField>
zip64FieldFound = true;
}
}
return true;
}
}

foreach (ZipGenericExtraField ef in markedForDelete)
extraFields.Remove(ef);
return false;
});

return zip64Field;
}

public static void RemoveZip64Blocks(List<ZipGenericExtraField> extraFields)
{
List<ZipGenericExtraField> markedForDelete = new List<ZipGenericExtraField>();
foreach (ZipGenericExtraField field in extraFields)
if (field.Tag == TagConstant)
markedForDelete.Add(field);

foreach (ZipGenericExtraField field in markedForDelete)
extraFields.Remove(field);
extraFields.RemoveAll(field => field.Tag == TagConstant);
}

public void WriteBlock(Stream stream)
Expand Down

0 comments on commit 10760a2

Please sign in to comment.