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

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

Closed
FcAYH opened this issue Dec 10, 2023 · 4 comments · Fixed by #88
Closed

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

FcAYH opened this issue Dec 10, 2023 · 4 comments · Fixed by #88
Labels

Comments

@FcAYH
Copy link

FcAYH commented Dec 10, 2023

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
Copy link
Owner

xoofx commented Dec 10, 2023

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

@xoofx xoofx added the question label Dec 10, 2023
@FcAYH
Copy link
Author

FcAYH commented Dec 11, 2023

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
Copy link
Author

FcAYH commented Dec 11, 2023

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
Copy link
Contributor

I think this is blocked by dotnet/runtime#24149

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants