Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid package error on unzip #9304

Closed
jonbarrow opened this issue Apr 27, 2017 · 3 comments
Closed

Invalid package error on unzip #9304

jonbarrow opened this issue Apr 27, 2017 · 3 comments

Comments

@jonbarrow
Copy link

jonbarrow commented Apr 27, 2017

image of the error

  • Electron version: Latest
  • Operating system: Windows 10

Expected behavior

When extracting a zip file that contains another electron app (or any .asar file), the file should extract normally with no errors.

Actual behavior

When extracting a zip file that contains another electron app (or any .asar file), Electron throws a Invalid package error and does not extract the .asar files at all (all other files are extracted correctly. Only the .asar files do not extract), rendering the extracted app useless.

How to reproduce

Download a zip file containing an electron app and try to extract it using Node/Electron.

Alright now that the format is filled out I'll explain what's going on a bit more. I am attempting to write my own "update checker" script for my electron app, which does not require Squirrel, however when extracting the downloaded zip file (which is a zip file containing the updated electron app data), Electron throws an error saying there is an Invalid package and refuses to extract the .asar files (all other files are extracted correctly. Only the .asar files do not extract).

I would prefer to not use Squirrel as, from my understanding, Squirrel requires the user to install the application. I would prefer to keep my application as portable as possible, so that users can simply download the latest build, extract it and run.

I am using these two functions to download and extract the update files:

function downloadFile(url, target, fileName, cb) { // Downloads
    var req = request({
        method: 'GET',
        uri: url
    });

    var out = fs.createWriteStream(target+'/'+fileName);
    req.pipe(out);

    req.on('end', function() {
        unzip(target+'/'+fileName, target, function() {
            if (cb) {
                cb();
            }
        });
    });
}
function unzip(file, target, cb) { // Unzips

    var out = fs.createReadStream(file);
    out.pipe(unzipper.Extract({ path: target })).on('finish', function () {
        dialog.showMessageBox({
            type: 'question',
            message: 'Finished extracting to `'+target+'`'
        });

        if (cb) {
            cb();
        }
    });
}

And I am calling the update via:

downloadFile('http://example.com/update.zip', path.join(__dirname, './'), 'update.zip', function() {
// http://example.com/update.zip is not the real source
    app.relaunch();
    app.quit();
});

Using the unzipper Node package (https://www.npmjs.com/package/unzipper) to handle extracting.

I have tried https://www.npmjs.com/package/electron-basic-updater, which does not throw the JavaScript error, but it still does not extract the .asar files at all, which means the "update" is still useless.

This script works perfectly fine for other zip files, it only throws the error when downloading the app updates. I have tried nearly every unzipping package I could find, but all throw errors when trying to extract the updates.

Also it should be noted that the files in the zip file are indeed functional. Extracting the zip file using WinRAR yields no errors and the app runs as normal. The error is only thrown, and the app fails to extract, when being extracted by another Electron app.

Due to the fact that it only happens when working with Electron, and the error is only thrown when unzipping an Electron app, I'm inclined to believe it's something to do with Electron itself and not the Node modules. Hence this issue report.

@MarshallOfSound
Copy link
Member

@RedDuckss This is due to the electron fs module treating asar files as directories rather than files. To make the unzip process work you need to do one of two things

  • Set process.noAsar = true
  • Use original-fs instead of fs

@oe
Copy link

oe commented Feb 24, 2018

An easy way of workaround: rename .asar file with a different extension; rename to extension .asar after unzipping

@gtkpr
Copy link

gtkpr commented Jan 19, 2020

the .asar file type also throws an invalid package error when fsp.stat(path.asar) or stats.size

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants