Skip to content

Commit

Permalink
Merge pull request #88 from GerardSmit/fix/zip-archive
Browse files Browse the repository at this point in the history
Fixed ZipArchiveFileSystem and project warnings
  • Loading branch information
xoofx committed Jun 1, 2024
2 parents e243083 + 917a70e commit 16d85e5
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 202 deletions.
101 changes: 101 additions & 0 deletions src/Zio.Tests/FileSystems/TestZipArchiveFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,105 @@ public void TestOpenStreamsMultithreaded()
thread1.Join();
thread2.Join();
}


[Theory]
[InlineData("TestData/Linux.zip")]
[InlineData("TestData/Windows.zip")]
public void TestCaseInSensitiveZip(string path)
{
using var stream = File.OpenRead(path);
using var archive = new ZipArchive(stream, ZipArchiveMode.Read);
var fs = new ZipArchiveFileSystem(archive);

Assert.True(fs.DirectoryExists("/Folder"));
Assert.True(fs.DirectoryExists("/folder"));

Assert.False(fs.FileExists("/Folder"));
Assert.False(fs.FileExists("/folder"));

Assert.True(fs.FileExists("/Folder/File.txt"));
Assert.True(fs.FileExists("/folder/file.txt"));

Assert.False(fs.DirectoryExists("/Folder/file.txt"));
Assert.False(fs.DirectoryExists("/folder/File.txt"));
}

[Theory]
[InlineData("TestData/Linux.zip")]
[InlineData("TestData/Windows.zip")]
public void TestCaseSensitiveZip(string path)
{
using var stream = File.OpenRead(path);
using var archive = new ZipArchive(stream, ZipArchiveMode.Read);
var fs = new ZipArchiveFileSystem(archive, true);

Assert.True(fs.DirectoryExists("/Folder"));
Assert.False(fs.DirectoryExists("/folder"));

Assert.False(fs.FileExists("/Folder"));
Assert.False(fs.FileExists("/folder"));

Assert.True(fs.FileExists("/Folder/File.txt"));
Assert.False(fs.FileExists("/folder/file.txt"));

Assert.False(fs.DirectoryExists("/Folder/file.txt"));
Assert.False(fs.DirectoryExists("/folder/File.txt"));
}

[Fact]
public void TestSaveStream()
{
var stream = new MemoryStream();

using var fs = new ZipArchiveFileSystem(stream);

fs.WriteAllText("/a/b.txt", "abc");
fs.Save();

stream.Seek(0, SeekOrigin.Begin);

using (var fs2 = new ZipArchiveFileSystem(stream, ZipArchiveMode.Read, leaveOpen: true))
{
Assert.Equal("abc", fs2.ReadAllText("/a/b.txt"));
}

Assert.Equal("abc", fs.ReadAllText("/a/b.txt"));
fs.WriteAllText("/a/b.txt", "def");
fs.Save();

stream.Seek(0, SeekOrigin.Begin);

using (var fs2 = new ZipArchiveFileSystem(stream, ZipArchiveMode.Read, leaveOpen: true))
{
Assert.Equal("def", fs2.ReadAllText("/a/b.txt"));
}
}

[Fact]
public void TestSaveFile()
{
var path = Path.Combine(SystemPath, Guid.NewGuid().ToString("N") + ".zip");

try
{
using var fs = new ZipArchiveFileSystem(path);

Assert.Equal(0, new FileInfo(path).Length);

fs.WriteAllText("/a/b.txt", "abc");
fs.Save();

// We cannot check the content because the file is still open
Assert.NotEqual(0, new FileInfo(path).Length);

// Ensure we can save multiple times
fs.WriteAllText("/a/b.txt", "def");
fs.Save();
}
finally
{
File.Delete(path);
}
}
}
Binary file added src/Zio.Tests/TestData/Linux.zip
Binary file not shown.
Binary file added src/Zio.Tests/TestData/Windows.zip
Binary file not shown.
6 changes: 6 additions & 0 deletions src/Zio.Tests/Zio.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@
<ItemGroup>
<ProjectReference Include="..\Zio\Zio.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestData\**\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
10 changes: 5 additions & 5 deletions src/Zio/FileSystems/FileSystemWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ protected void UnregisterEvents(IFileSystemWatcher watcher)
return pathFromEvent;
}

private void OnChanged(object sender, FileChangedEventArgs args)
private void OnChanged(object? sender, FileChangedEventArgs args)
{
var newPath = TryConvertPath(args.FullPath);
if (!newPath.HasValue)
Expand All @@ -235,7 +235,7 @@ private void OnChanged(object sender, FileChangedEventArgs args)
RaiseChanged(newArgs);
}

private void OnCreated(object sender, FileChangedEventArgs args)
private void OnCreated(object? sender, FileChangedEventArgs args)
{
var newPath = TryConvertPath(args.FullPath);
if (!newPath.HasValue)
Expand All @@ -247,7 +247,7 @@ private void OnCreated(object sender, FileChangedEventArgs args)
RaiseCreated(newArgs);
}

private void OnDeleted(object sender, FileChangedEventArgs args)
private void OnDeleted(object? sender, FileChangedEventArgs args)
{
var newPath = TryConvertPath(args.FullPath);
if (!newPath.HasValue)
Expand All @@ -259,12 +259,12 @@ private void OnDeleted(object sender, FileChangedEventArgs args)
RaiseDeleted(newArgs);
}

private void OnError(object sender, FileSystemErrorEventArgs args)
private void OnError(object? sender, FileSystemErrorEventArgs args)
{
RaiseError(args);
}

private void OnRenamed(object sender, FileRenamedEventArgs args)
private void OnRenamed(object? sender, FileRenamedEventArgs args)
{
var newPath = TryConvertPath(args.FullPath);
if (!newPath.HasValue)
Expand Down
10 changes: 5 additions & 5 deletions src/Zio/FileSystems/MemoryFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected override void DeleteDirectoryImpl(UPath path, bool isRecursive)
}
finally
{
if (deleteRootDirectory)
if (deleteRootDirectory && result.Node != null)
{
result.Node.DetachFromParent();
result.Node.Dispose();
Expand Down Expand Up @@ -1008,7 +1008,7 @@ private void MoveFileOrDirectory(UPath srcPath, UPath destPath, bool expectDirec
var parentSrcPath = srcPath.GetDirectory();
var parentDestPath = destPath.GetDirectory();

void AssertNoDestination(FileSystemNode node)
void AssertNoDestination(FileSystemNode? node)
{
if (expectDirectory)
{
Expand Down Expand Up @@ -1140,7 +1140,7 @@ private static void ValidateFile([NotNull] FileSystemNode? node, UPath srcPath)
}
}

private FileSystemNode TryFindNodeSafe(UPath path)
private FileSystemNode? TryFindNodeSafe(UPath path)
{
EnterFileSystemShared();
try
Expand Down Expand Up @@ -1193,7 +1193,7 @@ private void CreateDirectoryNode(UPath path)

private readonly struct NodeResult
{
public NodeResult(DirectoryNode? directory, FileSystemNode node, string? name, FindNodeFlags flags)
public NodeResult(DirectoryNode? directory, FileSystemNode? node, string? name, FindNodeFlags flags)
{
Directory = directory;
Node = node;
Expand All @@ -1203,7 +1203,7 @@ public NodeResult(DirectoryNode? directory, FileSystemNode node, string? name, F

public readonly DirectoryNode? Directory;

public readonly FileSystemNode Node;
public readonly FileSystemNode? Node;

public readonly string? Name;

Expand Down
Loading

0 comments on commit 16d85e5

Please sign in to comment.