forked from sole/node-zip-folder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (32 loc) · 1.02 KB
/
index.js
File metadata and controls
39 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var fs = require('fs');
var archiver = require('archiver');
/**
* @param {string|string[]} srcFolders Folder(s) to zip
* @param {string} zipFilePath Destination
* @param {Function} callback Filesize in bytes
*
* Slightly modified from original source: https://github.com/sole/node-zip-folder
*/
function zipFolder(srcFolders, zipFilePath, callback) {
var output = fs.createWriteStream(zipFilePath);
var zipArchive = archiver('zip');
output.on('close', function () {
callback(null, zipArchive.pointer()); // sends back filesize in byte
});
zipArchive.pipe(output);
if (!Array.isArray(srcFolders)) srcFolders = [srcFolders];
// pre 2.0.0
let arr = [];
for (let i = 0; i < srcFolders.length; i++) {
arr.push({ cwd: srcFolders[i], src: ['**/*'], expand: true });
}
zipArchive.bulk(arr);
// after 2.0.0
// for (let i = 0; i < srcFolders.length; i++) {
// zipArchive.directory(srcFolders[i], false);
// }
zipArchive.finalize(function (err, bytes) {
if (err) callback(err, null);
});
}
module.exports = zipFolder;