| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- var Utils = require("./util");
- var fs = Utils.FileSystem.require(),
- pth = require("path");
- fs.existsSync = fs.existsSync || pth.existsSync;
- var ZipEntry = require("./zipEntry"),
- ZipFile = require("./zipFile");
- var isWin = /^win/.test(process.platform);
- module.exports = function (/*String*/input) {
- var _zip = undefined,
- _filename = "";
- if (input && typeof input === "string") { // load zip file
- if (fs.existsSync(input)) {
- _filename = input;
- _zip = new ZipFile(input, Utils.Constants.FILE);
- } else {
- throw Utils.Errors.INVALID_FILENAME;
- }
- } else if (input && Buffer.isBuffer(input)) { // load buffer
- _zip = new ZipFile(input, Utils.Constants.BUFFER);
- } else { // create new zip file
- _zip = new ZipFile(null, Utils.Constants.NONE);
- }
- function sanitize(prefix, name) {
- prefix = pth.resolve(pth.normalize(prefix));
- var parts = name.split('/');
- for (var i = 0, l = parts.length; i < l; i++) {
- var path = pth.normalize(pth.join(prefix, parts.slice(i, l).join(pth.sep)));
- if (path.indexOf(prefix) === 0) {
- return path;
- }
- }
- return pth.normalize(pth.join(prefix, pth.basename(name)));
- }
- function getEntry(/*Object*/entry) {
- if (entry && _zip) {
- var item;
- // If entry was given as a file name
- if (typeof entry === "string")
- item = _zip.getEntry(entry);
- // if entry was given as a ZipEntry object
- if (typeof entry === "object" && typeof entry.entryName !== "undefined" && typeof entry.header !== "undefined")
- item = _zip.getEntry(entry.entryName);
- if (item) {
- return item;
- }
- }
- return null;
- }
- return {
- /**
- * Extracts the given entry from the archive and returns the content as a Buffer object
- * @param entry ZipEntry object or String with the full path of the entry
- *
- * @return Buffer or Null in case of error
- */
- readFile: function (/*Object*/entry) {
- var item = getEntry(entry);
- return item && item.getData() || null;
- },
- /**
- * Asynchronous readFile
- * @param entry ZipEntry object or String with the full path of the entry
- * @param callback
- *
- * @return Buffer or Null in case of error
- */
- readFileAsync: function (/*Object*/entry, /*Function*/callback) {
- var item = getEntry(entry);
- if (item) {
- item.getDataAsync(callback);
- } else {
- callback(null, "getEntry failed for:" + entry)
- }
- },
- /**
- * Extracts the given entry from the archive and returns the content as plain text in the given encoding
- * @param entry ZipEntry object or String with the full path of the entry
- * @param encoding Optional. If no encoding is specified utf8 is used
- *
- * @return String
- */
- readAsText: function (/*Object*/entry, /*String - Optional*/encoding) {
- var item = getEntry(entry);
- if (item) {
- var data = item.getData();
- if (data && data.length) {
- return data.toString(encoding || "utf8");
- }
- }
- return "";
- },
- /**
- * Asynchronous readAsText
- * @param entry ZipEntry object or String with the full path of the entry
- * @param callback
- * @param encoding Optional. If no encoding is specified utf8 is used
- *
- * @return String
- */
- readAsTextAsync: function (/*Object*/entry, /*Function*/callback, /*String - Optional*/encoding) {
- var item = getEntry(entry);
- if (item) {
- item.getDataAsync(function (data) {
- if (data && data.length) {
- callback(data.toString(encoding || "utf8"));
- } else {
- callback("");
- }
- })
- } else {
- callback("");
- }
- },
- /**
- * Remove the entry from the file or the entry and all it's nested directories and files if the given entry is a directory
- *
- * @param entry
- */
- deleteFile: function (/*Object*/entry) { // @TODO: test deleteFile
- var item = getEntry(entry);
- if (item) {
- _zip.deleteEntry(item.entryName);
- }
- },
- /**
- * Adds a comment to the zip. The zip must be rewritten after adding the comment.
- *
- * @param comment
- */
- addZipComment: function (/*String*/comment) { // @TODO: test addZipComment
- _zip.comment = comment;
- },
- /**
- * Returns the zip comment
- *
- * @return String
- */
- getZipComment: function () {
- return _zip.comment || '';
- },
- /**
- * Adds a comment to a specified zipEntry. The zip must be rewritten after adding the comment
- * The comment cannot exceed 65535 characters in length
- *
- * @param entry
- * @param comment
- */
- addZipEntryComment: function (/*Object*/entry, /*String*/comment) {
- var item = getEntry(entry);
- if (item) {
- item.comment = comment;
- }
- },
- /**
- * Returns the comment of the specified entry
- *
- * @param entry
- * @return String
- */
- getZipEntryComment: function (/*Object*/entry) {
- var item = getEntry(entry);
- if (item) {
- return item.comment || '';
- }
- return ''
- },
- /**
- * Updates the content of an existing entry inside the archive. The zip must be rewritten after updating the content
- *
- * @param entry
- * @param content
- */
- updateFile: function (/*Object*/entry, /*Buffer*/content) {
- var item = getEntry(entry);
- if (item) {
- item.setData(content);
- }
- },
- /**
- * 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
- */
- addLocalFile: function (/*String*/localPath, /*String*/zipPath, /*String*/zipName) {
- if (fs.existsSync(localPath)) {
- if (zipPath) {
- zipPath = zipPath.split("\\").join("/");
- if (zipPath.charAt(zipPath.length - 1) !== "/") {
- zipPath += "/";
- }
- } else {
- zipPath = "";
- }
- var p = localPath.split("\\").join("/").split("/").pop();
- if (zipName) {
- this.addFile(zipPath + zipName, fs.readFileSync(localPath), "", 0)
- } else {
- this.addFile(zipPath + p, fs.readFileSync(localPath), "", 0)
- }
- } else {
- throw Utils.Errors.FILE_NOT_FOUND.replace("%s", localPath);
- }
- },
- /**
- * 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.
- */
- addLocalFolder: function (/*String*/localPath, /*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 += "/";
- if (fs.existsSync(localPath)) {
- var items = Utils.findFiles(localPath),
- self = this;
- if (items.length) {
- items.forEach(function (path) {
- var p = path.split("\\").join("/").replace(new RegExp(localPath.replace(/(\(|\))/g, '\\$1'), 'i'), ""); //windows fix
- if (filter(p)) {
- if (p.charAt(p.length - 1) !== "/") {
- self.addFile(zipPath + p, fs.readFileSync(path), "", 0)
- } else {
- self.addFile(zipPath + p, Buffer.alloc(0), "", 0)
- }
- }
- });
- }
- } else {
- throw Utils.Errors.FILE_NOT_FOUND.replace("%s", localPath);
- }
- },
- /**
- * 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.
- * Comment and attributes are optional
- *
- * @param entryName
- * @param content
- * @param comment
- * @param attr
- */
- addFile: function (/*String*/entryName, /*Buffer*/content, /*String*/comment, /*Number*/attr) {
- var entry = new ZipEntry();
- entry.entryName = entryName;
- entry.comment = comment || "";
- if (!attr) {
- if (entry.isDirectory) {
- attr = (0o40755 << 16) | 0x10; // (permissions drwxr-xr-x) + (MS-DOS directory flag)
- } else {
- attr = 0o644 << 16; // permissions -r-wr--r--
- }
- }
- entry.attr = attr;
- entry.setData(content);
- _zip.setEntry(entry);
- },
- /**
- * Returns an array of ZipEntry objects representing the files and folders inside the archive
- *
- * @return Array
- */
- getEntries: function () {
- if (_zip) {
- return _zip.entries;
- } else {
- return [];
- }
- },
- /**
- * Returns a ZipEntry object representing the file or folder specified by ``name``.
- *
- * @param name
- * @return ZipEntry
- */
- getEntry: function (/*String*/name) {
- return getEntry(name);
- },
- /**
- * Extracts the given entry to the given targetPath
- * If the entry is a directory inside the archive, the entire directory and it's subdirectories will be extracted
- *
- * @param entry ZipEntry object or String with the full path of the entry
- * @param targetPath Target folder where to write the file
- * @param maintainEntryPath If maintainEntryPath is true and the entry is inside a folder, the entry folder
- * will be created in targetPath as well. Default is TRUE
- * @param overwrite If the file already exists at the target path, the file will be overwriten if this is true.
- * Default is FALSE
- *
- * @return Boolean
- */
- extractEntryTo: function (/*Object*/entry, /*String*/targetPath, /*Boolean*/maintainEntryPath, /*Boolean*/overwrite) {
- overwrite = overwrite || false;
- maintainEntryPath = typeof maintainEntryPath === "undefined" ? true : maintainEntryPath;
- var item = getEntry(entry);
- if (!item) {
- throw Utils.Errors.NO_ENTRY;
- }
- var entryName = item.entryName;
- var target = sanitize(targetPath, pth.resolve(targetPath, maintainEntryPath ? entryName : pth.basename(entryName)));
- if (item.isDirectory) {
- target = pth.resolve(target, "..");
- var children = _zip.getEntryChildren(item);
- children.forEach(function (child) {
- if (child.isDirectory) return;
- var content = child.getData();
- if (!content) {
- throw Utils.Errors.CANT_EXTRACT_FILE;
- }
- var childName = sanitize(targetPath, child.entryName);
- Utils.writeFileTo(pth.resolve(targetPath, maintainEntryPath ? childName : childName.substr(entryName.length)), content, overwrite);
- });
- return true;
- }
- var content = item.getData();
- if (!content) throw Utils.Errors.CANT_EXTRACT_FILE;
- if (fs.existsSync(target) && !overwrite) {
- throw Utils.Errors.CANT_OVERRIDE;
- }
- Utils.writeFileTo(target, content, overwrite);
- return true;
- },
- /**
- * Test the archive
- *
- */
- test: function () {
- if (!_zip) {
- return false;
- }
- for (var entry in _zip.entries) {
- try {
- if (entry.isDirectory) {
- continue;
- }
- var content = _zip.entries[entry].getData();
- if (!content) {
- return false;
- }
- } catch (err) {
- return false;
- }
- }
- return true;
- },
- /**
- * Extracts the entire archive to the given location
- *
- * @param targetPath Target location
- * @param overwrite If the file already exists at the target path, the file will be overwriten if this is true.
- * Default is FALSE
- */
- extractAllTo: function (/*String*/targetPath, /*Boolean*/overwrite) {
- overwrite = overwrite || false;
- if (!_zip) {
- throw Utils.Errors.NO_ZIP;
- }
- _zip.entries.forEach(function (entry) {
- var entryName = sanitize(targetPath, entry.entryName.toString());
- if (entry.isDirectory) {
- Utils.makeDir(entryName);
- return;
- }
- var content = entry.getData();
- if (!content) {
- throw Utils.Errors.CANT_EXTRACT_FILE;
- }
- Utils.writeFileTo(entryName, content, overwrite);
- fs.utimesSync(entryName, entry.header.time, entry.header.time)
- })
- },
- /**
- * Asynchronous extractAllTo
- *
- * @param targetPath Target location
- * @param overwrite If the file already exists at the target path, the file will be overwriten if this is true.
- * Default is FALSE
- * @param callback
- */
- extractAllToAsync: function (/*String*/targetPath, /*Boolean*/overwrite, /*Function*/callback) {
- if (!callback) {
- callback = function() {}
- }
- overwrite = overwrite || false;
- if (!_zip) {
- callback(new Error(Utils.Errors.NO_ZIP));
- return;
- }
- var entries = _zip.entries;
- var i = entries.length;
- entries.forEach(function (entry) {
- if (i <= 0) return; // Had an error already
- var entryName = pth.normalize(entry.entryName.toString());
- if (entry.isDirectory) {
- Utils.makeDir(sanitize(targetPath, entryName));
- if (--i === 0)
- callback(undefined);
- return;
- }
- entry.getDataAsync(function (content) {
- if (i <= 0) return;
- if (!content) {
- i = 0;
- callback(new Error(Utils.Errors.CANT_EXTRACT_FILE));
- return;
- }
- Utils.writeFileToAsync(sanitize(targetPath, entryName), content, overwrite, function (succ) {
- fs.utimesSync(pth.resolve(targetPath, entryName), entry.header.time, entry.header.time);
- if (i <= 0) return;
- if (!succ) {
- i = 0;
- callback(new Error('Unable to write'));
- return;
- }
- if (--i === 0)
- callback(undefined);
- });
- });
- })
- },
- /**
- * Writes the newly created zip file to disk at the specified location or if a zip was opened and no ``targetFileName`` is provided, it will overwrite the opened zip
- *
- * @param targetFileName
- * @param callback
- */
- writeZip: function (/*String*/targetFileName, /*Function*/callback) {
- if (arguments.length === 1) {
- if (typeof targetFileName === "function") {
- callback = targetFileName;
- targetFileName = "";
- }
- }
- if (!targetFileName && _filename) {
- targetFileName = _filename;
- }
- if (!targetFileName) return;
- var zipData = _zip.compressToBuffer();
- if (zipData) {
- var ok = Utils.writeFileTo(targetFileName, zipData, true);
- if (typeof callback === 'function') callback(!ok ? new Error("failed") : null, "");
- }
- },
- /**
- * Returns the content of the entire zip file as a Buffer object
- *
- * @return Buffer
- */
- toBuffer: function (/*Function*/onSuccess, /*Function*/onFail, /*Function*/onItemStart, /*Function*/onItemEnd) {
- this.valueOf = 2;
- if (typeof onSuccess === "function") {
- _zip.toAsyncBuffer(onSuccess, onFail, onItemStart, onItemEnd);
- return null;
- }
- return _zip.compressToBuffer()
- }
- }
- };
|