nodejs 打包exe 文件,及压缩 zip文件

原创
2019/10/18 14:41
阅读数 2.3K
pkg  node.js打包生成exe应用程序

let fs = require("fs");//获取文件系统模块,负责读写文件
 2 let path = require("path");//工具模块,处理文件路径的小工具
 3 let JSZIP = require("jszip");
 4 let zip = new JSZIP();
 5 
 6 //读取目录及文件
 7 function readDir(obj, nowPath) {
 8     let files = fs.readdirSync(nowPath);//读取目录中的所有文件及文件夹(同步操作)
 9     files.forEach(function (fileName, index) {//遍历检测目录中的文件
10         console.log(fileName, index);//打印当前读取的文件名
11         let fillPath = nowPath + "/" + fileName;
12         let file = fs.statSync(fillPath);//获取一个文件的属性
13         if (file.isDirectory()) {//如果是目录的话,继续查询
14             let dirlist = zip.folder(fileName);//压缩对象中生成该目录
15             readDir(dirlist, fillPath);//重新检索目录文件
16         } else {
17             obj.file(fileName, fs.readFileSync(fillPath));//压缩目录添加文件
18         }
19     });
20 }
21 
22 //开始压缩文件
23 function startZIP() {
24     var currPath = __dirname;//文件的绝对路径 当前当前js所在的绝对路径
25     var targetDir = path.join(currPath, "JsonMerge");
26     readDir(zip, targetDir);
27     zip.generateAsync({//设置压缩格式,开始打包
28         type: "nodebuffer",//nodejs用
29         compression: "DEFLATE",//压缩算法
30         compressionOptions: {//压缩级别
31             level: 9
32         }
33     }).then(function (content) {
34         fs.writeFileSync(currPath + "/result.zip", content, "utf-8");//将打包的内容写入 当前目录下的 result.zip中
35     });
36 }
37 
38 startZIP();
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部