Is there a way to zip files using JavaScript?? For an example, like in Yahoo mail, when you chose to download all the attachments from an email, it gets zipped and downloaded in a single zip file. Is JavaScript capable of doing that? If so, please provide a coding example.
I found this library called jszip to do the task but it has known and unresolved issues.
How do I solve the problem?
js" import FileSaver from "file-saver" const content = await downloadZip(files). blob() FileSaver. saveAs(content, "download. zip");
The zip() function is used to combine the values of given array with the values of original collection at a given index. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.
var zip = new JSZip(); // Add a text file with the contents "Hello World\n" zip. file("Hello. txt", "Hello World\n"); // Add a another text file with the contents "Goodbye, cruel world\n" zip. file("Goodbye.
In your computer's files, choose the folder you'd like to zip/compress. Right-click the folder, choose Send to, and then click Compressed (zipped) folder. A new zipped folder will appear in the same location as your original folder. This Zip File can now be used for your HTML drop.
JSZip has been updated over the years. Now you can find it on its GitHub repo
It can be used together with FileSaver.js
You can install them using npm:
npm install jszip --save npm install file-saver --save
And then import and use them:
import JSZip from 'jszip'; import FileSaver from 'file-saver'; const zip = new JSZip(); zip.file('idlist.txt', 'PMID:29651880\r\nPMID:29303721'); zip.generateAsync({ type: 'blob' }).then(function (content) { FileSaver.saveAs(content, 'download.zip'); });
Then you will download a zip file called download.zip, once you've extracted it, and you can find inside a file called idlist.txt, which has got two lines:
PMID:29651880 PMID:29303721
And for your reference, I tested with the following browsers, and all passed:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With