最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to zip a folder in node js application & after zip downloading start? - Stack Overflow

programmeradmin1浏览0评论

I tried with npm package adm-zip 0.4.4 because the latest one 0.4.7 doesn't work, adm-zip 0.4.4 works on Windows but not on Mac & Linux. Another problem is that I only want zip_folder to be zipped but it zipps the whole directory structure staring from folder_1. This is the code:

var zip = new admZip();

zip.addLocalFolder("./folder_1/folder_2/folder_3/**zip_folder**");

zip.writeZip("./folder_1/folder_2/folder_3/download_folder/zip_folder.zip");

All this happens on the server side. I have searched a lot and tried many npm packages to zip a folder or directory. Any suggestions or any other good approach to solve my problem?

I tried with npm package adm-zip 0.4.4 because the latest one 0.4.7 doesn't work, adm-zip 0.4.4 works on Windows but not on Mac & Linux. Another problem is that I only want zip_folder to be zipped but it zipps the whole directory structure staring from folder_1. This is the code:

var zip = new admZip();

zip.addLocalFolder("./folder_1/folder_2/folder_3/**zip_folder**");

zip.writeZip("./folder_1/folder_2/folder_3/download_folder/zip_folder.zip");

All this happens on the server side. I have searched a lot and tried many npm packages to zip a folder or directory. Any suggestions or any other good approach to solve my problem?

Share edited Oct 27, 2015 at 9:59 LordTribual 4,2492 gold badges29 silver badges38 bronze badges asked Oct 27, 2015 at 9:25 KinnyKinny 471 gold badge1 silver badge11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

You could also use node-archiver, which was very helpful when I was using it. First you need create an instance of the archiver as follows:

var fs = require('fs');
var archiver = require('archiver');

var archive = archiver.create('zip', {});
var output = fs.createWriteStream(__dirname + '/zip_folder.zip');

This way you tell the archiver to zip the files or folders based on the format you pass along with the method. In this case it's zip. In addition, we create a writeStream which will be piped to the archiver as its output. Also, we use the directory method to append a directory and its files, recursively, given its dirpath:

archive.pipe(output);

archive
  .directory(__dirname + '/folder_1/folder_2/folder_3/download_folder/zip_folder')
  .finalize();

At the end, we need to finalize the instance which prevents further appending to the archive structure.

Another option is to use the bulk method like so:

archive.bulk([{ 
  expand: true, cwd: './folder_1/folder_2/folder_3/download_folder/zip_folder/', 
  src: ['**/*'] 
}]).finalize();

Update 1

A little explanation for the [**/*] syntax: This will recursively include all folders ** and files *.

Try to use the system's zip function:

var execFile = require('child_process').execFile;
execFile('zip', ['-r', '-j', zipName, path], function(err, stdout) {
    if(err){
        console.log(err);
        throw err;
    }

    console.log('success');
});

Replace zipName and path with what you need.

发布评论

评论列表(0)

  1. 暂无评论