Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update before dispose #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
27 changes: 26 additions & 1 deletion SharpFileSystem.SharpZipLib/SharpZipLibFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using ICSharpCode.SharpZipLib.Zip;
using SharpFileSystem.FileSystems;
using SharpFileSystem.IO;

namespace SharpFileSystem.SharpZipLib
{
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
24 changes: 22 additions & 2 deletions SharpFileSystem.Tests/SharpZipLib/SharpZipLibFileSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}