// Export each model queue.push( models[model].findAll() .then((rows) => { let data = []; if(rows.length) { rows.forEach((row) => data.push(row.toJSON())); } // Write data to zip file zip.folder('data').file(model + '.json', JSON.stringify(data, null, 2)); }) .catch((err) => next(err)) );
/* 压缩文件 读取目录及文件 */ function readDir(obj, nowPath, laodPath) { let files = fs.readdirSync(nowPath); //读取目录中的所有文件及文件夹(同步操作) files.forEach(function(fileName, index) { //遍历检测目录中的文件 let fillPath = nowPath + "/" + fileName; let file = fs.statSync(fillPath); //获取一个文件的属性 if (file.isDirectory()) { //如果是目录的话,继续查询 let pushPath = fillPath.replace(laodPath, '') let dirlist = zip.folder(pushPath); //压缩对象中生成该目录 readDir(dirlist, fillPath, laodPath); //重新检索目录文件 } else { obj.file(fileName, fs.readFileSync(fillPath)); //压缩目录添加文件 } }); }
function packfiles (directory) { const files = fs.readdirSync(directory) for (let i = 0; i < files.length; i++) { const file = files[i] const filepath = path.join(directory, file) let invalid = false for (let j = 0; j < ignoreFiles.length; j++) { const ignoreFile = path.join(__dirname, '..', ignoreFiles[j]).replace(/{\.}/g, '.') if (filepath.substr(0, ignoreFile.length) === ignoreFile) { invalid = true break } } if (invalid) { continue } const stat = fs.statSync(filepath) const relative = path.relative(rootDir, filepath).replace(/\\/g, '/') if (stat.isDirectory()) { zipArchive.folder(relative) packfiles(filepath) } else { zipArchive.file(relative, fs.readFileSync(filepath)) } } }