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,57 @@ 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
+ using ( var inputStream = zipFile . GetInputStream ( entry ) )
206
+ using ( var sr = new StreamReader ( zis , Encoding . UTF8 ) )
207
+ {
208
+ var content = await sr . ReadToEndAsync ( ) ;
209
+ Assert . That ( content , Is . EqualTo ( DummyDataString ) , "Decompressed content does not match input data" ) ;
210
+ }
211
+ }
212
+ }
213
+ }
214
+ }
215
+
216
+ // Shared helper for the ZipFileStoreAes tests
217
+ private static Stream MakeAESEncryptedZipStream ( string password )
218
+ {
219
+ var memoryStream = new MemoryStream ( ) ;
220
+
221
+ // Try to create a zip stream
222
+ WriteEncryptedZipToStream ( memoryStream , password , 256 , CompressionMethod . Stored ) ;
223
+
224
+ // reset
225
+ memoryStream . Seek ( 0 , SeekOrigin . Begin ) ;
226
+
227
+ return memoryStream ;
228
+ }
229
+
183
230
/// <summary>
184
231
/// Test using AES encryption on a file whose contents are Stored rather than deflated
185
232
/// </summary>
@@ -469,7 +516,7 @@ public void ZipinputStreamShouldGracefullyFailWithAESStreams()
469
516
}
470
517
}
471
518
472
- public void WriteEncryptedZipToStream ( Stream stream , string password , int keySize , CompressionMethod compressionMethod = CompressionMethod . Deflated )
519
+ public static void WriteEncryptedZipToStream ( Stream stream , string password , int keySize , CompressionMethod compressionMethod = CompressionMethod . Deflated )
473
520
{
474
521
using ( var zs = new ZipOutputStream ( stream ) )
475
522
{
@@ -496,7 +543,7 @@ public void WriteEncryptedZipToStream(Stream stream, int entryCount, string pass
496
543
}
497
544
}
498
545
499
- private void AddEncrypedEntryToStream ( ZipOutputStream zipOutputStream , string entryName , int keySize , CompressionMethod compressionMethod )
546
+ private static void AddEncrypedEntryToStream ( ZipOutputStream zipOutputStream , string entryName , int keySize , CompressionMethod compressionMethod )
500
547
{
501
548
ZipEntry zipEntry = new ZipEntry ( entryName )
502
549
{
0 commit comments