Skip to content

Instantly share code, notes, and snippets.

@benloong
Forked from coclav/copyContent
Last active November 29, 2021 21:48
Show Gist options
  • Save benloong/586d6c899c2d84cea557d358311ae9d7 to your computer and use it in GitHub Desktop.
Save benloong/586d6c899c2d84cea557d358311ae9d7 to your computer and use it in GitHub Desktop.
Copy file out of ASAR archive for Electron app
var fs = require("fs");
var path = require("path")
var app = require("electron").remote.app;
// var fsj = require("fs-jetpack");
// example usage : copyFileOutsideOfElectronAsar( "myFolderInsideTheAsarFile", app.getPath("temp") + "com.bla.bla"
var copyFileOutsideOfElectronAsar = function (sourceInAsarArchive, destOutsideAsarArchive) {
if (fs.existsSync(app.getAppPath() + "/" + sourceInAsarArchive)) {
// file will be copied
if (fs.statSync(app.getAppPath() + "/" + sourceInAsarArchive).isFile()) {
let file = destOutsideAsarArchive
let dir = path.dirname(file);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(file, fs.readFileSync(app.getAppPath() + "/" + sourceInAsarArchive));
}
// dir is browsed
else if (fs.statSync(app.getAppPath() + "/" + sourceInAsarArchive).isDirectory()) {
fs.readdirSync(app.getAppPath() + "/" + sourceInAsarArchive).forEach(function (fileOrFolderName) {
copyFileOutsideOfElectronAsar(sourceInAsarArchive + "/" + fileOrFolderName, destOutsideAsarArchive + "/" + fileOrFolderName);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment