1
1
using System ;
2
2
using System . IO ;
3
+ using System . Net . Http ;
4
+ using System . Threading . Tasks ;
3
5
using BenchmarkDotNet . Attributes ;
6
+ using ICSharpCode . SharpZipLib . Zip ;
4
7
5
8
namespace ICSharpCode . SharpZipLib . Benchmark . Zip
6
9
{
@@ -14,14 +17,16 @@ public class ZipInputStream
14
17
15
18
byte [ ] zippedData ;
16
19
byte [ ] readBuffer = new byte [ 4096 ] ;
20
+ private string zipFileWithLargeAmountOfEntriesPath ;
17
21
18
- public ZipInputStream ( )
22
+ [ GlobalSetup ]
23
+ public async Task GlobalSetup ( )
19
24
{
20
25
using ( var memoryStream = new MemoryStream ( ) )
21
26
{
22
27
using ( var zipOutputStream = new SharpZipLib . Zip . ZipOutputStream ( memoryStream ) )
23
28
{
24
- zipOutputStream . PutNextEntry ( new SharpZipLib . Zip . ZipEntry ( "0" ) ) ;
29
+ zipOutputStream . PutNextEntry ( new ZipEntry ( "0" ) ) ;
25
30
26
31
var inputBuffer = new byte [ ChunkSize ] ;
27
32
@@ -33,6 +38,29 @@ public ZipInputStream()
33
38
34
39
zippedData = memoryStream . ToArray ( ) ;
35
40
}
41
+
42
+ // large real-world test file from test262 repository
43
+ string commitSha = "2e4e0e6b8ebe3348a207144204cb6d7a5571c863" ;
44
+ zipFileWithLargeAmountOfEntriesPath = Path . Combine ( Path . GetTempPath ( ) , $ "{ commitSha } .zip") ;
45
+ if ( ! File . Exists ( zipFileWithLargeAmountOfEntriesPath ) )
46
+ {
47
+ var uri = $ "https://github.com/tc39/test262/archive/{ commitSha } .zip";
48
+
49
+ Console . WriteLine ( "Loading test262 repository archive from {0}" , uri ) ;
50
+
51
+ using ( var client = new HttpClient ( ) )
52
+ {
53
+ using ( var downloadStream = await client . GetStreamAsync ( uri ) )
54
+ {
55
+ using ( var writeStream = File . OpenWrite ( zipFileWithLargeAmountOfEntriesPath ) )
56
+ {
57
+ await downloadStream . CopyToAsync ( writeStream ) ;
58
+ Console . WriteLine ( "File downloaded and saved to {0}" , zipFileWithLargeAmountOfEntriesPath ) ;
59
+ }
60
+ }
61
+ }
62
+ }
63
+
36
64
}
37
65
38
66
[ Benchmark ]
@@ -46,12 +74,28 @@ public long ReadZipInputStream()
46
74
47
75
while ( zipInputStream . Read ( readBuffer , 0 , readBuffer . Length ) > 0 )
48
76
{
49
-
50
77
}
51
78
52
79
return entry . Size ;
53
80
}
54
81
}
55
82
}
83
+
84
+ [ Benchmark ]
85
+ public void ReadLargeZipFile ( )
86
+ {
87
+ using ( var file = new ZipFile ( zipFileWithLargeAmountOfEntriesPath ) )
88
+ {
89
+ foreach ( ZipEntry entry in file )
90
+ {
91
+ using ( var stream = file . GetInputStream ( entry ) )
92
+ {
93
+ while ( stream . Read ( readBuffer , 0 , readBuffer . Length ) > 0 )
94
+ {
95
+ }
96
+ }
97
+ }
98
+ }
99
+ }
56
100
}
57
101
}
0 commit comments