44using System . IO ;
55using System . Text ;
66using ICSharpCode . SharpZipLib . Tests . TestSupport ;
7+ using System . Threading . Tasks ;
78
89namespace ICSharpCode . SharpZipLib . Tests . Zip
910{
@@ -149,14 +150,9 @@ public void ZipFileStoreAes()
149150 {
150151 string password = "password" ;
151152
152- using ( var memoryStream = new MemoryStream ( ) )
153+ // Make an encrypted zip file
154+ using ( var memoryStream = MakeAESEncryptedZipStream ( password ) )
153155 {
154- // Try to create a zip stream
155- WriteEncryptedZipToStream ( memoryStream , password , 256 , CompressionMethod . Stored ) ;
156-
157- // reset
158- memoryStream . Seek ( 0 , SeekOrigin . Begin ) ;
159-
160156 // try to read it
161157 var zipFile = new ZipFile ( memoryStream , leaveOpen : true )
162158 {
@@ -180,6 +176,59 @@ public void ZipFileStoreAes()
180176 }
181177 }
182178
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+
183232 /// <summary>
184233 /// Test using AES encryption on a file whose contents are Stored rather than deflated
185234 /// </summary>
@@ -469,7 +518,7 @@ public void ZipinputStreamShouldGracefullyFailWithAESStreams()
469518 }
470519 }
471520
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 )
473522 {
474523 using ( var zs = new ZipOutputStream ( stream ) )
475524 {
@@ -496,7 +545,7 @@ public void WriteEncryptedZipToStream(Stream stream, int entryCount, string pass
496545 }
497546 }
498547
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 )
500549 {
501550 ZipEntry zipEntry = new ZipEntry ( entryName )
502551 {
0 commit comments