Sometimes, we want to zip files using JavaScript.
In this article, we’ll look at how to zip files using JavaScript.
How to zip files using JavaScript?
To zip files using JavaScript, we use the jszip
and file-saver
packages.
To install them, we run
npm install jszip --save
npm install file-saver --save
Then we write
import JSZip from "jszip";
import FileSaver from "file-saver";
const zip = new JSZip();
zip.file("idlist.txt", "PMID:29651880\r\nPMID:29303721");
(async () => {
const content = await zip.generateAsync({ type: "blob" });
FileSaver.saveAs(content, "download.zip");
})();
to call zip.file
to zip the files listed in the path arguments.
Then we call generateAsync
to return a promise with the blob of the zip file.
Finally we call FileSaver.saveAs
to save content
as 'download.zip'
.
Conclusion
To zip files using JavaScript, we use the jszip
and file-saver
packages.