KouShuiYu
V2EX  ›  Node.js

使用 node 提供的`gzip`为什么压缩表格的结果会是一个文件夹(txt 文本没有这个问题)?

  •  
  •   KouShuiYu · Jan 19, 2021 · 2098 views
    This topic created in 2015 days ago, the information mentioned may be changed or developed.

    gzip

    async function zipBuffer(payload) {
      return new Promise((res, rej) => {
        gzip(payload, (err, buffer) => {
          if (err) {
            rej(err);
          }
          res(buffer);
        });
      });
    }
    

    使用archiver没这个问题,就是没办法直接压缩到 buffer,需要先生成压缩文件再读取,感觉绕了一个圈子

    function zipBuffer(buffer, outputName) {
      return new Promise((res, rej) => {
        const outputPath = `./temp_data/_${getToken()}.zip`;
        const output = fs.createWriteStream(outputPath);
        const archive = archiver('zip', {
          zlib: { level: 9 },
        });
        archive.pipe(output);
        archive.append(buffer, { name: outputName });
        archive.finalize();
        output.on('close', async function () {
          const zipBuffer = await streamToBuffer(fs.createReadStream(outputPath));
          await unlinkFile(outputPath);
          res(zipBuffer);
        });
        output.on('error', async function (err) {
          rej(err);
        });
      });
    }
    
    1 replies    2021-01-19 23:18:43 +08:00
    joesonw
        1
    joesonw  
       Jan 19, 2021
    xlsx 本来就是 zip 阿. 你把后缀改为.zip 打开就知道了.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4983 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:51 · PVG 17:51 · LAX 02:51 · JFK 05:51
    ♥ Do have faith in what you're doing.