jvilk / BrowserFS

BrowserFS is an in-browser filesystem that emulates the Node JS filesystem API and supports storing and retrieving files from various backends.
Other
3.06k stars 216 forks source link

How to load a zip file's content right direct into an LocalStorageFS? #333

Closed Atybot closed 1 year ago

james-pre commented 1 year ago

@Atybot Currently you would create an LocalStorageFS and ZipFS and mount them to a MountableFilesystem (e.g. /localstorage and /zip). then you could do fs.cp('/zip', '/localstorage', { recursive: true });

In the future, we could have a separate function for unpacking zip files (e.g. BrowserFS.BFSUtils.ConvertZipToDirectory)

h365chen commented 11 months ago

@james-pre Thanks for maintaining this great project! Following up the question, it seems the fs.cp is not in the code. I was wondering where to find it? Or currently I need to write my own traversal function for it? Thanks.

james-pre commented 11 months ago

@h365chen

That is correct. You would have to use fs.rename/fs.renameSync instead of copying until I get around to adding cp, rm, and mv.

I'm currently updating the tests and working on getting my changes put out to NPM. If my improvements are only on the GitHub repo it will not help the majority of BFS users.

h365chen commented 11 months ago

@james-pre

fs.rename / fs.renameSync seems not able to move dir from ZipFS. Does making it OverlayFS help?

Currently, I have a MountableFS where /zip is ZipFS and /mem is InMemory, but I don’t know how to make /zip as an OverlayFS.

I would like to copy /zip/* to /mem/

james-pre commented 11 months ago

@h365chen Looking through the source, it does not appear there is a way to copy files.

The best way I can think of to fix this (temporarily) is like so:

const overlayFS = await BrowserFS.FileSystem.OverlayFS.CreateAsync({ readable: zipFS, writable: memFS });
mountableFS.mount('/mnt/zip', overlayFS);

This assumes you are working with ES6+. If not, you must deal with the callbacks or .then.

h365chen commented 11 months ago

@james-pre Thanks! I was using npm install browserfs@beta so I guess there is no CreateAsync there. I'll see if I can find a workaround. Thanks!

james-pre commented 11 months ago

@h365chen You can use it with the regular Create method:

BrowserFS.FileSystem.OverlayFS.Create({ readable: zipFS, writable: memFS }, (e, overlayFS) => {
    if(e) {
        throw e;
    }

    mountableFS.mount('/mnt/zip', overlayFS);
});