//示例 files = [{name: 'xxxx.js',url:'https://xx/xx/xxxx.js'}] let dirPath = path.resolve(__dirname, '文件存放的本地位置')
mkdir(dirPath);
let tmps = files.map((item,index) => {
let stream = fs.createWriteStream(path.resolve(dirPath, item.name));
return new Promise((resolve,reject)=>{
try {
request(item.url).pipe(stream).on("close", function (err) {
console.log("文件[" + item.name + "]下载完毕");
resolve({
url: path.resolve(dirPath, item.name),
name: item.name
})
});
} catch (e) {
reject(e||'')
}
})
});
return new Promise((res,rej)=>{
Promise.all(tmps).then((result) => {
console.log(result)
res(result)
}).catch((error) => {
console.log(error||'')
})
})
}
//创建文件夹目录
function mkdir(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
console.log("文件夹创建成功");
} else {
console.log("文件夹已存在");
}
}
将下载到本地的文件打包为一个zip文件,可以参照 Quick Start 中的api组合使用
// append files from a sub-directory, putting its contents at the root of archive
archive.directory('subdir/', false);
//要注意第二个参数false,这个参数代表打包后的文件相对于output的目录结构,不写这个参数代表按照第一个参数('subdir/')的目录层级打包之后的文件位置是在本地位置,此时在推送到前端页面中下载url需要组装成http或https的地址









