Skip to content

Commit

Permalink
Add async version of addLocalFile
Browse files Browse the repository at this point in the history
Use open and readFile instead of existsSync and readFileSync. There
are still some sync functions left in the Utils.findFiles call,
but the impact is minimal compared to the readFileSync.
  • Loading branch information
Dewep committed Nov 22, 2018
1 parent cb300c9 commit 20c9cb8
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions adm-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,83 @@ module.exports = function (/*String*/input) {
}
},

/**
* Asynchronous addLocalFile
* @param localPath
* @param callback
* @param zipPath optional path inside zip
* @param filter optional RegExp or Function if files match will
* be included.
*/
addLocalFolderAsync: function (/*String*/localPath, /*Function*/callback, /*String*/zipPath, /*RegExp|Function*/filter) {
if (filter === undefined) {
filter = function () {
return true;
};
} else if (filter instanceof RegExp) {
filter = function (filter) {
return function (filename) {
return filter.test(filename);
}
}(filter);
}

if (zipPath) {
zipPath = zipPath.split("\\").join("/");
if (zipPath.charAt(zipPath.length - 1) !== "/") {
zipPath += "/";
}
} else {
zipPath = "";
}
// normalize the path first
localPath = pth.normalize(localPath);
localPath = localPath.split("\\").join("/"); //windows fix
if (localPath.charAt(localPath.length - 1) !== "/")
localPath += "/";

var self = this;
fs.open(localPath, 'r', function (err, fd) {
if (err && err.code === 'ENOENT') {
callback(undefined, Utils.Errors.FILE_NOT_FOUND.replace("%s", localPath));
} else if (err) {
callback(undefined, err);
} else {
var items = Utils.findFiles(localPath);
var i = -1;

var next = function () {
i += 1;
if (i < items.length) {
var p = items[i].split("\\").join("/").replace(new RegExp(localPath.replace(/(\(|\))/g, '\\$1'), 'i'), ""); //windows fix
if (filter(p)) {
if (p.charAt(p.length - 1) !== "/") {
fs.readFile(items[i], function (err, data) {
if (err) {
callback(undefined, err);
} else {
self.addFile(zipPath + p, data, '', 0);
next();
}
})
} else {
self.addFile(zipPath + p, Buffer.alloc(0), "", 0);
next();
}
} else {
next();
}

} else {
callback(true, undefined);
}
}

next();
}
});
},

/**
* Allows you to create a entry (file or directory) in the zip file.
* If you want to create a directory the entryName must end in / and a null buffer should be provided.
Expand Down

0 comments on commit 20c9cb8

Please sign in to comment.