Files
factorio-clusterio/packages/lib/src/zip_ops.ts
T
Hornwitser aeac7eedec zip_ops: Deduce root folder from first zip entry (#739)
Some mods, for example cubium, includes bogus folders like __MACOSX in
addition to the root folder where everything is located.  This only
works by chance as Factorio will check for the folder of the first
entry in the zip file and use that.

Match Factorio's logic in findRoot so that these mods work with
Clusterio.
2025-03-21 23:00:30 +00:00

28 lines
750 B
TypeScript

import type JSZip from "jszip";
/**
* Returns the root folder in the zip file
*
* Returns the name of the folder that the first entry in the zip file is
* contained in. Throws an error if this is not in a folder.
*
* This matches the logic Factorio uses when determining the folder to look
* for content in a zip file.
*
* @param zip - Zip to search through.
* @returns name of the root folder.
*/
export function findRoot(zip: JSZip) {
const relativePath = Object.keys(zip.files)[0];
if (relativePath === undefined) {
throw new Error("Empty zip file");
}
let index = relativePath.indexOf("/");
if (index === -1) {
throw new Error(`Zip contains file '${relativePath}' in root dir`);
}
return relativePath.slice(0, index);
}