Skip to content

Commit

Permalink
add general option
Browse files Browse the repository at this point in the history
  • Loading branch information
5saviahv committed Feb 25, 2021
1 parent f8b6a33 commit ad1309d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 76 deletions.
108 changes: 68 additions & 40 deletions adm-zip.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,75 @@
var Utils = require("./util");
var fs = Utils.FileSystem.require(),
pth = require("path");
const Utils = require("./util");
const pth = require("path");
const ZipEntry = require("./zipEntry");
const ZipFile = require("./zipFile");

const fs = Utils.FileSystem.require();
fs.existsSync = fs.existsSync || pth.existsSync;

var ZipEntry = require("./zipEntry"),
ZipFile = require("./zipFile");

var isWin = /^win/.test(process.platform);
const defaultOptions = {
// read entries during load (initial loading may be slower)
readEntries: false,
// default method is none
method: Utils.Constants.NONE
}

function canonical(p) {
var safeSuffix = pth.normalize(p).replace(/^(\.\.(\/|\\|$))+/, '');
return pth.join('./', safeSuffix);
}

module.exports = function (/**String*/input) {
var _zip = undefined,
_filename = "";
module.exports = function (/**String*/input, /** object */options) {
let inBuffer = null;

// create object based default options, allowing them to be overwritten
const opts = Object.assign(Object.create( null ), defaultOptions);

// test input variable
if (input && "object" === typeof input){
// if value is not buffer we accept it to be object with options
if (!(input instanceof Uint8Array)){
Object.assign(opts, input);
input = opts.input ? opts.input : undefined;
if (opts.input) delete opts.input;
}

// if input is buffer
if (input instanceof Uint8Array){
inBuffer = input;
opts.method = Utils.Constants.BUFFER;
input = undefined;
}
}

if (input && typeof input === "string") { // load zip file
if (fs.existsSync(input)) {
_filename = input;
_zip = new ZipFile(input, Utils.Constants.FILE);
} else {
throw new Error(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);
}
// assign options
Object.assign(opts, options);

// if input is file name we retrieve its content
if (input && "string" === typeof input) {
// load zip file
if (fs.existsSync(input)) {
opts.method = Utils.Constants.FILE;
opts.filename = input;
inBuffer = fs.readFileSync(input);
} else {
throw new Error(Utils.Errors.INVALID_FILENAME);
}
}

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)));
}
// create variable
const _zip = new ZipFile(inBuffer, opts);

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) {
Expand Down Expand Up @@ -384,10 +412,10 @@ module.exports = function (/**String*/input) {
* 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
* @param {string} entryName
* @param {Buffer | string} content - file content as buffer or utf8 coded string
* @param {string} comment - file comment
* @param {number | object} attr - number as unix file permissions, object as filesystem Stats object
*/
addFile: function (/**String*/entryName, /**Buffer*/content, /**String*/comment, /**Number*/attr) {
// prepare new entry
Expand Down Expand Up @@ -652,8 +680,8 @@ module.exports = function (/**String*/input) {
}
}

if (!targetFileName && _filename) {
targetFileName = _filename;
if (!targetFileName && opts.filename) {
targetFileName = opts.filename;
}
if (!targetFileName) return;

Expand All @@ -669,7 +697,7 @@ module.exports = function (/**String*/input) {

return new Promise((resolve, reject) => {
// find file name
if (!targetFileName && _filename) targetFileName = _filename;
if (!targetFileName && opts.filename) targetFileName = opts.filename;
if (!targetFileName) reject("ADM-ZIP: ZIP File Name Missing");

this.toBufferPromise().then((zipData) => {
Expand Down
4 changes: 2 additions & 2 deletions util/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ module.exports = {
FLG_MSK : 4096, // mask header values

/* Load type */
FILE : 0,
FILE : 2,
BUFFER : 1,
NONE : 2,
NONE : 0,

/* 4.5 Extensible data fields */
EF_ID : 0,
Expand Down
5 changes: 2 additions & 3 deletions zipEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var Utils = require("./util"),
Methods = require("./methods");

module.exports = function (/*Buffer*/input) {

var _entryHeader = new Headers.EntryHeader(),
_entryName = Buffer.alloc(0),
_comment = Buffer.alloc(0),
Expand Down Expand Up @@ -90,7 +89,7 @@ module.exports = function (/*Buffer*/input) {
} else { //si added otherwise did not seem to return data.
if (callback) callback(data);
}
})
});
}
break;
default:
Expand Down Expand Up @@ -132,7 +131,7 @@ module.exports = function (/*Buffer*/input) {
_entryHeader.compressedSize = data.length;
data.copy(compressedData);
callback && callback(compressedData);
})
});
}
deflater = null;
break;
Expand Down
56 changes: 25 additions & 31 deletions zipFile.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
var ZipEntry = require("./zipEntry"),
Headers = require("./headers"),
Utils = require("./util");

module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
var entryList = [],
entryTable = {},
_comment = Buffer.alloc(0),
filename = "",
fs = Utils.FileSystem.require(),
inBuffer = null,
mainHeader = new Headers.MainHeader(),
loadedEntries = false;

if (inputType === Utils.Constants.FILE) {
// is a filename
filename = input;
inBuffer = fs.readFileSync(filename);
readMainHeader();
} else if (inputType === Utils.Constants.BUFFER) {
// is a memory buffer
inBuffer = input;
readMainHeader();
} else {
// none. is a new file
loadedEntries = true;
}
const ZipEntry = require("./zipEntry");
const Headers = require("./headers");
const Utils = require("./util");

module.exports = function (/*Buffer|null*/inBuffer, /** object */options) {
var entryList = [],
entryTable = {},
_comment = Buffer.alloc(0),
mainHeader = new Headers.MainHeader(),
loadedEntries = false;

// assign options
const opts = Object.assign(Object.create(null), options);

if (inBuffer){
// is a memory buffer
readMainHeader(opts.readEntries);
} else {
// none. is a new file
loadedEntries = true;
}

function iterateEntries(callback) {
const totalEntries = mainHeader.diskEntries; // total number of entries
Expand Down Expand Up @@ -70,7 +64,7 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
}
}

function readMainHeader() {
function readMainHeader(/*Boolean*/ readNow) {
var i = inBuffer.length - Utils.Constants.ENDHDR, // END header size
max = Math.max(0, i - 0xFFFF), // 0xFFFF is the max zip file comment length
n = max,
Expand Down Expand Up @@ -110,8 +104,8 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
if (mainHeader.commentLength) {
_comment = inBuffer.slice(commentEnd + Utils.Constants.ENDHDR);
}
// readEntries();
}
if (readNow) readEntries();
}

return {
/**
Expand Down Expand Up @@ -309,7 +303,7 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {

mh.copy(outBuffer, dindex); // write main header

return outBuffer
return outBuffer;
},

toAsyncBuffer: function (/*Function*/onSuccess, /*Function*/onFail, /*Function*/onItemStart, /*Function*/onItemEnd) {
Expand Down

0 comments on commit ad1309d

Please sign in to comment.