首页 > 解决方案 > yauzl 返回错误:未找到中央目录记录签名的结尾

问题描述

以下 Node JS 代码总是失败:

错误:未找到中央目录记录签名的结尾

我尝试了几种 zip 文件。

  // Retrieve the snapshot, which will be a zip file
  S3.getObject({ Bucket: bucket, Key: snapshotPath}, (err, response) => {
    console.log(`Snapshot length: ${response.Body.length}`);  // confirmed the length is the same as the test file
    if (err) {
      throw(err);

    } else {

      fs.writeFileSync('/tmp/test.gz', response.Body);  // (debug) confirmed this writes out a zipped file identical to the original

      yauzl.fromBuffer(response.Body, {lazyEntries: true}, function(err, zipFile) {
        if (err) {
          console.log(`ERROR getting zipFile: ${err}`);
          throw err;
        }


        // never makes it to here

        // this will recurse through the gz file
        // in our case we will have only one file, but it will be very large
        zipFile.readEntry();
        zipFile.on('entry', function(entry) {
        ...

测试文件被这样模拟:

   aws.mock('S3', 'getObject', (params, callback) => {
      if (params.Key.includes('Metadata')) {
        ...
        });
      } else {
        console.log(`Mocking snapshot...`)
        callback(null, {
          AcceptRanges: "bytes",
          LastModified: "Fri, 19 Jun 2020 16:38:33 GMT",
          ContentLength: 940,
          ETag: "\"85ca928a4d8766c8bd53607c6782db0f\"",
          ContentType: "binary/octet-stream",
          Metadata: {},
          Body: Buffer.from(fs.readFileSync(__dirname + '/sample-data/sb2'))
        });
      }

我在 readFileSync 上尝试了不同的编码。yauzl 打开的文件长度为 940,与测试 zip 文件的大小一致。在压缩之前写出文件会创建一个不可压缩的文件。我尝试在 mac 上使用 gzip 文件,还尝试在 linux 上使用 gzip 创建的 zip 文件。

感谢你的协助!

标签: amazon-s3node-modulesunzip

解决方案


yauzl 不处理 gzip 或 gunzip。见https://github.com/thejoshwolfe/yauzl/issues/104


推荐阅读