Something went wrong. Try again.
A Discord bot that does Osu renders with Danser. Not built for scale, but instead small friend-group servers.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import { mkdir, writeFile } from "node:fs/promises";import path from "node:path";import { unzipSync } from "fflate";
// Every zip archive starts with the local file header signature "PK\x03\x04".const zipSignature = [0x50, 0x4b, 0x03, 0x04];
/**Whether `bytes` begins with a zip local file header.*/export function isZipArchive(bytes: Uint8Array): boolean { return zipSignature.every((byte, index) => bytes[index] === byte);}
export interface ExtractedFile { /** The entry's path inside the archive. */ name: string; /** Where it was written. */ path: string; bytes: Uint8Array;}
export interface ExtractZipOptions { /** Entries for which this returns false are neither written nor returned. */ filter?: (name: string) => boolean;}
/**Unpacks a zip archive in memory and writes its files under `directory`,creating subdirectories as needed. An entry whose path would land outside`directory` fails the whole extraction.*/export async function extractZip( archive: Uint8Array, directory: string, { filter = () => true }: ExtractZipOptions = {},): Promise<ExtractedFile[]> { const root = path.resolve(directory);
const entries = unzipSync(archive, { filter: (file) => !file.name.endsWith("/") && filter(file.name), });
const files: ExtractedFile[] = [];
for (const [name, bytes] of Object.entries(entries)) { const target = path.resolve(root, name.replaceAll("\\", "/"));
if (!target.startsWith(root + path.sep)) { throw new Error(`zip entry "${name}" escapes the target directory`); }
await mkdir(path.dirname(target), { recursive: true }); await writeFile(target, bytes); files.push({ name, path: target, bytes }); }
return files;}