From 196e356a58e89627a8305433a0e5fdd280639ced Mon Sep 17 00:00:00 2001 From: "L. Ellenbeck" <ellenbeck@itc.rwth-aachen.de> Date: Wed, 8 May 2019 13:52:19 +0200 Subject: [PATCH] updated unit test for S3MultipartStream coscine/issues#81 added more constructors to ZipUtilities for better usability coscine/issues#81 --- README.md | 6 +-- docs/home.md | 6 +-- src/cs-S3Zip.Tests/S3ZipTests.cs | 41 +++++++++-------- src/cs-S3Zip/S3MultipartStream.cs | 2 +- src/cs-S3Zip/S3Zip.cs | 44 ------------------ src/cs-S3Zip/ZipUtilities.cs | 75 +++++++++++++++++++++++++++++++ src/cs-S3Zip/cs-S3Zip.csproj | 2 +- 7 files changed, 103 insertions(+), 73 deletions(-) delete mode 100644 src/cs-S3Zip/S3Zip.cs create mode 100644 src/cs-S3Zip/ZipUtilities.cs diff --git a/README.md b/README.md index 7da81c1..e1c42f1 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ The allows to zip a folder and/or upload a whole stream in chunks to a S3 object For example: ``` -var s3 = new S3Zip(s3MultipartStreamConfiguration); -s3.ZipAndUploadFolder(@"c:\temp\test", "Test.zip"); +var zipUtilities = new ZipUtilities(_s3MultipartStreamConfiguration); +zipUtilities.ZipAndUploadFolder(@"c:\temp\test", "test.zip"); ``` -A zip file of the folder will be in S3 under the key Test.zip. \ No newline at end of file +A zip file of the folder will be in S3 under the key test.zip. \ No newline at end of file diff --git a/docs/home.md b/docs/home.md index 7da81c1..e1c42f1 100644 --- a/docs/home.md +++ b/docs/home.md @@ -4,8 +4,8 @@ The allows to zip a folder and/or upload a whole stream in chunks to a S3 object For example: ``` -var s3 = new S3Zip(s3MultipartStreamConfiguration); -s3.ZipAndUploadFolder(@"c:\temp\test", "Test.zip"); +var zipUtilities = new ZipUtilities(_s3MultipartStreamConfiguration); +zipUtilities.ZipAndUploadFolder(@"c:\temp\test", "test.zip"); ``` -A zip file of the folder will be in S3 under the key Test.zip. \ No newline at end of file +A zip file of the folder will be in S3 under the key test.zip. \ No newline at end of file diff --git a/src/cs-S3Zip.Tests/S3ZipTests.cs b/src/cs-S3Zip.Tests/S3ZipTests.cs index 966c81a..df276e1 100644 --- a/src/cs-S3Zip.Tests/S3ZipTests.cs +++ b/src/cs-S3Zip.Tests/S3ZipTests.cs @@ -18,8 +18,8 @@ namespace coscine.cs_S3Zip.Tests public S3ZipTests() { - var accessKey = Environment.GetEnvironmentVariable("S3_ACCESS_KEY", EnvironmentVariableTarget.User); - var secretKey = Environment.GetEnvironmentVariable("S3_SECRET_KEY", EnvironmentVariableTarget.User); + var accessKey = Environment.GetEnvironmentVariable("S3_ACCESS_KEY", EnvironmentVariableTarget.Process); + var secretKey = Environment.GetEnvironmentVariable("S3_SECRET_KEY", EnvironmentVariableTarget.Process); ONE_MB = (int)Math.Pow(2, 20); @@ -36,14 +36,6 @@ namespace coscine.cs_S3Zip.Tests }; } - /*[Test] - public void ZipAndUpload() - { - var s3 = new S3Zip(_s3MultipartStreamConfiguration); - s3.ZipAndUploadFolder(@"c:\temp\test", "zipTest4.zip"); - Assert.IsTrue(true); - }*/ - [TestCase(110, 50)] [TestCase(110, 5)] [TestCase(30, 50)] @@ -52,23 +44,19 @@ namespace coscine.cs_S3Zip.Tests { var key = "S3StreamTest.txt"; var chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray(); - var random = new Random(); - var randomArray = new byte[length * ONE_MB]; + var seed = DateTime.Now.Millisecond; + var random = new Random(seed); + var randomBuffer = new byte[ONE_MB]; var oldChunkSize = _s3MultipartStreamConfiguration.ChunckSize; _s3MultipartStreamConfiguration.ChunckSize = chunkSize * ONE_MB; - // fill array with random data - for (int i = 0; i < randomArray.Length; i++) - { - randomArray[i] = (byte)chars[random.Next(chars.Length)]; - } - using (var s3Stream = new S3MultipartStream(key, _s3MultipartStreamConfiguration)) { for (int i = 0; i < length; i++) { - s3Stream.Write(randomArray, i * ONE_MB, ONE_MB); + FillArrayWithRandomChars(randomBuffer, randomBuffer.Length, chars, random); + s3Stream.Write(randomBuffer, 0, ONE_MB); } } @@ -78,6 +66,9 @@ namespace coscine.cs_S3Zip.Tests Key = key }; + // reset the random number generator + random = new Random(seed); + using (var client = new AmazonS3Client(_s3MultipartStreamConfiguration.AccessKey, _s3MultipartStreamConfiguration.SecretKey, _s3MultipartStreamConfiguration.AmazonS3Config)) { var buffer = new byte[ONE_MB]; @@ -94,15 +85,23 @@ namespace coscine.cs_S3Zip.Tests for (int i = 0; i < length; i++) { int read = responseStream.Read(buffer, 0, buffer.Length); + FillArrayWithRandomChars(randomBuffer, read, chars, random); // check if the data is equal to the previously generated one. - Assert.IsTrue(Enumerable.SequenceEqual(randomArray.Skip(alreadyRead).Take(read), buffer.Take(read))); + Assert.IsTrue(Enumerable.SequenceEqual(randomBuffer.Take(read), buffer.Take(read))); alreadyRead += read; } } } } - _s3MultipartStreamConfiguration.ChunckSize = oldChunkSize; } + + private void FillArrayWithRandomChars(byte[] randomBuffer, int length, char[] chars, Random random) + { + for (int i = 0; i < length; i++) + { + randomBuffer[i] = (byte)chars[random.Next(chars.Length)]; + } + } } } diff --git a/src/cs-S3Zip/S3MultipartStream.cs b/src/cs-S3Zip/S3MultipartStream.cs index 028be4a..90f378c 100644 --- a/src/cs-S3Zip/S3MultipartStream.cs +++ b/src/cs-S3Zip/S3MultipartStream.cs @@ -60,7 +60,7 @@ namespace coscine.cs_S3Zip public override void Flush() { - // MS documentation says, that you should leave it empty for read only streams, to keep them compatible. + _internalStream.Flush(); } public override int Read(byte[] buffer, int offset, int count) diff --git a/src/cs-S3Zip/S3Zip.cs b/src/cs-S3Zip/S3Zip.cs deleted file mode 100644 index ed002ce..0000000 --- a/src/cs-S3Zip/S3Zip.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.IO; -using System.IO.Compression; - -namespace coscine.cs_S3Zip -{ - public class S3Zip - { - public S3MultipartStreamConfiguration S3ZipConfiguration { get; set; } - - public S3Zip(S3MultipartStreamConfiguration s3ZipConfiguration) - { - S3ZipConfiguration = s3ZipConfiguration; - } - - public void ZipFolder(string path, ZipArchive zipArchive) - { - var directoryInfo = new DirectoryInfo(path); - var prefixLength = directoryInfo.FullName.EndsWith(@"\") ? directoryInfo.FullName.Length : directoryInfo.FullName.Length + 1; - foreach (var file in directoryInfo.GetFiles("*", SearchOption.AllDirectories)) - { - // cut off parent directory path - var fileEnty = zipArchive.CreateEntry(file.FullName.Substring(prefixLength)); - using (var entryStream = fileEnty.Open()) - { - using (var fileStream = file.OpenRead()) - { - fileStream.CopyTo(entryStream); - } - } - } - } - - public void ZipAndUploadFolder(string path, string key) - { - using (var s3Stream = new S3MultipartStream(key, S3ZipConfiguration)) - { - using (var archive = new ZipArchive(s3Stream, ZipArchiveMode.Create, true)) - { - ZipFolder(path, archive); - } - } - } - } -} diff --git a/src/cs-S3Zip/ZipUtilities.cs b/src/cs-S3Zip/ZipUtilities.cs new file mode 100644 index 0000000..b717ce7 --- /dev/null +++ b/src/cs-S3Zip/ZipUtilities.cs @@ -0,0 +1,75 @@ +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; + +namespace coscine.cs_S3Zip +{ + public class ZipUtilities + { + public S3MultipartStreamConfiguration S3ZipConfiguration { get; set; } + + public ZipUtilities(S3MultipartStreamConfiguration s3ZipConfiguration) + { + S3ZipConfiguration = s3ZipConfiguration; + } + + public void ZipFolder(string path, ZipArchive zipArchive) + { + ZipFolder(new DirectoryInfo(path), zipArchive); + } + + public void ZipFolder(DirectoryInfo directoryInfo, ZipArchive zipArchive) + { + var prefixLength = directoryInfo.FullName.EndsWith(@"\") ? directoryInfo.FullName.Length : directoryInfo.FullName.Length + 1; + ZipFiles(directoryInfo.GetFiles("*", SearchOption.AllDirectories), prefixLength, zipArchive); + } + + public void ZipFiles(IEnumerable<FileInfo> files, int prefixLength, ZipArchive zipArchive) + { + foreach (var file in files) + { + // cut off parent directory path + var fileEntry = zipArchive.CreateEntry(file.FullName.Substring(prefixLength)); + using (var fileStream = file.OpenRead()) + { + ZipStream(fileStream, fileEntry); + } + } + } + + public void ZipStream(Stream stream, ZipArchiveEntry fileEntry) + { + using (var entryStream = fileEntry.Open()) + { + stream.CopyTo(entryStream); + } + } + + + public void ZipAndUploadFolder(string path) + { + ZipAndUploadFolder(new DirectoryInfo(path)); + } + + public void ZipAndUploadFolder(DirectoryInfo directoryInfo) + { + ZipAndUploadFolder(directoryInfo, directoryInfo.Name); + } + + public void ZipAndUploadFolder(string path, string key) + { + ZipAndUploadFolder(new DirectoryInfo(path), key); + } + + public void ZipAndUploadFolder(DirectoryInfo directoryInfo, string key) + { + using (var s3Stream = new S3MultipartStream(key, S3ZipConfiguration)) + { + using (var archive = new ZipArchive(s3Stream, ZipArchiveMode.Create, true)) + { + ZipFolder(directoryInfo, archive); + } + } + } + } +} diff --git a/src/cs-S3Zip/cs-S3Zip.csproj b/src/cs-S3Zip/cs-S3Zip.csproj index 7872ba7..49c61bf 100644 --- a/src/cs-S3Zip/cs-S3Zip.csproj +++ b/src/cs-S3Zip/cs-S3Zip.csproj @@ -49,7 +49,7 @@ </ItemGroup> <ItemGroup> <Compile Include="S3MultipartStream.cs" /> - <Compile Include="S3Zip.cs" /> + <Compile Include="ZipUtilities.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="S3MultipartStreamConfiguration.cs" /> </ItemGroup> -- GitLab