| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 | 
							- const fs = require('fs')
 
- const path = require('path')
 
- const archiver = require('archiver')
 
- const dayjs = require('dayjs')
 
- /**
 
-  * 说明:压缩后的命名格式:RESIST_2023-09-09 23_40.zip
 
-  */
 
- const currentDateTime = dayjs().format('YYYY-MM-DD HH_mm')
 
- /**
 
-  * 说明:输出的路径及文件名
 
-  * tips:是相对与package.json的路径,因为是在package.json中调用的scripts/zip.js
 
-  */
 
- const file_path = `./dist/RESIST_${currentDateTime}.zip`
 
- // 检查文件是否存在,是的话则删除后再压缩
 
- if (fs.existsSync(file_path)) {
 
-  console.log(`${file_path} already exists. Deleting the file...`)
 
-  fs.unlinkSync(file_path)
 
- }
 
- const output = fs.createWriteStream(file_path)
 
- const archive = archiver('zip', {
 
-  zlib: { level: 9 }, // 设置压缩级别
 
- })
 
- output.on('close', function() {
 
-  console.log(archive.pointer() + ' total bytes')
 
-  console.log(`RESIST_${currentDateTime}.zip has been created successfully.`)
 
- })
 
- output.on('end', function() {
 
-  console.log('Data has been drained')
 
- })
 
- archive.on('warning', function(err) {
 
-  if (err.code === 'ENOENT') {
 
-   console.warn(err)
 
-  } else {
 
-   throw err
 
-  }
 
- })
 
- archive.on('error', function(err) {
 
-  throw err
 
- })
 
- // 将要压缩的文件夹(相对于此文件的路径)添加到压缩包,解压后的名字仍为RESIST
 
- archive.directory(path.join(__dirname, '../dist/RESIST'), 'RESIST')
 
- // 完成压缩并写入到文件流中
 
- archive.pipe(output)
 
- archive.finalize()
 
 
  |