Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions SharpFileSystem.SharpZipLib/SharpZipLibFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ public static SharpZipLibFileSystem Open(Stream s)
return new SharpZipLibFileSystem(new ZipFile(s));
}

public static SharpZipLibFileSystem Open(Stream s,string password)
{
return new SharpZipLibFileSystem(new ZipFile(s){Password = password});
}

public static SharpZipLibFileSystem Create(Stream s)
{
return new SharpZipLibFileSystem(ZipFile.Create(s));
}

public static SharpZipLibFileSystem Create(Stream s,string password)
{
var zipFile = ZipFile.Create(s);
zipFile.Password = password;
return new SharpZipLibFileSystem(zipFile);
}

private SharpZipLibFileSystem(ZipFile zipFile)
{
ZipFile = zipFile;
Expand Down
8 changes: 6 additions & 2 deletions SharpFileSystem.Tests/SharpFileSystem.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -44,6 +44,7 @@
<Compile Include="FileSystems\MemoryFileSystemTest.cs" />
<Compile Include="FileSystems\PhysicalFileSystemTest.cs" />
<Compile Include="SharpZipLib\SharpZipLibFileSystemTest.cs" />
<Compile Include="SharpZipLib\SharpZipLibFileSystemWithPasswordTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down Expand Up @@ -71,5 +72,8 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using NUnit.Framework;
using SharpFileSystem.IO;
using SharpFileSystem.SharpZipLib;

namespace SharpFileSystem.Tests.SharpZipLib
{
[TestFixture]
public class SharpZipLibFileSystemWithPasswordTest
{
private Stream zipStream;
private SharpZipLibFileSystem fileSystem;
private SharpZipLibFileSystem badFileSystem;
private readonly string zipPassword = "unittestpassword";
private readonly string fileContent = "this is a file";
[OneTimeSetUp]
public void Initialize()
{
CreateInitialZipStream();
fileSystem = SharpZipLibFileSystem.Open(zipStream,zipPassword);
badFileSystem = SharpZipLibFileSystem.Open(zipStream, "wrongpassword");
}

private void CreateInitialZipStream()
{
var memoryStream = new MemoryStream();
zipStream = memoryStream;
var zipOutput = new ZipOutputStream(zipStream);
zipOutput.Password = zipPassword;

var fileContentString = fileContent;
var fileContentBytes = Encoding.ASCII.GetBytes(fileContentString);
zipOutput.PutNextEntry(new ZipEntry("textfileA.txt")
{
Size = fileContentBytes.Length
});
zipOutput.Write(fileContentBytes);
zipOutput.PutNextEntry(new ZipEntry("directory/fileInDirectory.txt"));
zipOutput.Finish();

memoryStream.Position = 0;

}

[OneTimeTearDown]
public void Cleanup()
{
fileSystem.Dispose();
badFileSystem.Dispose();
zipStream.Dispose();
}

private readonly FileSystemPath directoryPath = FileSystemPath.Parse("/directory/");
private readonly FileSystemPath textfileAPath = FileSystemPath.Parse("/textfileA.txt");
private readonly FileSystemPath fileInDirectoryPath = FileSystemPath.Parse("/directory/fileInDirectory.txt");

[Test]
public void GetEntitiesOfRootTest()
{
CollectionAssert.AreEquivalent(new[]
{
textfileAPath,
directoryPath
}, fileSystem.GetEntities(FileSystemPath.Root).ToArray());
}

[Test]
public void GetEntitiesOfDirectoryTest()
{
CollectionAssert.AreEquivalent(new[]
{
fileInDirectoryPath
}, fileSystem.GetEntities(directoryPath).ToArray());
}

[Test]
public void ExistsTest()
{
Assert.IsTrue(fileSystem.Exists(FileSystemPath.Root));
Assert.IsTrue(fileSystem.Exists(textfileAPath));
Assert.IsTrue(fileSystem.Exists(directoryPath));
Assert.IsTrue(fileSystem.Exists(fileInDirectoryPath));
Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/nonExistingFile")));
Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/nonExistingDirectory/")));
Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/directory/nonExistingFileInDirectory")));
}

[Test]
public void CanOpenFileWithCorrectPassword()
{
var fs = fileSystem.OpenFile(textfileAPath, FileAccess.Read);
Assert.AreEqual(fs.ReadAllText(), fileContent);
}

[Test]
public void OpenFileThrowsZipExceptiontWithIncorrectPassword()
{
Assert.Throws<ZipException>( () =>
{
badFileSystem.OpenFile(textfileAPath, FileAccess.Read);
});
}
}
}