I am trying to do the followings:
- use JSZip.loadAsync to read a zip file
- update some files inside the zip file
- generate an updated zip file and download
Here is an example of my code:
async function updatezipfile(){
var zip = await JSZip.loadAsync('oldfile.zip');
zip.forEach(function (relativePath, zipEntry) {
if (zipEntry.name.indexOf('abc') > -1) {
//update the file
zip.file(relativePath, updatedcontent);
}
zip.generateAsync({ type: "blob" }).then(function (blob) {
saveAs(blob, 'newfile.zip');
});
}
The structure of oldfile.zip is:
- abc_1.txt
- abc_2.txt
- abc_3.txt
- xyz.txt
The problem is - 'zip.file(relativePath, updatedcontent)' is updating the zip file correctly(I think), but the new file that 'zip.generateAsync' generates is not updated.
I believe it should be something simple but I just have no idea. Your help will be much appreciated
Cheers
I have tried putting 'zip.generateAsync' in different location, using Promise and counter but none of them works. See this request