From 71875a2e73d98164594d370614925862ac772f7c Mon Sep 17 00:00:00 2001 From: iason leiloglou Date: Sat, 8 Apr 2017 15:14:21 -0400 Subject: [PATCH 1/3] basic support for password protected (ZipCrypto) zip files --- .../SharpZipLibFileSystem.cs | 12 ++ .../SharpFileSystem.Tests.csproj | 5 +- .../SharpZipLibFileSystemWithPasswordTest.cs | 110 ++++++++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemWithPasswordTest.cs diff --git a/SharpFileSystem.SharpZipLib/SharpZipLibFileSystem.cs b/SharpFileSystem.SharpZipLib/SharpZipLibFileSystem.cs index ec215e3..db3e7bd 100644 --- a/SharpFileSystem.SharpZipLib/SharpZipLibFileSystem.cs +++ b/SharpFileSystem.SharpZipLib/SharpZipLibFileSystem.cs @@ -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; diff --git a/SharpFileSystem.Tests/SharpFileSystem.Tests.csproj b/SharpFileSystem.Tests/SharpFileSystem.Tests.csproj index 606a39c..bfb6e94 100644 --- a/SharpFileSystem.Tests/SharpFileSystem.Tests.csproj +++ b/SharpFileSystem.Tests/SharpFileSystem.Tests.csproj @@ -1,4 +1,4 @@ - + Debug @@ -44,6 +44,7 @@ + @@ -72,4 +73,4 @@ - + \ No newline at end of file diff --git a/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemWithPasswordTest.cs b/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemWithPasswordTest.cs new file mode 100644 index 0000000..5ad58e0 --- /dev/null +++ b/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemWithPasswordTest.cs @@ -0,0 +1,110 @@ +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 zipfileAPath = FileSystemPath.Parse("/internalzip.zip"); + 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( () => + { + badFileSystem.OpenFile(textfileAPath, FileAccess.Read); + }); + } + } +} From 54163790d9a9a38196fdbc9a4b61fea5bfd31b42 Mon Sep 17 00:00:00 2001 From: iason leiloglou Date: Sat, 8 Apr 2017 15:19:40 -0400 Subject: [PATCH 2/3] removed an unused global string --- .../SharpZipLib/SharpZipLibFileSystemWithPasswordTest.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemWithPasswordTest.cs b/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemWithPasswordTest.cs index 5ad58e0..3110453 100644 --- a/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemWithPasswordTest.cs +++ b/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemWithPasswordTest.cs @@ -57,7 +57,6 @@ public void Cleanup() private readonly FileSystemPath directoryPath = FileSystemPath.Parse("/directory/"); private readonly FileSystemPath textfileAPath = FileSystemPath.Parse("/textfileA.txt"); - private readonly FileSystemPath zipfileAPath = FileSystemPath.Parse("/internalzip.zip"); private readonly FileSystemPath fileInDirectoryPath = FileSystemPath.Parse("/directory/fileInDirectory.txt"); [Test] From f753a6227a648922c92b9ba6bb4c7b14d984f436 Mon Sep 17 00:00:00 2001 From: iason leiloglou Date: Sat, 8 Apr 2017 15:20:03 -0400 Subject: [PATCH 3/3] update to csproj. --- SharpFileSystem.Tests/SharpFileSystem.Tests.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SharpFileSystem.Tests/SharpFileSystem.Tests.csproj b/SharpFileSystem.Tests/SharpFileSystem.Tests.csproj index bfb6e94..b3386f0 100644 --- a/SharpFileSystem.Tests/SharpFileSystem.Tests.csproj +++ b/SharpFileSystem.Tests/SharpFileSystem.Tests.csproj @@ -72,5 +72,8 @@ PreserveNewest + + + \ No newline at end of file