2

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?

LordTribual
  • 4,219
  • 2
  • 28
  • 38
Kinny
  • 47
  • 1
  • 1
  • 10

2 Answers2

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 *.

mepley
  • 470
  • 4
  • 18
LordTribual
  • 4,219
  • 2
  • 28
  • 38
  • 1
    Hi thanks, It working but one issue is there, it archive.directory zip the whole directory structure means when it zip its include all folders i.e. /folder_1/folder_2/folder_3/ I want only zip the last_folder in the whole path. So what I do ???? – Kinny Oct 27 '15 at 12:38
  • What problem are you facing? – LordTribual Oct 27 '15 at 12:38
  • Hi thanks, It working but one issue is there, it archive.directory zip the whole directory structure means when it zip its include all folders i.e. /folder_1/folder_2/folder_3/ I want only zip the last_folder in the whole path. So what I do ???? – Kinny Oct 27 '15 at 12:43
  • In that case use the `bulk` method instead of the `directory`. Give it a try. – LordTribual Oct 27 '15 at 12:56
  • yes its working... thanks, Can you explain me src: ['**/*'] in bulk ? what its mean. – Kinny Oct 27 '15 at 13:08
  • Quick FYI - the `bulk` option was [removed from `archiver` in version 2.0.0](https://archiverjs.com/docs/#changelog). – decates Aug 07 '18 at 09:52
1

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.

LordTribual
  • 4,219
  • 2
  • 28
  • 38
Subham
  • 1,414
  • 4
  • 17
  • 34