diff --git a/SharpFileSystem.SharpZipLib/SharpZipLibFileSystem.cs b/SharpFileSystem.SharpZipLib/SharpZipLibFileSystem.cs index ec215e3..fa982c8 100644 --- a/SharpFileSystem.SharpZipLib/SharpZipLibFileSystem.cs +++ b/SharpFileSystem.SharpZipLib/SharpZipLibFileSystem.cs @@ -4,6 +4,7 @@ using System.Linq; using ICSharpCode.SharpZipLib.Zip; using SharpFileSystem.FileSystems; +using SharpFileSystem.IO; namespace SharpFileSystem.SharpZipLib { @@ -78,9 +79,29 @@ public bool Exists(FileSystemPath path) public Stream CreateFile(FileSystemPath path) { + return CreateFile(path,null); + } + public Stream CreateFile(FileSystemPath path, byte[] data) + { + BeginUpdate(); var entry = new MemoryZipEntry(); ZipFile.Add(entry, ToEntryPath(path)); - return entry.GetSource(); + var s = entry.GetSource(); + + if (data!=null) s.Write(data); + + EndUpdate(); + return s; + } + private void BeginUpdate() + { + if (!ZipFile.IsUpdating) + ZipFile.BeginUpdate(); + } + private void EndUpdate() + { + if (ZipFile.IsUpdating) + ZipFile.CommitUpdate(); } public Stream OpenFile(FileSystemPath path, FileAccess access) @@ -92,12 +113,16 @@ public Stream OpenFile(FileSystemPath path, FileAccess access) public void CreateDirectory(FileSystemPath path) { + BeginUpdate(); ZipFile.AddDirectory(ToEntryPath(path)); + EndUpdate(); } public void Delete(FileSystemPath path) { + BeginUpdate(); ZipFile.Delete(ToEntryPath(path)); + EndUpdate(); } public class MemoryZipEntry: MemoryFileSystem.MemoryFile, IStaticDataSource diff --git a/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemTest.cs b/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemTest.cs index 1d3c543..6668af1 100644 --- a/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemTest.cs +++ b/SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemTest.cs @@ -15,13 +15,14 @@ public class SharpZipLibFileSystemTest { private Stream zipStream; private SharpZipLibFileSystem fileSystem; - + private MemoryStream memoryStream; [OneTimeSetUp] public void Initialize() { - var memoryStream = new MemoryStream(); + memoryStream = new MemoryStream(); zipStream = memoryStream; var zipOutput = new ZipOutputStream(zipStream); + zipOutput.UseZip64 = UseZip64.Off; var fileContentString = "this is a file"; var fileContentBytes = Encoding.ASCII.GetBytes(fileContentString); @@ -78,5 +79,24 @@ public void ExistsTest() Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/nonExistingDirectory/"))); Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/directory/nonExistingFileInDirectory"))); } + + [Test] + public void CanCreateFileTest() + { + var text = "recent text"; + var textBytes = Encoding.ASCII.GetBytes(text); + var fsp = FileSystemPath.Parse("/mostrecentfile.txt"); + var fs = fileSystem.CreateFile(fsp, textBytes); + fs.Close(); + Assert.IsTrue(fileSystem.Exists(fsp)); + + fs = fileSystem.OpenFile(fsp, FileAccess.Read); + var fsText = fs.ReadAllText(); + + Assert.IsTrue(fsText.Equals(text)); + + //delete the file we created, so as not to intefere with the other tests. + fileSystem.Delete(fsp); + } } }