const fs = require("fs"); const path = require('path'); const JSZip = require("jszip"); const { log } = require("console"); const folderPath = "./configs" const start = () => { const files = fs.readdirSync(folderPath); // 过滤出所有的 JSON 文件 const jsonFiles = files.filter(file => path.extname(file) === '.json'); // 并行读取所有的 JSON 文件内容 const combinedJson = {} const jsonContents = jsonFiles.forEach(file => { const fileData = JSON.parse(fs.readFileSync(path.join(folderPath, file), 'utf8')) combinedJson[path.basename(file, '.json')] = fileData }); compress(JSON.stringify(combinedJson, null, 2)) } const compress = json => { const zip = new JSZip(); // 将JSON文件内容添加到ZIP实例中 zip.file('config.json', json, { compression: "DEFLATE", compressionOptions: { level: 9 } }); // 生成ZIP文件内容 zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true }) .pipe(fs.createWriteStream('config.xml')) .on('finish', function () { console.log('ZIP file created.'); }); } start()