4
4
using System . IO ;
5
5
using System . Text ;
6
6
using ICSharpCode . SharpZipLib . Tests . TestSupport ;
7
+ using System . Threading . Tasks ;
7
8
8
9
namespace ICSharpCode . SharpZipLib . Tests . Zip
9
10
{
@@ -149,14 +150,9 @@ public void ZipFileStoreAes()
149
150
{
150
151
string password = "password" ;
151
152
152
- using ( var memoryStream = new MemoryStream ( ) )
153
+ // Make an encrypted zip file
154
+ using ( var memoryStream = MakeAESEncryptedZipStream ( password ) )
153
155
{
154
- // Try to create a zip stream
155
- WriteEncryptedZipToStream ( memoryStream , password , 256 , CompressionMethod . Stored ) ;
156
-
157
- // reset
158
- memoryStream . Seek ( 0 , SeekOrigin . Begin ) ;
159
-
160
156
// try to read it
161
157
var zipFile = new ZipFile ( memoryStream , leaveOpen : true )
162
158
{
@@ -180,6 +176,59 @@ public void ZipFileStoreAes()
180
176
}
181
177
}
182
178
179
+ /// <summary>
180
+ /// As <see cref="ZipFileStoreAes"/>, but with Async reads
181
+ /// </summary>
182
+ [ Test ]
183
+ [ Category ( "Encryption" ) ]
184
+ [ Category ( "Zip" ) ]
185
+ public async Task ZipFileStoreAesAsync ( )
186
+ {
187
+ string password = "password" ;
188
+
189
+ // Make an encrypted zip file
190
+ using ( var memoryStream = MakeAESEncryptedZipStream ( password ) )
191
+ {
192
+ // try to read it
193
+ var zipFile = new ZipFile ( memoryStream , leaveOpen : true )
194
+ {
195
+ Password = password
196
+ } ;
197
+
198
+ foreach ( ZipEntry entry in zipFile )
199
+ {
200
+ // Should be stored rather than deflated
201
+ Assert . That ( entry . CompressionMethod , Is . EqualTo ( CompressionMethod . Stored ) , "Entry should be stored" ) ;
202
+
203
+ using ( var zis = zipFile . GetInputStream ( entry ) )
204
+ {
205
+ var buffer = new byte [ entry . Size ] ;
206
+
207
+ using ( var inputStream = zipFile . GetInputStream ( entry ) )
208
+ using ( var sr = new StreamReader ( zis , Encoding . UTF8 ) )
209
+ {
210
+ var content = await sr . ReadToEndAsync ( ) ;
211
+ Assert . That ( content , Is . EqualTo ( DummyDataString ) , "Decompressed content does not match input data" ) ;
212
+ }
213
+ }
214
+ }
215
+ }
216
+ }
217
+
218
+ // Shared helper for the ZipFileStoreAes tests
219
+ private static Stream MakeAESEncryptedZipStream ( string password )
220
+ {
221
+ var memoryStream = new MemoryStream ( ) ;
222
+
223
+ // Try to create a zip stream
224
+ WriteEncryptedZipToStream ( memoryStream , password , 256 , CompressionMethod . Stored ) ;
225
+
226
+ // reset
227
+ memoryStream . Seek ( 0 , SeekOrigin . Begin ) ;
228
+
229
+ return memoryStream ;
230
+ }
231
+
183
232
/// <summary>
184
233
/// Test using AES encryption on a file whose contents are Stored rather than deflated
185
234
/// </summary>
@@ -469,7 +518,7 @@ public void ZipinputStreamShouldGracefullyFailWithAESStreams()
469
518
}
470
519
}
471
520
472
- public void WriteEncryptedZipToStream ( Stream stream , string password , int keySize , CompressionMethod compressionMethod = CompressionMethod . Deflated )
521
+ public static void WriteEncryptedZipToStream ( Stream stream , string password , int keySize , CompressionMethod compressionMethod = CompressionMethod . Deflated )
473
522
{
474
523
using ( var zs = new ZipOutputStream ( stream ) )
475
524
{
@@ -496,7 +545,7 @@ public void WriteEncryptedZipToStream(Stream stream, int entryCount, string pass
496
545
}
497
546
}
498
547
499
- private void AddEncrypedEntryToStream ( ZipOutputStream zipOutputStream , string entryName , int keySize , CompressionMethod compressionMethod )
548
+ private static void AddEncrypedEntryToStream ( ZipOutputStream zipOutputStream , string entryName , int keySize , CompressionMethod compressionMethod )
500
549
{
501
550
ZipEntry zipEntry = new ZipEntry ( entryName )
502
551
{
0 commit comments