在节点10中使用adm-zipunzipperyauzl解压失败,无声无息。

问题描述 投票:1回答:1

我一直在尝试在我的代码中添加一个用例,在这个用例中,我试图解压一个太大的压缩包,以适应磁盘空间,我希望我的代码能够抛出ENOSPC。我已经尝试了多个库,但它们都没有抛出错误,而是在没有完成压缩的情况下默默地失败了。我希望它们能抛出ENOSPC错误,但所有的包似乎都记录了第一条info语句,说明解压缩已经开始,但之后就没有了。他们中的大多数人都创建了不完整的文件夹,不管他们在磁盘空间耗尽之前能写什么。下面是我对每个库的代码。

我的代码使用 识别压缩包:

exports.unzip = function(source, destination) {
    console.info("Started un-zipping from source: %s to destination: %s", source, destination);
    try {
        const zip = new AdmZip(source);
        zip.extractAllTo(destination, true);
        console.info("done unzipping");
    } catch (error) {
        console.error("Unzipping failed. Reason: %s", error)
        throw new Error(error)
    }
};

代码使用 yauzl:

exports.extractZip = function(source, destination) {
    return new Promise(function(resolve, reject) {
      console.log("Extracting zip: '" + source + "' to '" + destination + "'");

      yauzl.open(source, {
        lazyEntries: true
      }, function(err, zipfile) {
        if (err) throw err;
        zipfile.readEntry();
        zipfile.on("error", function (err) {
            console.error("Something went wrong while extracting!");
            reject(new Error(err));
        });
        zipfile.on("end", function () {
            console.log("Completed extracting zip!");
            resolve();
        });
        zipfile.on("entry", function(entry) {
          if (/\/$/.test(entry.fileName)) {
            // directory file names end with '/'
            mkdirp(destination + '/' + entry.fileName, function(err) {
              if (err) {
                  console.error("Something went wrong while extracting!");
                  throw err;
              }
              zipfile.readEntry();
            });
          } else {
            // file entry
            zipfile.openReadStream(entry, function(err, readStream) {
              if (err) {
                console.error("Something went wrong while extracting!");
                throw err;
              }
              // ensure parent directory exists
              mkdirp(destination + '/' + path.dirname(entry.fileName), function(err) {
                if (err) throw err;
                readStream.pipe(fs.createWriteStream(destination + '/' + entry.fileName));
                readStream.on("end", function() {
                  zipfile.readEntry();
                });
              });
            });
          }
        });
      });
    });
  }

代码使用 拉开拉链:

exports.unzip2 = function(source, destination) {
    console.info("Started un-zipping from source: %s to destination: %s", source, destination);
    try {
        fs.createReadStream(source)
        .pipe(unzipper.Extract({ path: destination }))
        .on('error',function (err){
            console.error("something went wrong", err.code);
            throw err;
        });
    } catch (error) {
        console.error("Unzipping failed. Reason: %s", error)
        throw new Error(error)
    }
};

代码使用 解压缩:

exports.extractArchive = async function(source, destination) {
    try {
        extract(source, { dir: destination }, function (err) {
            if (err) {
                console.error("Something went wrong!", err.code);
                throw err;
            }
        });
        console.log('Extraction complete')
      } catch (err) {
        // handle any errors
      }
};

我的代码有什么问题吗?是否有什么特别的事件需要我去监听?

node.js npm unzip adm-zip
1个回答
0
投票

经过在Yauzl和unzipper上的一些跟踪和错误,unzipper似乎可以用下面的代码工作(当解压过程中磁盘空间耗尽时,会抛出ENOSPC)。

exports.unzip2 = function(source, destination) {
    return new Promise(function(resolve, reject) {
        console.info("Started un-zipping from source: %s to destination: %s", source, destination);
        try {
            var sourceStream = fs.createReadStream(source);
            sourceStream.on('error',function (err){
                console.error("something went wrong", err.code);
                reject(new Error(err));
            });
            var destinationStream = unzipper.Extract({ path: destination });
            destinationStream.on('error',function (err){
                console.error("something went wrong", err.code);
                reject(new Error(err));
            });
            destinationStream.on('close',function (){
                console.log("Completed extract!");
                resolve();
            });
            sourceStream.pipe(destinationStream).on('error',function (err){
                console.error("something went wrong", err.code);
                reject(new Error(err));
            });;
        } catch (error) {
            console.error("something went wrong", err.code);
            reject(new Error(err));
        }
    });
};
© www.soinside.com 2019 - 2024. All rights reserved.