1 year ago

#333011

test-img

T Mack

Unzip a MacOS .app file in Electron using Node.js

I am trying to unzip a file called Restart.Manager.zip which contains a single item, Restart Manager.app. This code seems to unzip the file correctly but upon launching the outputted .app folder, I get an error "The application “Restart Manager” can’t be opened."

const JSZip = require('jszip');
const fs = require('fs');
const jetpack = require('fs-jetpack');
const originalFs = require('original-fs');

async function extractZip(filePath, destination) {
  fs.readFile(filePath, function(err, data) {
    if (!err) {
      var zip = new JSZip();
      zip.loadAsync(data).then(function(contents) {
        Object.keys(contents.files).forEach(function(filename) {
          const file = zip.file(filename);
          if (file) {
            file.async('nodebuffer').then(function(content) {
              var dest = destination + '/' + filename;
              if (filename.endsWith('.asar')) {
                originalFs.writeFileSync(dest, content)
              } else {
                jetpack.write(dest, content);
              }
            });
          }
        });
      });
    }
  });
};

extractZip('/Users/me/Desktop/Restart.Manager.zip', '/Users/me/Desktop')

Manually unzipping the .zip file creates a working .app so I'm not sure where the code is messing up.

Here is the file on GitHub releases for testing: https://github.com/itw-creative-works/restart-manager-download-server/releases/download/installer/Restart.Manager.zip but feel free to use your own zipped .app file (although it should probably be an Electron app in which case you can find one here https://www.electronjs.org/apps)

I have tried zipping things like a .png and it unzips fine, which makes me think it is having problems with .app files or possibly the fact that the .app contains a .asar file which Electron supposedly has problems handling when it comes to the fs module: https://github.com/electron/electron/issues/1658

node.js

electron

unzip

0 Answers

Your Answer

Accepted video resources