Skip to content

Commit e10c683

Browse files
karakasacarlossanlop
authored andcommitted
improved large zip test
1 parent 4b92da1 commit e10c683

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

src/libraries/System.IO.Compression/tests/ZipArchive/zip_LargeFiles.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,23 @@ public static void UnzipOver4GBZipFile()
4646
}
4747
}
4848

49-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized), nameof(PlatformDetection.Is64BitProcess))] // don't run it on slower runtimes
50-
[OuterLoop("It requires 5 GB of free disk space")]
51-
public static void CheckZIP64VersionIsSet_ForSmallFilesAfterBigFiles()
49+
private static void FillWithHardToCompressData(byte[] buffer)
50+
{
51+
Random.Shared.NextBytes(buffer);
52+
}
53+
54+
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized), nameof(PlatformDetection.Is64BitProcess))] // don't run it on slower runtimes
55+
[OuterLoop("It requires 5~6 GB of free disk space and a lot of CPU time for compressed tests")]
56+
[InlineData(false)]
57+
[InlineData(true)]
58+
public static void CheckZIP64VersionIsSet_ForSmallFilesAfterBigFiles(bool isCompressed)
5259
{
5360
// issue #94899
5461

55-
byte[] largeBuffer = GC.AllocateUninitializedArray<byte>(1_000_000_000); // 1 GB
5662
byte[] smallBuffer = GC.AllocateUninitializedArray<byte>(1000);
63+
byte[] largeBuffer = GC.AllocateUninitializedArray<byte>(1_000_000_000); // ~1 GB
5764

5865
string zipArchivePath = Path.Combine(Path.GetTempPath(), "over4GB.zip");
59-
DirectoryInfo tempDir = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "over4GB"));
6066

6167
try
6268
{
@@ -69,21 +75,29 @@ public static void CheckZIP64VersionIsSet_ForSmallFilesAfterBigFiles()
6975
{
7076
// Create
7177

78+
var compressLevel = isCompressed ? CompressionLevel.Optimal : CompressionLevel.NoCompression;
79+
7280
using var archive = new ZipArchive(fs, ZipArchiveMode.Create, true);
73-
ZipArchiveEntry file = archive.CreateEntry(LargeFileName, CompressionLevel.NoCompression);
81+
ZipArchiveEntry file = archive.CreateEntry(LargeFileName, compressLevel);
7482

7583
using (Stream stream = file.Open())
7684
{
7785
// Write 5GB of data
7886

79-
stream.Write(largeBuffer);
80-
stream.Write(largeBuffer);
81-
stream.Write(largeBuffer);
82-
stream.Write(largeBuffer);
83-
stream.Write(largeBuffer);
87+
const int HOW_MANY_GB_TO_WRITE = 5;
88+
89+
for (var i = 0; i < HOW_MANY_GB_TO_WRITE; i++)
90+
{
91+
if (isCompressed)
92+
{
93+
FillWithHardToCompressData(largeBuffer);
94+
}
95+
96+
stream.Write(largeBuffer);
97+
}
8498
}
8599

86-
file = archive.CreateEntry(SmallFileName, CompressionLevel.NoCompression);
100+
file = archive.CreateEntry(SmallFileName, compressLevel);
87101

88102
using (Stream stream = file.Open())
89103
{
@@ -110,15 +124,13 @@ public static void CheckZIP64VersionIsSet_ForSmallFilesAfterBigFiles()
110124
fs.Position = (long)offsetOfLHField.GetValue(entry) + ZipLocalFileHeader_OffsetToVersionFromHeaderStart;
111125
ushort versionNeeded = reader.ReadUInt16();
112126

113-
Assert.True(versionNeeded >= Zip64Version, "ZIP64 version is not set for files with LH at >4GB offset.");
127+
Assert.True(versionNeeded == Zip64Version, "Version is not ZIP64 for files with Local Header at >4GB offset.");
114128
}
115129
}
116130
}
117131
finally
118132
{
119133
File.Delete(zipArchivePath);
120-
121-
tempDir.Delete(recursive: true);
122134
}
123135
}
124136
}

0 commit comments

Comments
 (0)