Skip to content
Snippets Groups Projects
Commit 196e356a authored by L. Ellenbeck's avatar L. Ellenbeck
Browse files

updated unit test for S3MultipartStream coscine/issues#81

added more constructors to ZipUtilities for better usability coscine/issues#81
parent ddf7ca8b
No related branches found
No related tags found
2 merge requests!2Product/75 s3 zip metadata,!1Topic/81 s3 zip upload
......@@ -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
......@@ -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
......@@ -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)];
}
}
}
}
......@@ -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)
......
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
namespace coscine.cs_S3Zip
{
public class S3Zip
public class ZipUtilities
{
public S3MultipartStreamConfiguration S3ZipConfiguration { get; set; }
public S3Zip(S3MultipartStreamConfiguration s3ZipConfiguration)
public ZipUtilities(S3MultipartStreamConfiguration s3ZipConfiguration)
{
S3ZipConfiguration = s3ZipConfiguration;
}
public void ZipFolder(string path, ZipArchive zipArchive)
{
var directoryInfo = new DirectoryInfo(path);
ZipFolder(new DirectoryInfo(path), zipArchive);
}
public void ZipFolder(DirectoryInfo directoryInfo, ZipArchive zipArchive)
{
var prefixLength = directoryInfo.FullName.EndsWith(@"\") ? directoryInfo.FullName.Length : directoryInfo.FullName.Length + 1;
foreach (var file in directoryInfo.GetFiles("*", SearchOption.AllDirectories))
ZipFiles(directoryInfo.GetFiles("*", SearchOption.AllDirectories), prefixLength, zipArchive);
}
public void ZipFiles(IEnumerable<FileInfo> files, int prefixLength, ZipArchive zipArchive)
{
// cut off parent directory path
var fileEnty = zipArchive.CreateEntry(file.FullName.Substring(prefixLength));
using (var entryStream = fileEnty.Open())
foreach (var file in files)
{
// cut off parent directory path
var fileEntry = zipArchive.CreateEntry(file.FullName.Substring(prefixLength));
using (var fileStream = file.OpenRead())
{
fileStream.CopyTo(entryStream);
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(path, archive);
ZipFolder(directoryInfo, archive);
}
}
}
......
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment