Skip to content

Commit a075f76

Browse files
authored
Merge pull request #31698 from bdach/weird-zips
Work around rare sharpcompress failure to extract certain archives
2 parents 129ab03 + bb8f58f commit a075f76

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

osu.Game/IO/Archives/ZipArchiveReader.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Linq;
1010
using System.Text;
1111
using Microsoft.Toolkit.HighPerformance;
12+
using osu.Framework.Extensions;
1213
using osu.Framework.IO.Stores;
1314
using SharpCompress.Archives.Zip;
1415
using SharpCompress.Common;
@@ -54,12 +55,22 @@ public override Stream GetStream(string name)
5455
if (entry == null)
5556
return null;
5657

57-
var owner = MemoryAllocator.Default.Allocate<byte>((int)entry.Size);
58-
5958
using (Stream s = entry.OpenEntryStream())
60-
s.ReadExactly(owner.Memory.Span);
61-
62-
return new MemoryOwnerMemoryStream(owner);
59+
{
60+
if (entry.Size > 0)
61+
{
62+
var owner = MemoryAllocator.Default.Allocate<byte>((int)entry.Size);
63+
s.ReadExactly(owner.Memory.Span);
64+
return new MemoryOwnerMemoryStream(owner);
65+
}
66+
67+
// due to a sharpcompress bug (https://github.com/adamhathcock/sharpcompress/issues/88),
68+
// in rare instances the `ZipArchiveEntry` will not contain a correct `Size` but instead report 0.
69+
// this would lead to the block above reading nothing, and the game basically seeing an archive full of empty files.
70+
// since the bug is years old now, and this is a rather rare situation anyways (reported once in years),
71+
// work around this locally by falling back to reading as many bytes as possible and using a standard non-pooled memory stream.
72+
return new MemoryStream(s.ReadAllRemainingBytesToArray());
73+
}
6374
}
6475

6576
public override void Dispose()

0 commit comments

Comments
 (0)