Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract password protected zip file using javascript (client side)

There are many excellent libraries to open zip files on the client as: zip.js, jszip etc ... But I could not find any library that can open an encrypted zip file. is there a solution to open a zip file on the client side (in the browser)?

like image 905
maori danii Avatar asked Nov 01 '22 11:11

maori danii


1 Answers

Zip.js supports encryption. Try it on the demo: https://gildas-lormeau.github.io/zip.js/demos/demo-read-file.html. Here is below an example of how to check the password of a zip file (provided as a Blob object) is valid or not.

const verifyZipPassword = async (file, password) => {
    let reader;
    try {
        reader = new zip.ZipReader(new zip.BlobReader(file), { password });
        const entries = await reader.getEntries();
        for (const entry of entries) {
            try {
                await entry.getData(new zip.BlobWriter());
            } catch (error) {
                if (error.message === zip.ERR_ENCRYPTED ||
                    error.message === zip.ERR_INVALID_PASSWORD) {
                    return false;
                } else {
                    throw error;
                }
            }
        }
    } finally {
        await reader.close();
    }
    return true;
};
like image 176
Bao To Quoc Avatar answered Nov 09 '22 09:11

Bao To Quoc