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?
You could also use node-archiver , which was very helpful when I was using it. First you need create
and 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 your passing 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, recusively, 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.