Unable to update a file

Viewed 34

I have a file which I am updating using fs and then creating zip in other location. When I check, file update is working fine, but in zip updated contents are not there. Can you tell me what I am doing wrong here. Here is my code.

const content = "new content";
const outputFile = `${unzipDir}/output.docx`
const zip = new AdmZip(outputFile);
fs.writeFileSync(`${unzipDir}/word/document.xml`,content); //content updated successfully in this path.
zip.addLocalFolder(`${unzipDir}/_rels/`);
zip.addLocalFolder(`${unzipDir}/customXml/`);
zip.addLocalFolder(`${unzipDir}/docProps/`);
zip.addLocalFolder(`${unzipDir}/word/`);
zip.addLocalFile(`${unzipDir}/[Content_Types].xml`);
zip.writeZip(outputFile);//old content is showing when extracting zip
1 Answers

Finally I got the solution. When we try to add local folders like I have done in above code, it not add folders and instead add all files containing those folders to root. So in that case file from original location was not replaced and I got old contents. So instead of adding all folders one-by-one, I have added entire folder structure at once and it works for me.

//zip.addLocalFolder(`${unzipDir}/_rels/`);
//zip.addLocalFolder(`${unzipDir}/customXml/`);
//zip.addLocalFolder(`${unzipDir}/docProps/`);
//zip.addLocalFolder(`${unzipDir}/word/`);
//commented above lines and added below line
zip.addLocalFolder(`${unzipDir}/`);

Hope this will help others also.

Related