|
|
@ -11,21 +11,65 @@
|
|
|
|
var bitjs = bitjs || {};
|
|
|
|
var bitjs = bitjs || {};
|
|
|
|
bitjs.archive = bitjs.archive || {};
|
|
|
|
bitjs.archive = bitjs.archive || {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
|
|
|
|
// Stolen from Closure because it's the best way to do Java-like inheritance.
|
|
|
|
|
|
|
|
bitjs.base = function(me, opt_methodName, var_args) {
|
|
|
|
|
|
|
|
var caller = arguments.callee.caller;
|
|
|
|
|
|
|
|
if (caller.superClass_) {
|
|
|
|
|
|
|
|
// This is a constructor. Call the superclass constructor.
|
|
|
|
|
|
|
|
return caller.superClass_.constructor.apply(
|
|
|
|
|
|
|
|
me, Array.prototype.slice.call(arguments, 1));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var args = Array.prototype.slice.call(arguments, 2);
|
|
|
|
|
|
|
|
var foundCaller = false;
|
|
|
|
|
|
|
|
for (var ctor = me.constructor;
|
|
|
|
|
|
|
|
ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
|
|
|
|
|
|
|
|
if (ctor.prototype[opt_methodName] === caller) {
|
|
|
|
|
|
|
|
foundCaller = true;
|
|
|
|
|
|
|
|
} else if (foundCaller) {
|
|
|
|
|
|
|
|
return ctor.prototype[opt_methodName].apply(me, args);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If we did not find the caller in the prototype chain,
|
|
|
|
|
|
|
|
// then one of two things happened:
|
|
|
|
|
|
|
|
// 1) The caller is an instance method.
|
|
|
|
|
|
|
|
// 2) This method was not called by the right caller.
|
|
|
|
|
|
|
|
if (me[opt_methodName] === caller) {
|
|
|
|
|
|
|
|
return me.constructor.prototype[opt_methodName].apply(me, args);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw Error(
|
|
|
|
|
|
|
|
'goog.base called from a method of one name ' +
|
|
|
|
|
|
|
|
'to a method of a different name');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
bitjs.inherits = function(childCtor, parentCtor) {
|
|
|
|
|
|
|
|
/** @constructor */
|
|
|
|
|
|
|
|
function tempCtor() {};
|
|
|
|
|
|
|
|
tempCtor.prototype = parentCtor.prototype;
|
|
|
|
|
|
|
|
childCtor.superClass_ = parentCtor.prototype;
|
|
|
|
|
|
|
|
childCtor.prototype = new tempCtor();
|
|
|
|
|
|
|
|
childCtor.prototype.constructor = childCtor;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* An unarchive event.
|
|
|
|
* An unarchive event.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param {string} type The event type.
|
|
|
|
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.UnarchiveEvent = class {
|
|
|
|
bitjs.archive.UnarchiveEvent = function(type) {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param {string} type The event type.
|
|
|
|
* The event type.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
constructor(type) {
|
|
|
|
this.type = type;
|
|
|
|
/**
|
|
|
|
};
|
|
|
|
* The event type.
|
|
|
|
|
|
|
|
* @type {string}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.type = type;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The UnarchiveEvent types.
|
|
|
|
* The UnarchiveEvent types.
|
|
|
@ -41,102 +85,78 @@ bitjs.archive.UnarchiveEvent.Type = {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Useful for passing info up to the client (for debugging).
|
|
|
|
* Useful for passing info up to the client (for debugging).
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param {string} msg The info message.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.UnarchiveInfoEvent = class extends bitjs.archive.UnarchiveEvent {
|
|
|
|
bitjs.archive.UnarchiveInfoEvent = function(msg) {
|
|
|
|
|
|
|
|
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.INFO);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param {string} msg The info message.
|
|
|
|
* The information message.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
constructor(msg) {
|
|
|
|
this.msg = msg;
|
|
|
|
super(bitjs.archive.UnarchiveEvent.Type.INFO);
|
|
|
|
};
|
|
|
|
|
|
|
|
bitjs.inherits(bitjs.archive.UnarchiveInfoEvent, bitjs.archive.UnarchiveEvent);
|
|
|
|
/**
|
|
|
|
|
|
|
|
* The information message.
|
|
|
|
|
|
|
|
* @type {string}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.msg = msg;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* An unrecoverable error has occured.
|
|
|
|
* An unrecoverable error has occured.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param {string} msg The error message.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.UnarchiveErrorEvent = class extends bitjs.archive.UnarchiveEvent {
|
|
|
|
bitjs.archive.UnarchiveErrorEvent = function(msg) {
|
|
|
|
|
|
|
|
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.ERROR);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param {string} msg The error message.
|
|
|
|
* The information message.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
constructor(msg) {
|
|
|
|
this.msg = msg;
|
|
|
|
super(bitjs.archive.UnarchiveEvent.Type.ERROR);
|
|
|
|
};
|
|
|
|
|
|
|
|
bitjs.inherits(bitjs.archive.UnarchiveErrorEvent, bitjs.archive.UnarchiveEvent);
|
|
|
|
/**
|
|
|
|
|
|
|
|
* The information message.
|
|
|
|
|
|
|
|
* @type {string}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.msg = msg;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Start event.
|
|
|
|
* Start event.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param {string} msg The info message.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.UnarchiveStartEvent = class extends bitjs.archive.UnarchiveEvent {
|
|
|
|
bitjs.archive.UnarchiveStartEvent = function() {
|
|
|
|
constructor() {
|
|
|
|
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.START);
|
|
|
|
super(bitjs.archive.UnarchiveEvent.Type.START);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
bitjs.inherits(bitjs.archive.UnarchiveStartEvent, bitjs.archive.UnarchiveEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Finish event.
|
|
|
|
* Finish event.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param {string} msg The info message.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.UnarchiveFinishEvent = class extends bitjs.archive.UnarchiveEvent {
|
|
|
|
bitjs.archive.UnarchiveFinishEvent = function() {
|
|
|
|
constructor() {
|
|
|
|
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.FINISH);
|
|
|
|
super(bitjs.archive.UnarchiveEvent.Type.FINISH);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
bitjs.inherits(bitjs.archive.UnarchiveFinishEvent, bitjs.archive.UnarchiveEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Progress event.
|
|
|
|
* Progress event.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.UnarchiveProgressEvent = class extends bitjs.archive.UnarchiveEvent {
|
|
|
|
bitjs.archive.UnarchiveProgressEvent = function(
|
|
|
|
/**
|
|
|
|
currentFilename,
|
|
|
|
* @param {string} currentFilename
|
|
|
|
currentFileNumber,
|
|
|
|
* @param {number} currentFileNumber
|
|
|
|
currentBytesUnarchivedInFile,
|
|
|
|
* @param {number} currentBytesUnarchivedInFile
|
|
|
|
currentBytesUnarchived,
|
|
|
|
* @param {number} currentBytesUnarchived
|
|
|
|
totalUncompressedBytesInArchive,
|
|
|
|
* @param {number} totalUncompressedBytesInArchive
|
|
|
|
totalFilesInArchive) {
|
|
|
|
* @param {number} totalFilesInArchive
|
|
|
|
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.PROGRESS);
|
|
|
|
* @param {number} totalCompressedBytesRead
|
|
|
|
|
|
|
|
*/
|
|
|
|
this.currentFilename = currentFilename;
|
|
|
|
constructor(currentFilename, currentFileNumber, currentBytesUnarchivedInFile,
|
|
|
|
this.currentFileNumber = currentFileNumber;
|
|
|
|
currentBytesUnarchived, totalUncompressedBytesInArchive, totalFilesInArchive,
|
|
|
|
this.currentBytesUnarchivedInFile = currentBytesUnarchivedInFile;
|
|
|
|
totalCompressedBytesRead) {
|
|
|
|
this.totalFilesInArchive = totalFilesInArchive;
|
|
|
|
super(bitjs.archive.UnarchiveEvent.Type.PROGRESS);
|
|
|
|
this.currentBytesUnarchived = currentBytesUnarchived;
|
|
|
|
|
|
|
|
this.totalUncompressedBytesInArchive = totalUncompressedBytesInArchive;
|
|
|
|
this.currentFilename = currentFilename;
|
|
|
|
};
|
|
|
|
this.currentFileNumber = currentFileNumber;
|
|
|
|
bitjs.inherits(bitjs.archive.UnarchiveProgressEvent, bitjs.archive.UnarchiveEvent);
|
|
|
|
this.currentBytesUnarchivedInFile = currentBytesUnarchivedInFile;
|
|
|
|
|
|
|
|
this.totalFilesInArchive = totalFilesInArchive;
|
|
|
|
|
|
|
|
this.currentBytesUnarchived = currentBytesUnarchived;
|
|
|
|
|
|
|
|
this.totalUncompressedBytesInArchive = totalUncompressedBytesInArchive;
|
|
|
|
|
|
|
|
this.totalCompressedBytesRead = totalCompressedBytesRead;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Extract event.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
bitjs.archive.UnarchiveExtractEvent = class extends bitjs.archive.UnarchiveEvent {
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param {UnarchivedFile} unarchivedFile
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
constructor(unarchivedFile) {
|
|
|
|
|
|
|
|
super(bitjs.archive.UnarchiveEvent.Type.EXTRACT);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @type {UnarchivedFile}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.unarchivedFile = unarchivedFile;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* All extracted files returned by an Unarchiver will implement
|
|
|
|
* All extracted files returned by an Unarchiver will implement
|
|
|
@ -150,190 +170,185 @@ bitjs.archive.UnarchiveExtractEvent = class extends bitjs.archive.UnarchiveEvent
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Base class for all Unarchivers.
|
|
|
|
* Extract event.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.Unarchiver = class {
|
|
|
|
bitjs.archive.UnarchiveExtractEvent = function(unarchivedFile) {
|
|
|
|
|
|
|
|
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.EXTRACT);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param {ArrayBuffer} arrayBuffer The Array Buffer.
|
|
|
|
* @type {UnarchivedFile}
|
|
|
|
* @param {string} opt_pathToBitJS Optional string for where the BitJS files are located.
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
constructor(arrayBuffer, opt_pathToBitJS) {
|
|
|
|
this.unarchivedFile = unarchivedFile;
|
|
|
|
/**
|
|
|
|
};
|
|
|
|
* The ArrayBuffer object.
|
|
|
|
bitjs.inherits(bitjs.archive.UnarchiveExtractEvent, bitjs.archive.UnarchiveEvent);
|
|
|
|
* @type {ArrayBuffer}
|
|
|
|
|
|
|
|
* @protected
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.ab = arrayBuffer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* The path to the BitJS files.
|
|
|
|
|
|
|
|
* @type {string}
|
|
|
|
|
|
|
|
* @private
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.pathToBitJS_ = opt_pathToBitJS || '/';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* A map from event type to an array of listeners.
|
|
|
|
|
|
|
|
* @type {Map.<string, Array>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.listeners_ = {};
|
|
|
|
|
|
|
|
for (let type in bitjs.archive.UnarchiveEvent.Type) {
|
|
|
|
|
|
|
|
this.listeners_[bitjs.archive.UnarchiveEvent.Type[type]] = [];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Private web worker initialized during start().
|
|
|
|
|
|
|
|
* @type {Worker}
|
|
|
|
|
|
|
|
* @private
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.worker_ = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Base class for all Unarchivers.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param {ArrayBuffer} arrayBuffer The Array Buffer.
|
|
|
|
|
|
|
|
* @param {string} opt_pathToBitJS Optional string for where the BitJS files are located.
|
|
|
|
|
|
|
|
* @constructor
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
bitjs.archive.Unarchiver = function(arrayBuffer, opt_pathToBitJS) {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* This method must be overridden by the subclass to return the script filename.
|
|
|
|
* The ArrayBuffer object.
|
|
|
|
* @return {string} The script filename.
|
|
|
|
* @type {ArrayBuffer}
|
|
|
|
* @protected.
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
getScriptFileName() {
|
|
|
|
this.ab = arrayBuffer;
|
|
|
|
throw 'Subclasses of AbstractUnarchiver must overload getScriptFileName()';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Adds an event listener for UnarchiveEvents.
|
|
|
|
* The path to the BitJS files.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
* @param {string} Event type.
|
|
|
|
* @private
|
|
|
|
* @param {function} An event handler function.
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
addEventListener(type, listener) {
|
|
|
|
this.pathToBitJS_ = opt_pathToBitJS || '/';
|
|
|
|
if (type in this.listeners_) {
|
|
|
|
|
|
|
|
if (this.listeners_[type].indexOf(listener) == -1) {
|
|
|
|
|
|
|
|
this.listeners_[type].push(listener);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Removes an event listener.
|
|
|
|
* A map from event type to an array of listeners.
|
|
|
|
*
|
|
|
|
* @type {Map.<string, Array>}
|
|
|
|
* @param {string} Event type.
|
|
|
|
|
|
|
|
* @param {EventListener|function} An event listener or handler function.
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
removeEventListener(type, listener) {
|
|
|
|
this.listeners_ = {};
|
|
|
|
if (type in this.listeners_) {
|
|
|
|
for (var type in bitjs.archive.UnarchiveEvent.Type) {
|
|
|
|
const index = this.listeners_[type].indexOf(listener);
|
|
|
|
this.listeners_[bitjs.archive.UnarchiveEvent.Type[type]] = [];
|
|
|
|
if (index != -1) {
|
|
|
|
|
|
|
|
this.listeners_[type].splice(index, 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Receive an event and pass it to the listener functions.
|
|
|
|
* Private web worker initialized during start().
|
|
|
|
*
|
|
|
|
* @type {Worker}
|
|
|
|
* @param {bitjs.archive.UnarchiveEvent} e
|
|
|
|
* @private
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.Unarchiver.prototype.worker_ = null;
|
|
|
|
handleWorkerEvent_(e) {
|
|
|
|
|
|
|
|
if ((e instanceof bitjs.archive.UnarchiveEvent || e.type) &&
|
|
|
|
|
|
|
|
this.listeners_[e.type] instanceof Array) {
|
|
|
|
|
|
|
|
this.listeners_[e.type].forEach(function (listener) { listener(e) });
|
|
|
|
|
|
|
|
if (e.type == bitjs.archive.UnarchiveEvent.Type.FINISH) {
|
|
|
|
|
|
|
|
this.worker_.terminate();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Starts the unarchive in a separate Web Worker thread and returns immediately.
|
|
|
|
* This method must be overridden by the subclass to return the script filename.
|
|
|
|
*/
|
|
|
|
* @return {string} The script filename.
|
|
|
|
start() {
|
|
|
|
* @protected.
|
|
|
|
const me = this;
|
|
|
|
*/
|
|
|
|
const scriptFileName = this.pathToBitJS_ + this.getScriptFileName();
|
|
|
|
bitjs.archive.Unarchiver.prototype.getScriptFileName = function() {
|
|
|
|
if (scriptFileName) {
|
|
|
|
throw 'Subclasses of AbstractUnarchiver must overload getScriptFileName()';
|
|
|
|
this.worker_ = new Worker(scriptFileName);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.worker_.onerror = function(e) {
|
|
|
|
/**
|
|
|
|
console.log('Worker error: message = ' + e.message);
|
|
|
|
* Adds an event listener for UnarchiveEvents.
|
|
|
|
throw e;
|
|
|
|
*
|
|
|
|
};
|
|
|
|
* @param {string} Event type.
|
|
|
|
|
|
|
|
* @param {function} An event handler function.
|
|
|
|
this.worker_.onmessage = function(e) {
|
|
|
|
*/
|
|
|
|
if (typeof e.data == 'string') {
|
|
|
|
bitjs.archive.Unarchiver.prototype.addEventListener = function(type, listener) {
|
|
|
|
// Just log any strings the workers pump our way.
|
|
|
|
if (type in this.listeners_) {
|
|
|
|
console.log(e.data);
|
|
|
|
if (this.listeners_[type].indexOf(listener) == -1) {
|
|
|
|
} else {
|
|
|
|
this.listeners_[type].push(listener);
|
|
|
|
// Assume that it is an UnarchiveEvent. Some browsers preserve the 'type'
|
|
|
|
|
|
|
|
// so that instanceof UnarchiveEvent returns true, but others do not.
|
|
|
|
|
|
|
|
me.handleWorkerEvent_(e.data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ab = this.ab;
|
|
|
|
|
|
|
|
this.worker_.postMessage({
|
|
|
|
|
|
|
|
file: ab,
|
|
|
|
|
|
|
|
logToConsole: false,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
this.ab = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Adds more bytes to the unarchiver's Worker thread.
|
|
|
|
* Removes an event listener.
|
|
|
|
*/
|
|
|
|
*
|
|
|
|
update(ab) {
|
|
|
|
* @param {string} Event type.
|
|
|
|
if (this.worker_) {
|
|
|
|
* @param {EventListener|function} An event listener or handler function.
|
|
|
|
this.worker_.postMessage({bytes: ab});
|
|
|
|
*/
|
|
|
|
|
|
|
|
bitjs.archive.Unarchiver.prototype.removeEventListener = function(type, listener) {
|
|
|
|
|
|
|
|
if (type in this.listeners_) {
|
|
|
|
|
|
|
|
var index = this.listeners_[type].indexOf(listener);
|
|
|
|
|
|
|
|
if (index != -1) {
|
|
|
|
|
|
|
|
this.listeners_[type].splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Terminates the Web Worker for this Unarchiver and returns immediately.
|
|
|
|
* Receive an event and pass it to the listener functions.
|
|
|
|
*/
|
|
|
|
*
|
|
|
|
stop() {
|
|
|
|
* @param {bitjs.archive.UnarchiveEvent} e
|
|
|
|
if (this.worker_) {
|
|
|
|
* @private
|
|
|
|
this.worker_.terminate();
|
|
|
|
*/
|
|
|
|
|
|
|
|
bitjs.archive.Unarchiver.prototype.handleWorkerEvent_ = function(e) {
|
|
|
|
|
|
|
|
if ((e instanceof bitjs.archive.UnarchiveEvent || e.type) &&
|
|
|
|
|
|
|
|
this.listeners_[e.type] instanceof Array) {
|
|
|
|
|
|
|
|
this.listeners_[e.type].forEach(function (listener) { listener(e) });
|
|
|
|
|
|
|
|
if (e.type == bitjs.archive.UnarchiveEvent.Type.FINISH) {
|
|
|
|
|
|
|
|
this.worker_.terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Starts the unarchive in a separate Web Worker thread and returns immediately.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
bitjs.archive.Unarchiver.prototype.start = function() {
|
|
|
|
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
var scriptFileName = this.pathToBitJS_ + this.getScriptFileName();
|
|
|
|
|
|
|
|
if (scriptFileName) {
|
|
|
|
|
|
|
|
this.worker_ = new Worker(scriptFileName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.worker_.onerror = function(e) {
|
|
|
|
|
|
|
|
console.log('Worker error: message = ' + e.message);
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.worker_.onmessage = function(e) {
|
|
|
|
|
|
|
|
if (typeof e.data == 'string') {
|
|
|
|
|
|
|
|
// Just log any strings the workers pump our way.
|
|
|
|
|
|
|
|
console.log(e.data);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Assume that it is an UnarchiveEvent. Some browsers preserve the 'type'
|
|
|
|
|
|
|
|
// so that instanceof UnarchiveEvent returns true, but others do not.
|
|
|
|
|
|
|
|
me.handleWorkerEvent_(e.data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.worker_.postMessage({file: this.ab});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Unzipper
|
|
|
|
* Terminates the Web Worker for this Unarchiver and returns immediately.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.Unzipper = class extends bitjs.archive.Unarchiver {
|
|
|
|
bitjs.archive.Unarchiver.prototype.stop = function() {
|
|
|
|
constructor(arrayBuffer, opt_pathToBitJS) {
|
|
|
|
if (this.worker_) {
|
|
|
|
super(arrayBuffer, opt_pathToBitJS);
|
|
|
|
this.worker_.terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
getScriptFileName() { return 'archive/unzip.js'; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Unzipper
|
|
|
|
|
|
|
|
* @extends {bitjs.archive.Unarchiver}
|
|
|
|
|
|
|
|
* @constructor
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
bitjs.archive.Unzipper = function(arrayBuffer, opt_pathToBitJS) {
|
|
|
|
|
|
|
|
bitjs.base(this, arrayBuffer, opt_pathToBitJS);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
bitjs.inherits(bitjs.archive.Unzipper, bitjs.archive.Unarchiver);
|
|
|
|
|
|
|
|
bitjs.archive.Unzipper.prototype.getScriptFileName = function() { return 'unzip.js' };
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Unrarrer
|
|
|
|
* Unrarrer
|
|
|
|
|
|
|
|
* @extends {bitjs.archive.Unarchiver}
|
|
|
|
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.Unrarrer = class extends bitjs.archive.Unarchiver {
|
|
|
|
bitjs.archive.Unrarrer = function(arrayBuffer, opt_pathToBitJS) {
|
|
|
|
constructor(arrayBuffer, opt_pathToBitJS) {
|
|
|
|
bitjs.base(this, arrayBuffer, opt_pathToBitJS);
|
|
|
|
super(arrayBuffer, opt_pathToBitJS);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
bitjs.inherits(bitjs.archive.Unrarrer, bitjs.archive.Unarchiver);
|
|
|
|
|
|
|
|
bitjs.archive.Unrarrer.prototype.getScriptFileName = function() { return 'unrar.js' };
|
|
|
|
getScriptFileName() { return 'archive/unrar.js'; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Untarrer
|
|
|
|
* Untarrer
|
|
|
|
* @extends {bitjs.archive.Unarchiver}
|
|
|
|
* @extends {bitjs.archive.Unarchiver}
|
|
|
|
* @constructor
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.Untarrer = class extends bitjs.archive.Unarchiver {
|
|
|
|
bitjs.archive.Untarrer = function(arrayBuffer, opt_pathToBitJS) {
|
|
|
|
constructor(arrayBuffer, opt_pathToBitJS) {
|
|
|
|
bitjs.base(this, arrayBuffer, opt_pathToBitJS);
|
|
|
|
super(arrayBuffer, opt_pathToBitJS);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
bitjs.inherits(bitjs.archive.Untarrer, bitjs.archive.Unarchiver);
|
|
|
|
|
|
|
|
bitjs.archive.Untarrer.prototype.getScriptFileName = function() { return 'untar.js' };
|
|
|
|
getScriptFileName() { return 'archive/untar.js'; };
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Factory method that creates an unarchiver based on the byte signature found
|
|
|
|
* Factory method that creates an unarchiver based on the byte signature found
|
|
|
@ -343,20 +358,18 @@ bitjs.archive.Untarrer = class extends bitjs.archive.Unarchiver {
|
|
|
|
* @return {bitjs.archive.Unarchiver}
|
|
|
|
* @return {bitjs.archive.Unarchiver}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bitjs.archive.GetUnarchiver = function(ab, opt_pathToBitJS) {
|
|
|
|
bitjs.archive.GetUnarchiver = function(ab, opt_pathToBitJS) {
|
|
|
|
if (ab.byteLength < 10) {
|
|
|
|
var unarchiver = null;
|
|
|
|
return null;
|
|
|
|
var pathToBitJS = opt_pathToBitJS || '';
|
|
|
|
}
|
|
|
|
var h = new Uint8Array(ab, 0, 10);
|
|
|
|
|
|
|
|
|
|
|
|
let unarchiver = null;
|
|
|
|
|
|
|
|
const pathToBitJS = opt_pathToBitJS || '';
|
|
|
|
|
|
|
|
const h = new Uint8Array(ab, 0, 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (h[0] == 0x52 && h[1] == 0x61 && h[2] == 0x72 && h[3] == 0x21) { // Rar!
|
|
|
|
if (h[0] == 0x52 && h[1] == 0x61 && h[2] == 0x72 && h[3] == 0x21) { // Rar!
|
|
|
|
unarchiver = new bitjs.archive.Unrarrer(ab, pathToBitJS);
|
|
|
|
unarchiver = new bitjs.archive.Unrarrer(ab, pathToBitJS);
|
|
|
|
} else if (h[0] == 0x50 && h[1] == 0x4B) { // PK (Zip)
|
|
|
|
} else if (h[0] == 80 && h[1] == 75) { // PK (Zip)
|
|
|
|
unarchiver = new bitjs.archive.Unzipper(ab, pathToBitJS);
|
|
|
|
unarchiver = new bitjs.archive.Unzipper(ab, pathToBitJS);
|
|
|
|
} else { // Try with tar
|
|
|
|
} else { // Try with tar
|
|
|
|
unarchiver = new bitjs.archive.Untarrer(ab, pathToBitJS);
|
|
|
|
unarchiver = new bitjs.archive.Untarrer(ab, pathToBitJS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unarchiver;
|
|
|
|
return unarchiver;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})();
|