Skip to content

Commit 7ac1fda

Browse files
authored
#579: Implement very simple ReadAsync in ZipAESStream
* add an extra unit test for doing an async read on a ZipFile InputStream * Implement ReadAsync in ZipAESStream, extra simple version
1 parent 99170e1 commit 7ac1fda

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.IO;
33
using System.Security.Cryptography;
4+
using System.Threading;
5+
using System.Threading.Tasks;
46
using ICSharpCode.SharpZipLib.Core;
57
using ICSharpCode.SharpZipLib.Zip;
68

@@ -91,6 +93,13 @@ public override int Read(byte[] buffer, int offset, int count)
9193
return nBytes;
9294
}
9395

96+
/// <inheritdoc/>
97+
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
98+
{
99+
var readCount = Read(buffer, offset, count);
100+
return Task.FromResult(readCount);
101+
}
102+
94103
// Read data from the underlying stream and decrypt it
95104
private int ReadAndTransform(byte[] buffer, int offset, int count)
96105
{

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs

+56-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Text;
66
using ICSharpCode.SharpZipLib.Tests.TestSupport;
7+
using System.Threading.Tasks;
78

89
namespace 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,57 @@ 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+
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+
183230
/// <summary>
184231
/// Test using AES encryption on a file whose contents are Stored rather than deflated
185232
/// </summary>
@@ -469,7 +516,7 @@ public void ZipinputStreamShouldGracefullyFailWithAESStreams()
469516
}
470517
}
471518

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)
473520
{
474521
using (var zs = new ZipOutputStream(stream))
475522
{
@@ -496,7 +543,7 @@ public void WriteEncryptedZipToStream(Stream stream, int entryCount, string pass
496543
}
497544
}
498545

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)
500547
{
501548
ZipEntry zipEntry = new ZipEntry(entryName)
502549
{

0 commit comments

Comments
 (0)