node.js - Archiving folder using archiver generate an empty zip

佐手、 提交于 2021-02-20 02:59:29

问题


I am trying to archive a folder using archiver, the path of the folder that i'd like to archive look like this :

Project
| app.js
| tmp
    |
     folderToArchive
            │file1.txt 
            │file2.txt 
            │file3.txt 

my server side code where the zip file will be generated look like this :

var archiver = require("archiver");

    app.get("/download/:folder", (req, res) => {
    
      var FolderName = req.params.folder;
      var zipName = FolderName + ".zip";
      var source = path.join(__dirname, "tmp", FolderName);
      var out = path.join(__dirname, "tmp", zipName);
    
    
      const archive = archiver('zip', { zlib: { level: 9 }});
      const stream = fs.createWriteStream(out);
    
      return new Promise((resolve, reject) => {
        archive
          .directory(source, false)
          .on('error', err => reject(err))
          .pipe(stream)
        ;
    
        stream.on('close', () => resolve());
        archive.finalize();
        console.log("zip file created");
      });
    
    });

The issue is when i run the app it will create an empty zip file in the right destination.

This is totally new for me and I'd like to understand why the zip file is empty ?

Regards


回答1:


It could likely be the same reason that I had an empty archive, which is the main process ending before the archive get to write into zip.

Hence it should be wrapped in a promise, something like this:

    async function zipDirectory(source, dest) {
  const stream = fs.createWriteStream(dest);
  const archive = archiver('zip', { zlib: { level: 9 } });

  archive.on('error', function(err) {
  throw err;
  });

  await new Promise((resolve, reject) => {
    archive.pipe(stream);
    archive.directory(source, false);
    archive.on('error', err => {throw err;});
    archive.finalize();

    stream
        .on('close', function() {
        console.log(`zipped ${archive.pointer()} total bytes.`);
        resolve();
        });
  });
}



回答2:


Tested the following code and it works:

const path = require('path');
const archiver = require('archiver');
const fs = require('fs');

const FolderName = "fol";
const zipName = FolderName + ".zip";
const source = path.join(__dirname, "tmp", FolderName);
const out = path.join(__dirname, "tmp", zipName);

const archive = archiver('zip', { zlib: { level: 9 }});
const stream = fs.createWriteStream(out);

archive
    .directory(source, false)
    .on('error', err => {throw err;})
    .pipe(stream);

stream.on('close', () => console.log("closed"));
archive.finalize();
console.log("zip file created");

So I suggest:

const path = require('path');
const archiver = require('archiver');
const fs = require('fs');

const archive = (folderName) => {
    const zipName = folderName + ".zip";
    const source = path.join(__dirname, "tmp", folderName);
    const out = path.join(__dirname, "tmp", zipName);

    const archive = archiver('zip', { zlib: { level: 9 }});
    const stream = fs.createWriteStream(out);

    archive
        .directory(source, false)
        .on('error', err => {throw err;})
        .pipe(stream);

    stream.on('close', () => console.log("closed"));
    archive.finalize();
    console.log("zip file created");
};

app.get("/download/:folder", (req, res) => {
    const folderName = req.params.folder;
    archive(folderName);
});


来源:https://stackoverflow.com/questions/58312922/node-js-archiving-folder-using-archiver-generate-an-empty-zip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!