yarn add jszip
yarn add file-saver
import Zip from 'jszip'
import FileSave from 'file-saver'
var zip = new Zip()
zip.file("test.log","文件内容");
zip.folder("path").file("test2.log","test2.log 的文件内容")
//图片
var arr = [
{path:"https://uieditor.ui.cn/editor/lib/10/4.png"},
{path:"https://uieditor.ui.cn/editor/lib/10/3.png"}
]
const downloaded = await Promise.all(
arr.map(res => {
return fetch(res.url)
.then(resp => resp.blob())
.then(data => {
return {
...res,
data
};
});
})
)
zip.file("test.png",downloaded[0].data,{binary:true})
打包压缩 并保存到本地
zip.generateAsync({ type: "blob" }).then(res=>{
console.log(res)
FileSaver.saveAs(res,"test.zip")
});
读取压缩文件内容
function readzip(e){
var zipFile=new JSZip();
zipFile.loadAsync(e.target.files[0]).then(function(zip){
zip.folder("a").file("a.txt").async("string").then(function(res){
console.log(res)
})
})
}