xoofx / zio

A cross-platform abstract/virtual filesystem framework with many built-ins filesystems for .NET
BSD 2-Clause "Simplified" License
827 stars 61 forks source link

Is it possible to save FileSystem changes to disk? #81

Closed FcAYH closed 5 months ago

FcAYH commented 11 months ago

I have used the ZipArchiveFileSystem to create a zip file on disk. And then I add some directory and files. But how can I save these modifications to disk?

I use the code below:

using Zio;
using Zio.FileSystems;

namespace ZioTest;

internal class ZipCreator
{
    public void CreateZip()
    {
        var path = Environment.CurrentDirectory;
        //Console.WriteLine(path);

        var zipFs = new ZipArchiveFileSystem($"{path}/test1.zip");
        zipFs.CreateDirectory("/test");
        zipFs.WriteAllText("/test/test.txt", "Hello World");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        ZipCreator creator = new();
        creator.CreateZip();
    }
}

After I run the code, I got the zip file "test1.zip", but it's size is zero. image

I have read the document and not found any information about save a FileSystem to disk.

dotnet version: 7.0 zio version: 0.17.0

xoofx commented 11 months ago

ZipArchiveFileSystem was added by a contributor, I have never used it, but looking at the code, try to dispose the ZipArchiveFileSystem before exiting.

FcAYH commented 11 months ago

Yes, you are right, after I called zipFs.Dispose(), the changes were saved to the disk. But another question, if I want to modify it repeatedly and then save it repeatedly, I need to call Dispose() and then reread the local file. like this:

using Zio;
using Zio.FileSystems;

namespace ZioTest;

internal class ZipCreator
{
    private ZipArchiveFileSystem _zipFs;

    public void CreateZip(int index)
    {
        var path = Environment.CurrentDirectory;
        //Console.WriteLine(path);

        _zipFs = new ZipArchiveFileSystem($"{path}/test1.zip");
        _zipFs.CreateDirectory($"/test{index}");
        _zipFs.WriteAllText($"/test{index}/test.txt", "Hello World");
    }

    public void Close()
    {
        _zipFs.Dispose();
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        ZipCreator creator = new();
        creator.CreateZip(1);
        creator.Close();
        creator.CreateZip(2);
        creator.Close();
    }
}

These operations seems not very good for larger files. Is there any way to save changes manually like the belowing code:

using Zio;
using Zio.FileSystems;

namespace ZioTest;

internal class ZipCreator
{
    private ZipArchiveFileSystem _zipFs;

    public void CreateZip()
    {
        var path = Environment.CurrentDirectory;
        //Console.WriteLine(path);

        _zipFs = new ZipArchiveFileSystem($"{path}/test1.zip");
    }

    public void MakeContents(int index)
    {
        _zipFs.CreateDirectory($"/test{index}");
        _zipFs.WriteAllText($"/test{index}/test.txt", "Hello World");
    }

    public void PersistenceToDisk()
    {
        _zipFs.FlushToDisk(); // Is there any method to save the zip file without closing the file system?
    }

    public void Close()
    {
        _zipFs.Dispose();
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        ZipCreator creator = new();
        creator.CreateZip();
        creator.MakeContents(1);
        creator.PersistenceToDisk();
        creator.MakeContents(2);
        creator.Close();
    }
}
FcAYH commented 11 months ago

I see the ZipArchiveFileSystem.Dispose() called ZipArchive.Dispose(). So it should be ZipArchive.Dispose() that save its changes to disk. But I found that ZipArchive dosen't have a method to save itself unless call dispose. So I'm confused about the performance of calling dispose and then reloading the file every time I save the file to disk.

GerardSmit commented 11 months ago

I think this is blocked by https://github.com/dotnet/runtime/issues/24149