123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- const fs = require('fs');
- const path = require("path");
- const archiver = require('archiver')
- // 获取当前命令行上下文路径
- const currentDirectory = process.cwd();
- const distJoinPath = path.join(currentDirectory, '/zip/');
- if(!fs.existsSync(distJoinPath)) {
- fs.mkdir(distJoinPath, (err)=>{
- if(err){
- console.log('zip文件夹创建失败')
- }else{
- console.log('zip文件夹创建成功')
- }
- })
- }
- // 创建文件输出流
- const distZipPath = path.join(currentDirectory, '/zip/archives.zip');
- let output = fs.createWriteStream(distZipPath)
- let archive = archiver('zip', {
- zlib: { level: 9 } // 设置压缩级别
- })
- // 文件输出流结束
- output.on('close', function() {
- console.log(`总共 ${archive.pointer()} 字节`)
- console.log('archiver完成文件的归档,文件输出流描述符已关闭')
- })
- // 数据源是否耗尽
- output.on('end', function() {
- console.log('数据源已耗尽')
- })
- // 存档警告
- archive.on('warning', function(err) {
- if (err.code === 'ENOENT') {
- console.warn('stat故障和其他非阻塞错误')
- } else {
- throw err
- }
- })
- // 存档出错
- archive.on('error', function(err) {
- throw err
- })
- // 通过管道方法将输出流存档到文件
- archive.pipe(output)
- const distPath = path.join(currentDirectory, '/dist/');
- //打包dist里面的所有文件和目录
- archive.directory(distPath, false)
- //完成归档
- archive.finalize()
|