gildas-lormeau/zip.js

Taking time to load

Panjady opened this issue · 3 comments

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Unzip and Form URL Example</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
    <script src="zip.js"></script>
    <script src="epub.js"></script>
   
</head>
<body>
   
    <div id="epubArea" style="height: 90%; width: 90%">
        <button type="button" id="prev">Previous page</button>
        <button type="button" id="next">Next page</button>
    </div>
 
    <script>
             let entries = {};
 
async function verifyZipPassword(blob, password) {
    try {
        console.log('Starting password verification...');
        const reader = new zip.ZipReader(new zip.BlobReader(blob), { password });
        entries = await reader.getEntries();
 
        for (const entry of entries) {
            try {
                 entry.getData(new zip.BlobWriter());
            } catch (error) {
                if (error.message === zip.ERR_ENCRYPTED || error.message === zip.ERR_INVALID_PASSWORD) {
                    console.log('Invalid password or encrypted ZIP file.');
                    return false;
                } else {
                    throw error;
                }
            }
        }
       
        return true;
    } catch (error) {
        console.error('Error during password verification:', error);
       
        return false;
    }
}
 
 
async function passwordCheck() {
    const zipPath = 'BLS PM eBook (978-1-61669-799-0_1).zip';
    const password = 'blspm';
   
    try {
        const response = await fetch(zipPath);
        console.log("response" , response);
        alert(response)
        const blob = await response.blob();
        console.log("blob", blob);
        alert(blob)
        
        const passwordCorrect = await verifyZipPassword(blob, password);
        if(!passwordCorrect){
            alert("wrong password");
        }
        if (passwordCorrect) {
           
       
            const newZip = new zip.ZipWriter(new zip.BlobWriter(), { level: 9 });
       
            await Promise.all(entries.map(async (entry) => {
                const entryData = await entry.getData(new zip.BlobWriter());
                await newZip.add(entry.filename, new zip.BlobReader(entryData));
            }));
           
            const decryptedFile = await newZip.close();
            console.log("decryptedFile" , decryptedFile);
            const book = ePub(decryptedFile);
            const rendition = book.renderTo("epubArea", {
                width: 600,
                height: 400,
                allowScriptedContent: true
            });
            rendition.display();
            document.getElementById("next").addEventListener("click", () => {
        rendition.next();
        console.log(book)
 
 
      });
 
      document.getElementById("prev").addEventListener("click", () => {
        rendition.prev();
     
 
      });
 
        } else {
            console.error('Invalid password or encrypted ZIP file.');
        }
    } catch (error) {
        console.error('Error:', error);
    }
}
passwordCheck();
    </script>
</body>
</html> 

Hi this is my code to check password , i am using client side to extract password protected zip file and zipping it and loading it into epubjs . Can i emit zipping the file again , Is there any related method to check password and load ?

Hi can anyone help on this

Can anyone help on this ?

You could use the FS API for this, see this simple example of code below:

const zipFs = new zip.fs.FS();
await zipFs.importHttpContent(zipPath, { password });
const blob = await zipFs.exportBlob();