archiverjs / node-archiver

a streaming interface for archive generation
https://www.archiverjs.com
MIT License
2.76k stars 218 forks source link

Corrupted zip file, created using data event from archiver #770

Open RakeshPawar opened 1 week ago

RakeshPawar commented 1 week ago

Hello

I am using archiver to create zip from list of files. and final goal is to stream archived file to azure storage.

Please find below code

{ const archiver = (await import('archiver')).default;
  const archive = archiver('zip');

  const p = new Promise((resolve, reject) => {
    archive.on('finish', resolve);
    archive.on('error', err => {
      console.log('error while archiving', err);
      reject(err);
    });

    archive.on('warning', err => {
      if (err.code !== 'ENOENT') {
        reject(err);
      }
    });
    archive.on('data', chunk => {
      appendFile(path.join(cwd, '.build/test2.zip'), chunk) // appending chunks to file , this simulates passing data to passthrough stream by pipe
    });

    archive.pipe(require('fs').createWriteStream('test1.zip')); // steaming to writeStream

    for (const fileOrSubDirPath of filePaths) {
      const relativePath = path.relative(cwd, fileOrSubDirPath);
      archive.file(fileOrSubDirPath, { name: relativePath });
     }

  });
  await archive.finalize();
  await p;
  } 

Using passthrough stream to pass it to azure storage but same result as below, here for simplicity I created 2 files test1 -> created using writestream using pipe. test2 -> created using "chunks" emitted by data event.

I can both files with same size , however I am able unzip test1 , and test2 file is corrupted.

would appreciate any help here :) thanks in advance !