[Free]VFS-Virtual File System Open Source

This extension will allow users to create a virtual file system (VFS) inside the app. Instead of writing files to the actual storage, it will keep them in memory and simulate a real filesystem

Download Now:
com.bbl.virtualfilesystem.aix (5.5 KB)

Features:

:white_check_mark: Create, read, delete, and check for files in a virtual storage system
:white_check_mark: Encrypt and decrypt virtual files using Base64
:white_check_mark: Export and import the entire virtual file system as a single encoded string

block1vfs

Purpose: Creates a virtual file in the system with the given path and content.

bl2vfs

Purpose: Reads and returns the content of a file stored in the virtual file system.

bl3vfs

Purpose: Deletes a file from the virtual file system.

bl4vfs

Purpose: Checks if a file exists in the virtual file system.

bl5

Purpose: Encrypts a file’s content using Base64 encoding.

bl6

Purpose: Decrypts a previously encrypted file.

bl8

Purpose: Converts the entire virtual file system into a Base64-encoded string.

bl9

Purpose: Restores the virtual file system from an exported Base64 string.

Thanks for getting bored by reading these

Made by Aman

package com.bbl.virtualfilesystem;

import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.errors.YailRuntimeError;
import com.google.appinventor.components.runtime.util.YailList;
import java.util.HashMap;
import java.util.Map;
import android.util.Base64;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import java.nio.charset.StandardCharsets;

public class VirtualFileSystem extends AndroidNonvisibleComponent {
    private final Map<String, String> vfs = new HashMap<>();

    public VirtualFileSystem(ComponentContainer container) {
        super(container.$form());
    }

    @SimpleFunction
    public void CreateFile(String path, String content) {
        vfs.put(path, content);
    }

    @SimpleFunction
    public String ReadFile(String path) {
        return vfs.getOrDefault(path, "File not found");
    }

    @SimpleFunction
    public void DeleteFile(String path) {
        vfs.remove(path);
    }

    @SimpleFunction
    public boolean FileExists(String path) {
        return vfs.containsKey(path);
    }

    @SimpleFunction
    public void EncryptFile(String path) {
        if (vfs.containsKey(path)) {
            String content = vfs.get(path);
            String encrypted = Base64.encodeToString(content.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
            vfs.put(path, encrypted);
        }
    }

    @SimpleFunction
    public void DecryptFile(String path) {
        if (vfs.containsKey(path)) {
            try {
                String encrypted = vfs.get(path);
                byte[] decodedBytes = Base64.decode(encrypted, Base64.DEFAULT);
                String decrypted = new String(decodedBytes, StandardCharsets.UTF_8);
                vfs.put(path, decrypted);
            } catch (Exception e) {
                vfs.put(path, "Decryption failed");
            }
        }
    }

    @SimpleFunction
    public String ExportVFS() {
        return Base64.encodeToString(vfs.toString().getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
    }

    @SimpleFunction
    public void ImportVFS(String encodedData) {
        try {
            byte[] decodedBytes = Base64.decode(encodedData, Base64.DEFAULT);
            String decodedString = new String(decodedBytes, StandardCharsets.UTF_8);
            vfs.clear();
            for (String entry : decodedString.substring(1, decodedString.length() - 1).split(", ")) {
                String[] keyValue = entry.split("=");
                if (keyValue.length == 2) {
                    vfs.put(keyValue[0], keyValue[1]);
                }
            }
        } catch (Exception e) {
            vfs.clear();
        }
    }
}

Interesting.

List files in VFS?
Folders/Sub folders in VFS?

I assume it is persistent over a reboot?
What about on uninstall of app?

I will surely add these in next update

1 Like

On uninstall, all data is deleted permanently. but I try If I modify it to save data to TinyDB or an actual file, the VFS can persist until the user clears app data.

2 Likes

Thank you for your contribution
Do you also provide an aix file to download?

Taifun