This is actually pretty neat! You can expand it by changing listPages to listFiles, so it will attempt to download everything:
```space-lua
-- ==============================================================
-- 100% Offline Space Exporter (.ZIP with Exact Folder Structure)
-- ==============================================================
command.define {
name = "Space: Export to Zip",
run = function()
editor.flashNotification("Collecting local notes from phone...", "info")
local allFiles = space.listFiles()
local files = {}
-- 1. Read all files currently cached in IndexedDB
for f in each(allFiles) do
if not string.startsWith(f.name, "Library/Std") then
table.insert(files, {
name = f.name,
content = space.readFile(f.name),
})
end
end
editor.flashNotification("Packaging " .. #files .. " files into .zip...", "info")
-- 2. Pure client-side ZIP generator (runs in phone browser memory)
local zipExporter = js.window.Function([[
const files = arguments[0];
// Standard CRC32 table
const crcTable = [];
for (let n = 0; n < 256; n++) {
let c = n;
for (let k = 0; k < 8; k++) c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
crcTable[n] = c;
}
function crc32(buf) {
let crc = -1;
for (let i = 0; i < buf.length; i++) crc = (crc >>> 8) ^ crcTable[(crc ^ buf[i]) & 0xFF];
return (crc ^ -1) >>> 0;
}
const enc = new TextEncoder();
const entries = [];
let offset = 0;
for (const f of files) {
const nameBytes = enc.encode(f.name);
const dataBytes = f.content;
const crc = crc32(dataBytes);
const size = dataBytes.length;
// Local header (PK\x03\x04)
const h = new Uint8Array(30 + nameBytes.length);
const v = new DataView(h.buffer);
v.setUint32(0, 0x04034b50, true);
v.setUint16(4, 20, true);
v.setUint16(6, 0x0800, true); // UTF-8 filename flag
v.setUint16(8, 0, true); // Store (no compression)
v.setUint32(14, crc, true);
v.setUint32(18, size, true);
v.setUint32(22, size, true);
v.setUint16(26, nameBytes.length, true);
h.set(nameBytes, 30);
entries.push({ nameBytes, crc, size, offset, header: h, data: dataBytes });
offset += h.length + size;
}
// Central directory headers (PK\x01\x02)
let cdSize = 0;
const cdHeaders = [];
for (const e of entries) {
const ch = new Uint8Array(46 + e.nameBytes.length);
const v = new DataView(ch.buffer);
v.setUint32(0, 0x02014b50, true);
v.setUint16(4, 20, true);
v.setUint16(6, 20, true);
v.setUint16(8, 0x0800, true);
v.setUint16(10, 0, true);
v.setUint32(16, e.crc, true);
v.setUint32(20, e.size, true);
v.setUint32(24, e.size, true);
v.setUint16(28, e.nameBytes.length, true);
v.setUint32(42, e.offset, true);
ch.set(e.nameBytes, 46);
cdHeaders.push(ch);
cdSize += ch.length;
}
// End of central directory record (PK\x05\x06)
const eocd = new Uint8Array(22);
const ev = new DataView(eocd.buffer);
ev.setUint32(0, 0x06054b50, true);
ev.setUint16(8, entries.length, true);
ev.setUint16(10, entries.length, true);
ev.setUint32(12, cdSize, true);
ev.setUint32(16, offset, true);
// Assemble blob
const parts = [];
for (const e of entries) { parts.push(e.header); parts.push(e.data); }
for (const ch of cdHeaders) parts.push(ch);
parts.push(eocd);
const blob = new Blob(parts, { type: "application/zip" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "SilverBullet-Backup-" + new Date().toISOString().slice(0, 10) + ".zip";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(() => URL.revokeObjectURL(url), 10000);
]])
zipExporter(files)
editor.flashNotification("Download started! Check your phone files.", "info")
end
}
```
Files which aren't cached will then be read as zero-size files, but the added side benefit to this is that the full list of filenames will be preserved.