问题描述
有没有办法使用JavaScript压缩文件?例如,在Yahoo邮件中,当您选择从电子邮件下载所有附件时,它会被压缩并下载到单个zip文件中。 JavaScript能够做到这一点吗?若是,请提供编码示例。
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.
我发现这个名为中找到它
JSZip has been updated over the years. Now you can find it on its GitHub repo
它可以与一起使用
您可以使用npm安装它们:
You can install them using npm:
npm install jszip --save
npm install file-saver --save
然后导入并使用它们:
import JSZip from 'jszip';
import FileSaver from 'file-saver';
let zip = new JSZip();
zip.file("idlist.txt", `PMID:29651880\r\nPMID:29303721`);
zip.generateAsync({type: "blob"}).then(function(content) {
FileSaver.saveAs(content, "download.zip");
});
然后你将下载一个名为download.zip的zip文件,一旦你解压缩它,你就会可以找到一个名为idlist.txt的文件,里面有两行:
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:
- Firefox 59.0.2(Windows 10)
- Chrome 65.0.3325.181(Windows 10)
- Microsoft Edge 41.16299.371.0(Windows 10)
- Internet Explorer 11.0.60(Windows 10)
- Opera 52(Mac OSX) 10.13)
- Safari 11(Mac OSX 10.13)
- Firefox 59.0.2 (Windows 10)
- Chrome 65.0.3325.181 (Windows 10)
- Microsoft Edge 41.16299.371.0 (Windows 10)
- Internet Explorer 11.0.60 (Windows 10)
- Opera 52 (Mac OSX 10.13)
- Safari 11 (Mac OSX 10.13)
这篇关于如何使用JavaScript压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!