From 20c9cb8ae28b090e42dd3b4c406a3e4d9d7f3875 Mon Sep 17 00:00:00 2001 From: Maigret Aurelien Date: Thu, 22 Nov 2018 16:00:40 +0100 Subject: [PATCH 1/2] Add async version of addLocalFile 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. --- adm-zip.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/adm-zip.js b/adm-zip.js index 0602374..bbfa058 100644 --- a/adm-zip.js +++ b/adm-zip.js @@ -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. From 237bb7fd1a53597b37ddd3b4f877a61c48375268 Mon Sep 17 00:00:00 2001 From: mart_- Date: Fri, 1 Feb 2019 11:45:30 +0100 Subject: [PATCH 2/2] fix accent filename --- adm-zip.js | 1 + 1 file changed, 1 insertion(+) diff --git a/adm-zip.js b/adm-zip.js index bbfa058..0bc17ae 100644 --- a/adm-zip.js +++ b/adm-zip.js @@ -336,6 +336,7 @@ module.exports = function (/*String*/input) { i += 1; if (i < items.length) { var p = items[i].split("\\").join("/").replace(new RegExp(localPath.replace(/(\(|\))/g, '\\$1'), 'i'), ""); //windows fix + p = p.normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/[^\x20-\x7E]/g, '') // accent fix if (filter(p)) { if (p.charAt(p.length - 1) !== "/") { fs.readFile(items[i], function (err, data) {