You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js Archiver报错archive already finalizing,多文件夹压缩求助

问题分析与修复方案

错误原因

出现ArchiverError: archive already finalizing的核心原因是全局复用了同一个archiver实例。第一次调用archive.finalize()后,该实例进入终态无法再次使用,后续复用它处理新压缩任务时就会触发错误。

修复代码

将archiver实例的创建逻辑移到makeZip函数内部,让每个压缩任务使用独立实例,同时完善Promise的错误处理:

const fs = require("fs");
const archiver = require("archiver");
import { format } from "date-fns";

const exclude = ["assets/**", "**/*.json"];

export const makeZip = (srcFolder: string, rootPath: string): Promise<void> =>
  new Promise((resolve, reject) => {
    // 关键:每个压缩任务创建独立的archiver实例
    const archive = archiver("zip");
    
    const timestamp = format(new Date(), "MM-DD-YYYY_HH-mm-ssZZ");
    const dirParts = srcFolder.split("/");
    const dirName = dirParts[dirParts.length - 1];
    const zipName = `${timestamp}_${dirName}`;
    const zipPath = `${rootPath}/build/${zipName}.zip`;

    const output = fs.createWriteStream(zipPath);
    
    output.on("close", function () {
      console.log("done writing: " + archive.pointer() + " total bytes");
      resolve();
    });

    archive.pipe(output);

    // 错误处理:用reject传递错误,避免未捕获异常
    archive.on("error", function (err: Error) {
      reject(err);
    });

    archive.glob("**/*", {
      cwd: srcFolder,
      ignore: exclude,
    });

    archive.finalize();
  });

// 原zipFolders函数无需修改
const zipFolders = async (
  rootPath: string,
  dirPaths: string[]
): Promise<void> => {
  for await (const folder of dirPaths) {
    await makeZip(folder, rootPath);
  }
};

关键改动点

  1. 移除全局archiver实例:把const archive = archiver("zip");移到makeZip内部,确保每次压缩都用全新实例。
  2. 完善错误处理:将直接throw err改为reject(err),让错误能被上层await捕获,避免程序崩溃。

可选优化:并行压缩

如果想提升压缩速度,可改用Promise.all并行处理所有文件夹:

const zipFolders = async (rootPath: string, dirPaths: string[]): Promise<void> => {
  await Promise.all(dirPaths.map(folder => makeZip(folder, rootPath)));
};

内容的提问来源于stack exchange,提问作者user21039427

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 12:55:25