zip.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const fs = require('fs');
  2. const path = require("path");
  3. const archiver = require('archiver')
  4. // 获取当前命令行上下文路径
  5. const currentDirectory = process.cwd();
  6. const distZipPath = path.join(currentDirectory, '/zip/client.zip');
  7. // 创建文件输出流
  8. let output = fs.createWriteStream(distZipPath)
  9. let archive = archiver('zip', {
  10. zlib: { level: 9 } // 设置压缩级别
  11. })
  12. // 文件输出流结束
  13. output.on('close', function() {
  14. console.log(`总共 ${archive.pointer()} 字节`)
  15. console.log('archiver完成文件的归档,文件输出流描述符已关闭')
  16. })
  17. // 数据源是否耗尽
  18. output.on('end', function() {
  19. console.log('数据源已耗尽')
  20. })
  21. // 存档警告
  22. archive.on('warning', function(err) {
  23. if (err.code === 'ENOENT') {
  24. console.warn('stat故障和其他非阻塞错误')
  25. } else {
  26. throw err
  27. }
  28. })
  29. // 存档出错
  30. archive.on('error', function(err) {
  31. throw err
  32. })
  33. // 通过管道方法将输出流存档到文件
  34. archive.pipe(output)
  35. const distPath = path.join(currentDirectory, '/dist/');
  36. //打包dist里面的所有文件和目录
  37. archive.directory(distPath, false)
  38. //完成归档
  39. archive.finalize()