Skip to content

Commit

Permalink
Merge pull request #29 from BloomBooks/BL12009_FixUnzipDir
Browse files Browse the repository at this point in the history
Fix unzipping directories (BL-12009)
  • Loading branch information
andrew-polk authored Nov 22, 2023
2 parents b225b8f + d8cb702 commit 88ec9c5
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ ipcMain.on("unpack-zip-file", (event, zipFilePath) => {
return;
}

// Keep a list for optimization, so we don't have to keep calling fs.mkdirSync.
const directories: Set<string> = new Set<string>();
// Seed it with the root directory, since we know that exists.
directories.add(unpackedFolder);

jszip.loadAsync(data).then(function (zip) {
Promise.all(
Object.keys(zip.files).map(function (filename) {
Expand All @@ -219,6 +224,14 @@ ipcMain.on("unpack-zip-file", (event, zipFilePath) => {
Promise.all(
files.map(function (file) {
const dest = Path.join(unpackedFolder, file.filename);

// Ensure the directory exists (create it if not)
const dir = Path.dirname(dest);
if (!directories.has(dir)) {
fs.mkdirSync(dir, { recursive: true });
directories.add(dir);
}

return new Promise<void>(function (resolve, reject) {
fs.writeFile(dest, file.content, function (err) {
if (err) reject(err);
Expand Down

0 comments on commit 88ec9c5

Please sign in to comment.