utils.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import fs from 'fs';
  2. import archiver from 'archiver';
  3. import { exec } from 'child_process';
  4. import { readdir, stat } from 'fs/promises';
  5. export function readFileRange(file, start, length) {
  6. return new Promise((resolve, reject) => {
  7. fs.open(file, 'r', (err, fd) => {
  8. if (err) {
  9. reject('Error opening file:', er);
  10. return;
  11. }
  12. const buffer = Buffer.alloc(length);
  13. fs.read(fd, buffer, 0, length, start, (err, bytesRead, buffer) => {
  14. if (err) {
  15. reject('Error reading file:', err)
  16. return;
  17. }
  18. fs.close(fd, (err) => {
  19. if (err) {
  20. reject('Error closing file:', err)
  21. return ;
  22. }
  23. resolve(buffer);
  24. });
  25. });
  26. });
  27. })
  28. }
  29. export function pad(num, n) {
  30. var strNum = num.toString();
  31. var len = strNum.length;
  32. while (len < n) {
  33. strNum = "0" + strNum;
  34. len++;
  35. }
  36. return strNum;
  37. }
  38. export function execAsync(command) {
  39. return new Promise((resolve, reject) => {
  40. console.log('[CMD] ' + command);
  41. exec(command, (error, stdout, stderr) => {
  42. if (error) {
  43. reject(error);
  44. return;
  45. }
  46. resolve(stdout);
  47. });
  48. })
  49. }
  50. export async function compressZip(dir, targetFilePath, skipFiles) {
  51. const output = fs.createWriteStream(targetFilePath);
  52. const archive = archiver('zip', {
  53. zlib: { level: 9 } // Sets the compression level.
  54. });
  55. archive.pipe(output);
  56. function checkPathSkip(pathString) {
  57. if (!pathString)
  58. return true;
  59. if (pathString.startsWith('.') || pathString.startsWith('/.'))
  60. return true;
  61. if (!skipFiles || skipFiles.length === 0)
  62. return;
  63. return skipFiles.find((item) => pathString.startsWith(item) || pathString.startsWith(`/${item}`));
  64. }
  65. async function loopDir(path, subPrefix) {
  66. const files = await readdir(path);
  67. for (const file of files) {
  68. const subPath = subPrefix + '/' + file;
  69. if (checkPathSkip(subPath))
  70. continue;
  71. console.log('File: ' + subPath);
  72. const filestat = await stat(dir + subPath);
  73. if (filestat.isDirectory()) {
  74. await loopDir(dir + subPath, subPath);
  75. } else {
  76. archive.file(dir + subPath, { name: subPath });
  77. }
  78. }
  79. }
  80. await loopDir(dir, '')
  81. console.log('等待压缩zip...');
  82. const waitArchive = new Promise((resolve, reject) => {
  83. output.on('close', function() {
  84. console.log(archive.pointer() + ' total bytes');
  85. console.log('archiver has been finalized and the output file descriptor has closed.');
  86. resolve();
  87. });
  88. archive.on('error', function(err) {
  89. reject(err);
  90. });
  91. })
  92. await archive.finalize();
  93. await waitArchive;
  94. }