Skip to content

Commit

Permalink
Merge pull request #501 from 5saviahv/master
Browse files Browse the repository at this point in the history
added addLocalFolderAsync2
  • Loading branch information
5saviahv authored Jun 9, 2024
2 parents b2b2406 + 7198176 commit a8ddc64
Show file tree
Hide file tree
Showing 6 changed files with 1,016 additions and 426 deletions.
242 changes: 183 additions & 59 deletions adm-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ module.exports = function (/**String*/ input, /** object */ options) {
return join(".", normalize(sep + zipPath.split("\\").join(sep) + sep));
}

function filenameFilter(filterfn) {
if (filterfn instanceof RegExp) {
// if filter is RegExp wrap it
return (function (rx) {
return function (filename) {
return rx.test(filename);
};
})(filterfn);
} else if ("function" !== typeof filterfn) {
// if filter is not function we will replace it
return () => true;
}
return filterfn;
}

// keep last character on folders
const relativePath = (local, entry) => {
let lastChar = entry.slice(-1);
lastChar = lastChar === filetools.sep ? filetools.sep : "";
return pth.relative(local, entry) + lastChar;
};

return {
/**
* Extracts the given entry from the archive and returns the content as a Buffer object
Expand Down Expand Up @@ -234,55 +256,96 @@ module.exports = function (/**String*/ input, /** object */ options) {
/**
* Adds a file from the disk to the archive
*
* @param localPath File to add to zip
* @param zipPath Optional path inside the zip
* @param zipName Optional name for the file
* @param {string} localPath File to add to zip
* @param {string} [zipPath] Optional path inside the zip
* @param {string} [zipName] Optional name for the file
* @param {string} [comment] Optional file comment
*/
addLocalFile: function (/**String*/ localPath, /**String=*/ zipPath, /**String=*/ zipName, /**String*/ comment) {
addLocalFile: function (localPath, zipPath, zipName, comment) {
if (filetools.fs.existsSync(localPath)) {
// fix ZipPath
zipPath = zipPath ? fixPath(zipPath) : "";

// p - local file name
var p = localPath.split("\\").join("/").split("/").pop();
const p = pth.win32.basename(pth.win32.normalize(localPath));

// add file name into zippath
zipPath += zipName ? zipName : p;

// read file attributes
const _attr = filetools.fs.statSync(localPath);

// get file content
const data = _attr.isFile() ? filetools.fs.readFileSync(localPath) : Buffer.alloc(0);

// if folder
if (_attr.isDirectory()) zipPath += filetools.sep;

// add file into zip file
this.addFile(zipPath, filetools.fs.readFileSync(localPath), comment, _attr);
this.addFile(zipPath, data, comment, _attr);
} else {
throw new Error(Utils.Errors.FILE_NOT_FOUND.replace("%s", localPath));
}
},

/**
* Callback for showing if everything was done.
*
* @callback doneCallback
* @param {Error} err - Error object
* @param {boolean} done - was request fully completed
*/

/**
* Adds a file from the disk to the archive
*
* @param {(object|string)} options - options object, if it is string it us used as localPath.
* @param {string} options.localPath - Local path to the file.
* @param {string} [options.comment] - Optional file comment.
* @param {string} [options.zipPath] - Optional path inside the zip
* @param {string} [options.zipName] - Optional name for the file
* @param {doneCallback} callback - The callback that handles the response.
*/
addLocalFileAsync: function (options, callback) {
options = typeof options === "object" ? options : { localPath: options };
const localPath = pth.resolve(options.localPath);
const { comment } = options;
let { zipPath, zipName } = options;
const self = this;

filetools.fs.stat(localPath, function (err, stats) {
if (err) return callback(err, false);
// fix ZipPath
zipPath = zipPath ? fixPath(zipPath) : "";
// p - local file name
const p = pth.win32.basename(pth.win32.normalize(localPath));
// add file name into zippath
zipPath += zipName ? zipName : p;

if (stats.isFile()) {
filetools.fs.readFile(localPath, function (err, data) {
if (err) return callback(err, false);
self.addFile(zipPath, data, comment, stats);
return setImmediate(callback, undefined, true);
});
} else if (stats.isDirectory()) {
zipPath += filetools.sep;
self.addFile(zipPath, Buffer.alloc(0), comment, stats);
return setImmediate(callback, undefined, true);
}
});
},

/**
* Adds a local directory and all its nested files and directories to the archive
*
* @param localPath
* @param zipPath optional path inside zip
* @param filter optional RegExp or Function if files match will
* be included.
* @param {number | object} attr - number as unix file permissions, object as filesystem Stats object
* @param {string} localPath - local path to the folder
* @param {string} [zipPath] - optional path inside zip
* @param {(RegExp|function)} [filter] - optional RegExp or Function if files match will be included.
*/
addLocalFolder: function (/**String*/ localPath, /**String=*/ zipPath, /**=RegExp|Function*/ filter, /**=number|object*/ attr) {
addLocalFolder: function (localPath, zipPath, filter) {
// Prepare filter
if (filter instanceof RegExp) {
// if filter is RegExp wrap it
filter = (function (rx) {
return function (filename) {
return rx.test(filename);
};
})(filter);
} else if ("function" !== typeof filter) {
// if filter is not function we will replace it
filter = function () {
return true;
};
}
filter = filenameFilter(filter);

// fix ZipPath
zipPath = zipPath ? fixPath(zipPath) : "";
Expand All @@ -295,43 +358,29 @@ module.exports = function (/**String*/ input, /** object */ options) {
const self = this;

if (items.length) {
items.forEach(function (filepath) {
var p = pth.relative(localPath, filepath).split("\\").join("/"); //windows fix
for (const filepath of items) {
const p = pth.join(zipPath, relativePath(localPath, filepath));
if (filter(p)) {
var stats = filetools.fs.statSync(filepath);
if (stats.isFile()) {
self.addFile(zipPath + p, filetools.fs.readFileSync(filepath), "", attr ? attr : stats);
} else {
self.addFile(zipPath + p + "/", Buffer.alloc(0), "", attr ? attr : stats);
}
self.addLocalFile(filepath, pth.dirname(p));
}
});
}
}
} else {
throw new Error(Utils.Errors.FILE_NOT_FOUND.replace("%s", localPath));
}
},

/**
* Asynchronous addLocalFile
* Asynchronous addLocalFolder
* @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 instanceof RegExp) {
filter = (function (rx) {
return function (filename) {
return rx.test(filename);
};
})(filter);
} else if ("function" !== typeof filter) {
filter = function () {
return true;
};
}
// Prepare filter
filter = filenameFilter(filter);

// fix ZipPath
zipPath = zipPath ? fixPath(zipPath) : "";
Expand All @@ -353,7 +402,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
i += 1;
if (i < items.length) {
var filepath = items[i];
var p = pth.relative(localPath, filepath).split("\\").join("/"); //windows fix
var p = relativePath(localPath, filepath).split("\\").join("/"); //windows fix
p = p
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
Expand Down Expand Up @@ -391,24 +440,99 @@ module.exports = function (/**String*/ input, /** object */ options) {
},

/**
* Adds a local directory and all its nested files and directories to the archive
*
* @param {object | string} options - options object, if it is string it us used as localPath.
* @param {string} options.localPath - Local path to the folder.
* @param {string} [options.zipPath] - optional path inside zip.
* @param {RegExp|function} [options.filter] - optional RegExp or Function if files match will be included.
* @param {function|string} [options.namefix] - optional function to help fix filename
* @param {doneCallback} callback - The callback that handles the response.
*
*/
addLocalFolderAsync2: function (/*String*/ options, /*Function*/ callback) {
const self = this;
options = typeof options === "object" ? options : { localPath: options };
localPath = pth.resolve(fixPath(options.localPath));
let { zipPath, filter, namefix } = options;

if (filter instanceof RegExp) {
filter = (function (rx) {
return function (filename) {
return rx.test(filename);
};
})(filter);
} else if ("function" !== typeof filter) {
filter = function () {
return true;
};
}

// fix ZipPath
zipPath = zipPath ? fixPath(zipPath) : "";

// Check Namefix function
if (namefix == "latin1") {
namefix = (str) =>
str
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.replace(/[^\x20-\x7E]/g, ""); // accent fix (latin1 characers only)
}

if (typeof namefix !== "function") namefix = (str) => str;

// internal, create relative path + fix the name
const relPathFix = (entry) => pth.join(zipPath, namefix(relativePath(localPath, entry)));
const fileNameFix = (entry) => pth.win32.basename(pth.win32.normalize(namefix(entry)));

filetools.fs.open(localPath, "r", function (err) {
if (err && err.code === "ENOENT") {
callback(undefined, Utils.Errors.FILE_NOT_FOUND.replace("%s", localPath));
} else if (err) {
callback(undefined, err);
} else {
filetools.findFilesAsync(localPath, function (err, fileEntries) {
if (err) return callback(err);
fileEntries = fileEntries.filter((dir) => filter(relPathFix(dir)));
if (!fileEntries.length) callback(undefined, false);

setImmediate(
fileEntries.reverse().reduce(function (next, entry) {
return function (err, done) {
if (err || done === false) return setImmediate(next, err, false);

self.addLocalFileAsync(
{
localPath: entry,
zipPath: pth.dirname(relPathFix(entry)),
zipName: fileNameFix(entry)
},
next
);
};
}, callback)
);
});
}
});
},

/**
* Adds a local directory and all its nested files and directories to the archive
*
* @param {string} localPath - path where files will be extracted
* @param {object} props - optional properties
* @param {string} props.zipPath - optional path inside zip
* @param {regexp, function} props.filter - RegExp or Function if files match will be included.
* @param {string} [props.zipPath] - optional path inside zip
* @param {RegExp|function} [props.filter] - optional RegExp or Function if files match will be included.
* @param {function|string} [props.namefix] - optional function to help fix filename
*/
addLocalFolderPromise: function (/*String*/ localPath, /* object */ props) {
return new Promise((resolve, reject) => {
const { filter, zipPath } = Object.assign({}, props);
this.addLocalFolderAsync(
localPath,
(done, err) => {
if (err) reject(err);
if (done) resolve(this);
},
zipPath,
filter
);
this.addLocalFolderAsync2(Object.assign({ localPath }, props), (err, done) => {
if (err) reject(err);
if (done) resolve(this);
});
});
},

Expand Down
Loading

0 comments on commit a8ddc64

Please sign in to comment.