basic local file opening example, now using bitjs

instead of zip.js for cbz & cbr support.
This commit is contained in:
Bala Clark 2013-03-26 21:41:43 +01:00
parent 291b1e411f
commit a21d7f2f4c
59 changed files with 2555 additions and 9042 deletions

View file

@ -1,79 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<title>App</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<script src="../lib/zip.js/zip.js"></script>
<script src="../lib/zip.js/zip-ext.js"></script>
<script src="../lib/zip.js/zip-fs.js"></script>
<script src="../lib/underscore.js"></script>
<script src="../lib/ComicBook.min.js"></script>
<link rel="stylesheet" href="../css/reset.css">
<link rel="stylesheet" href="../css/Aristo/css/Aristo/Aristo.css">
<link rel="stylesheet" href="../css/styles.css">
</head>
<body>
<p id='zipstatus' style="margin:10px"></p>
<canvas id="comic"></canvas>
<script>
(function () {
'use strict';
var book, cbz_url = 'http://localhost/cbr/examples/goldenboy.cbz',
pages = [],
status = $('#zipstatus');
zip.workerScriptsPath = '/cbr/lib/zip.js/';
status.text('opening ' + cbz_url);
zip.createReader(new zip.HttpReader(cbz_url), function (zipReader) {
zipReader.getEntries(function (entries) {
var image_entries = [];
entries.forEach(function (entry, i) {
if (entry.filename.match(/\.(jpg|JPG)$/)) {
image_entries.push(entry);
}
});
image_entries.forEach(function (image_entry, i) {
image_entry.getData(new zip.BlobWriter('image/jpeg'), function (blob) {
status.text('getting page url ' + i);
pages.push({ index: i, url: window.URL.createObjectURL(blob) });
if (pages.length === image_entries.length) {
pages = _.sortBy(pages, function (page) {
return page.index;
});
status.remove();
drawBook(pages);
}
});
});
});
});
function drawBook(pages) {
var book = new ComicBook('comic', _.pluck(pages, 'url'));
book.draw();
$(window).resize(function(event) {
book.draw();
});
}
}());
</script>
</body>
</html>

106
examples/file.html Normal file
View file

@ -0,0 +1,106 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<title>App</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<script src="../lib/bitjs/io.js"></script>
<script src="../lib/bitjs/archive.js"></script>
<script src="../lib/ComicBook.min.js"></script>
<link rel="stylesheet" href="../css/reset.css">
<link rel="stylesheet" href="../css/Aristo/css/Aristo/Aristo.css">
<link rel="stylesheet" href="../css/styles.css">
<style>
.progress, .bar {
width: 200px;
height: 1em;
display: inline-block;
}
.progress {
border: solid 1px;
}
.bar {
background-color: gray;
width: 0;
}
</style>
</head>
<body>
<div id="filepicker" style="margin:10px">
<input type="file" id="open">
<div id="progressbar" style="display:none">
opening...
<br>
<span class="progress"><span class="bar"></span></span>
</div>
</div>
<canvas id="comic" style="display:none"></canvas>
<script>
(function () {
'use strict';
var pages = [], $progressbar = $('.bar');
bitjs.archive.Unrarrer.prototype.getScriptFileName = function() { return '/cbr/lib/bitjs/unrar.js' }
bitjs.archive.Unzipper.prototype.getScriptFileName = function() { return '/cbr/lib/bitjs/unzip.js' }
$('#open').on('change', function () {
var fr = new FileReader();
var file = this.files[0];
var archive_class = ({ cbz: 'Unzipper', cbr: 'Unrarrer' })[file.name.match(/\.(cbz|cbr)$/)[1]];
$('#open').hide();
$('#progressbar').show();
fr.onload = function () {
var done = false;
var ua = new bitjs.archive[archive_class](this.result);
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, function (e) {
var progress = Math.floor(e.currentBytesUnarchived / e.totalUncompressedBytesInArchive * 100);
$progressbar.css('width', progress + '%');
if (progress == 100 && !done) {
drawBook(pages);
done = true;
}
});
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.EXTRACT, function (e) {
if (e.unarchivedFile.filename.match(/jpg$/)) {
var url = window.URL.createObjectURL(new Blob([e.unarchivedFile.fileData], { type: 'image/jpeg' } ));
pages.push(url);
// $('body').append($('<image>').attr('src', url).css('width', '200px'));
}
});
ua.start();
};
fr.readAsArrayBuffer(file);
});
function drawBook(pages) {
var book = new ComicBook('comic', pages);
$('#filepicker').hide();
$('#comic').show();
book.draw();
$(window).resize(function(event) {
book.draw();
});
}
}());
</script>
</body>
</html>

1
lib/bitjs/README.txt Executable file
View file

@ -0,0 +1 @@
bitjs: Binary Tools for JavaScript

340
lib/bitjs/archive.js Executable file
View file

@ -0,0 +1,340 @@
/**
* archive.js
*
* Provides base functionality for unarchiving.
*
* Licensed under the MIT License
*
* Copyright(c) 2011 Google Inc.
*/
var bitjs = bitjs || {};
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.
*
* @param {string} type The event type.
* @constructor
*/
bitjs.archive.UnarchiveEvent = function(type) {
/**
* The event type.
*
* @type {string}
*/
this.type = type;
};
/**
* The UnarchiveEvent types.
*/
bitjs.archive.UnarchiveEvent.Type = {
START: 'start',
PROGRESS: 'progress',
EXTRACT: 'extract',
FINISH: 'finish',
INFO: 'info',
ERROR: 'error'
};
/**
* Useful for passing info up to the client (for debugging).
*
* @param {string} msg The info message.
*/
bitjs.archive.UnarchiveInfoEvent = function(msg) {
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.INFO);
/**
* The information message.
*
* @type {string}
*/
this.msg = msg;
};
bitjs.inherits(bitjs.archive.UnarchiveInfoEvent, bitjs.archive.UnarchiveEvent);
/**
* An unrecoverable error has occured.
*
* @param {string} msg The error message.
*/
bitjs.archive.UnarchiveErrorEvent = function(msg) {
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.ERROR);
/**
* The information message.
*
* @type {string}
*/
this.msg = msg;
};
bitjs.inherits(bitjs.archive.UnarchiveErrorEvent, bitjs.archive.UnarchiveEvent);
/**
* Start event.
*
* @param {string} msg The info message.
*/
bitjs.archive.UnarchiveStartEvent = function() {
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.START);
};
bitjs.inherits(bitjs.archive.UnarchiveStartEvent, bitjs.archive.UnarchiveEvent);
/**
* Finish event.
*
* @param {string} msg The info message.
*/
bitjs.archive.UnarchiveFinishEvent = function() {
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.FINISH);
};
bitjs.inherits(bitjs.archive.UnarchiveFinishEvent, bitjs.archive.UnarchiveEvent);
/**
* Progress event.
*/
bitjs.archive.UnarchiveProgressEvent = function(
currentFilename,
currentFileNumber,
currentBytesUnarchivedInFile,
currentBytesUnarchived,
totalUncompressedBytesInArchive,
totalFilesInArchive) {
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.PROGRESS);
this.currentFilename = currentFilename;
this.currentFileNumber = currentFileNumber;
this.currentBytesUnarchivedInFile = currentBytesUnarchivedInFile;
this.totalFilesInArchive = totalFilesInArchive;
this.currentBytesUnarchived = currentBytesUnarchived;
this.totalUncompressedBytesInArchive = totalUncompressedBytesInArchive;
};
bitjs.inherits(bitjs.archive.UnarchiveProgressEvent, bitjs.archive.UnarchiveEvent);
/**
* All extracted files returned by an Unarchiver will implement
* the following interface:
*
* interface UnarchivedFile {
* string filename
* TypedArray fileData
* }
*
*/
/**
* Extract event.
*/
bitjs.archive.UnarchiveExtractEvent = function(unarchivedFile) {
bitjs.base(this, bitjs.archive.UnarchiveEvent.Type.EXTRACT);
/**
* @type {UnarchivedFile}
*/
this.unarchivedFile = unarchivedFile;
};
bitjs.inherits(bitjs.archive.UnarchiveExtractEvent, bitjs.archive.UnarchiveEvent);
/**
* 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) {
/**
* The ArrayBuffer object.
* @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 (var type in bitjs.archive.UnarchiveEvent.Type) {
this.listeners_[bitjs.archive.UnarchiveEvent.Type[type]] = [];
}
};
/**
* Private web worker initialized during start().
* @type {Worker}
* @private
*/
bitjs.archive.Unarchiver.prototype.worker_ = null;
/**
* This method must be overridden by the subclass to return the script filename.
* @return {string} The script filename.
* @protected.
*/
bitjs.archive.Unarchiver.prototype.getScriptFileName = function() {
throw 'Subclasses of AbstractUnarchiver must overload getScriptFileName()';
};
/**
* Adds an event listener for UnarchiveEvents.
*
* @param {string} Event type.
* @param {function} An event handler function.
*/
bitjs.archive.Unarchiver.prototype.addEventListener = function(type, listener) {
if (type in this.listeners_) {
if (this.listeners_[type].indexOf(listener) == -1) {
this.listeners_[type].push(listener);
}
}
};
/**
* Removes an event listener.
*
* @param {string} Event type.
* @param {EventListener|function} An event listener or handler function.
*/
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);
}
}
};
/**
* Receive an event and pass it to the listener functions.
*
* @param {bitjs.archive.UnarchiveEvent} e
* @private
*/
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) });
} 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
* @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
* @extends {bitjs.archive.Unarchiver}
* @constructor
*/
bitjs.archive.Unrarrer = function(arrayBuffer, opt_pathToBitJS) {
bitjs.base(this, arrayBuffer, opt_pathToBitJS);
};
bitjs.inherits(bitjs.archive.Unrarrer, bitjs.archive.Unarchiver);
bitjs.archive.Unrarrer.prototype.getScriptFileName = function() { return 'unrar.js' };
/**
* Untarrer
* @extends {bitjs.archive.Unarchiver}
* @constructor
*/
bitjs.archive.Untarrer = function(arrayBuffer, opt_pathToBitJS) {
bitjs.base(this, arrayBuffer, opt_pathToBitJS);
};
bitjs.inherits(bitjs.archive.Untarrer, bitjs.archive.Unarchiver);
bitjs.archive.Untarrer.prototype.getScriptFileName = function() { return 'untar.js' };
})();

79
lib/bitjs/drive.html Executable file
View file

@ -0,0 +1,79 @@
<!DOCTYPE html>
<html><head>
<script src="io.js"></script>
<script src="archive.js"></script>
</head>
<body>
<input id='filechooser' type='file'></input>
</body>
<script>
function assertTrue(a) {
if (!a) {
alert("'" + a + "' not true");
}
}
function assertEquals(a,b) {
if (a != b) {
alert("'" + a + "' != '" + b + "'")
}
}
function testAddRemoveEventListeners() {
var progress = function(e) { alert('progress') };
var progress2 = function(e) { alert('progress2') };
var ua = new bitjs.archive.Unarchiver(null);
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress);
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress);
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress);
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress2);
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress2);
assertEquals(2, ua.listeners_[bitjs.archive.UnarchiveEvent.Type.PROGRESS].length);
ua.removeEventListener(bitjs.archive.UnarchiveEvent.Type.START, progress);
assertEquals(2, ua.listeners_[bitjs.archive.UnarchiveEvent.Type.PROGRESS].length);
ua.removeEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress);
assertEquals(1, ua.listeners_[bitjs.archive.UnarchiveEvent.Type.PROGRESS].length);
}
function testAbstractUnarchiveThrows() {
var ua = new bitjs.archive.Unarchiver(null);
try {
ua.start();
alert("unarchive() didn't throw");
} catch(e) {}
}
// unit tests
// testAddRemoveEventListeners();
// testAbstractUnarchiveThrows();
// var uz = new bitjs.archive.Unzipper(null);
document.body.querySelector("#filechooser").addEventListener("change",
function(evt) {
var inp = evt.target;
var filelist = inp.files;
if (filelist.length == 1) {
var blob = filelist[0];
var fr = new FileReader();
fr.onload = function() {
var ua = new bitjs.archive.Unzipper(fr.result);
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.INFO, function(e) {
console.log("info: " + e.msg);
})
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, function(e) {
console.log("progress: " + e.msg);
})
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.EXTRACT, function(e) {
console.log("extract: " + e.unarchiveFile.filename);
})
ua.start();
};
fr.readAsArrayBuffer(blob);
}
}, false);
</script>
</html>

310
lib/bitjs/io.js Executable file
View file

@ -0,0 +1,310 @@
/*
* io.js
*
* Provides readers for bit/byte streams (reading) and a byte buffer (writing).
*
* Licensed under the MIT License
*
* Copyright(c) 2011 Google Inc.
* Copyright(c) 2011 antimatter15
*/
var bitjs = bitjs || {};
bitjs.io = bitjs.io || {};
(function() {
// mask for getting the Nth bit (zero-based)
bitjs.BIT = [ 0x01, 0x02, 0x04, 0x08,
0x10, 0x20, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800,
0x1000, 0x2000, 0x4000, 0x8000];
// mask for getting N number of bits (0-8)
var BITMASK = [0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF ];
/**
* This bit stream peeks and consumes bits out of a binary stream.
*
* {ArrayBuffer} ab An ArrayBuffer object or a Uint8Array.
* {boolean} rtl Whether the stream reads bits from the byte starting
* from bit 7 to 0 (true) or bit 0 to 7 (false).
* {Number} opt_offset The offset into the ArrayBuffer
* {Number} opt_length The length of this BitStream
*/
bitjs.io.BitStream = function(ab, rtl, opt_offset, opt_length) {
if (!ab || !ab.toString || ab.toString() !== "[object ArrayBuffer]") {
throw "Error! BitArray constructed with an invalid ArrayBuffer object";
}
var offset = opt_offset || 0;
var length = opt_length || ab.byteLength;
this.bytes = new Uint8Array(ab, offset, length);
this.bytePtr = 0; // tracks which byte we are on
this.bitPtr = 0; // tracks which bit we are on (can have values 0 through 7)
this.peekBits = rtl ? this.peekBits_rtl : this.peekBits_ltr;
};
// byte0 byte1 byte2 byte3
// 7......0 | 7......0 | 7......0 | 7......0
//
// The bit pointer starts at bit0 of byte0 and moves left until it reaches
// bit7 of byte0, then jumps to bit0 of byte1, etc.
bitjs.io.BitStream.prototype.peekBits_ltr = function(n, movePointers) {
if (n <= 0 || typeof n != typeof 1) {
return 0;
}
var movePointers = movePointers || false,
bytePtr = this.bytePtr,
bitPtr = this.bitPtr,
result = 0,
bitsIn = 0,
bytes = this.bytes;
// keep going until we have no more bits left to peek at
// TODO: Consider putting all bits from bytes we will need into a variable and then
// shifting/masking it to just extract the bits we want.
// This could be considerably faster when reading more than 3 or 4 bits at a time.
while (n > 0) {
if (bytePtr >= bytes.length) {
throw "Error! Overflowed the bit stream! n=" + n + ", bytePtr=" + bytePtr + ", bytes.length=" +
bytes.length + ", bitPtr=" + bitPtr;
return -1;
}
var numBitsLeftInThisByte = (8 - bitPtr);
if (n >= numBitsLeftInThisByte) {
var mask = (BITMASK[numBitsLeftInThisByte] << bitPtr);
result |= (((bytes[bytePtr] & mask) >> bitPtr) << bitsIn);
bytePtr++;
bitPtr = 0;
bitsIn += numBitsLeftInThisByte;
n -= numBitsLeftInThisByte;
}
else {
var mask = (BITMASK[n] << bitPtr);
result |= (((bytes[bytePtr] & mask) >> bitPtr) << bitsIn);
bitPtr += n;
bitsIn += n;
n = 0;
}
}
if (movePointers) {
this.bitPtr = bitPtr;
this.bytePtr = bytePtr;
}
return result;
};
// byte0 byte1 byte2 byte3
// 7......0 | 7......0 | 7......0 | 7......0
//
// The bit pointer starts at bit7 of byte0 and moves right until it reaches
// bit0 of byte0, then goes to bit7 of byte1, etc.
bitjs.io.BitStream.prototype.peekBits_rtl = function(n, movePointers) {
if (n <= 0 || typeof n != typeof 1) {
return 0;
}
var movePointers = movePointers || false,
bytePtr = this.bytePtr,
bitPtr = this.bitPtr,
result = 0,
bytes = this.bytes;
// keep going until we have no more bits left to peek at
// TODO: Consider putting all bits from bytes we will need into a variable and then
// shifting/masking it to just extract the bits we want.
// This could be considerably faster when reading more than 3 or 4 bits at a time.
while (n > 0) {
if (bytePtr >= bytes.length) {
throw "Error! Overflowed the bit stream! n=" + n + ", bytePtr=" + bytePtr + ", bytes.length=" +
bytes.length + ", bitPtr=" + bitPtr;
return -1;
}
var numBitsLeftInThisByte = (8 - bitPtr);
if (n >= numBitsLeftInThisByte) {
result <<= numBitsLeftInThisByte;
result |= (BITMASK[numBitsLeftInThisByte] & bytes[bytePtr]);
bytePtr++;
bitPtr = 0;
n -= numBitsLeftInThisByte;
}
else {
result <<= n;
result |= ((bytes[bytePtr] & (BITMASK[n] << (8 - n - bitPtr))) >> (8 - n - bitPtr));
bitPtr += n;
n = 0;
}
}
if (movePointers) {
this.bitPtr = bitPtr;
this.bytePtr = bytePtr;
}
return result;
};
//some voodoo magic
bitjs.io.BitStream.prototype.getBits = function() {
return (((((this.bytes[this.bytePtr] & 0xff) << 16) +
((this.bytes[this.bytePtr+1] & 0xff) << 8) +
((this.bytes[this.bytePtr+2] & 0xff))) >>> (8-this.bitPtr)) & 0xffff);
};
bitjs.io.BitStream.prototype.readBits = function(n) {
return this.peekBits(n, true);
};
// This returns n bytes as a sub-array, advancing the pointer if movePointers
// is true.
// Only use this for uncompressed blocks as this throws away remaining bits in
// the current byte.
bitjs.io.BitStream.prototype.peekBytes = function(n, movePointers) {
if (n <= 0 || typeof n != typeof 1) {
return 0;
}
// from http://tools.ietf.org/html/rfc1951#page-11
// "Any bits of input up to the next byte boundary are ignored."
while (this.bitPtr != 0) {
this.readBits(1);
}
var movePointers = movePointers || false;
var bytePtr = this.bytePtr,
bitPtr = this.bitPtr;
var result = this.bytes.subarray(bytePtr, bytePtr + n);
if (movePointers) {
this.bytePtr += n;
}
return result;
};
bitjs.io.BitStream.prototype.readBytes = function( n ) {
return this.peekBytes(n, true);
};
/**
* This object allows you to peek and consume bytes as numbers and strings
* out of an ArrayBuffer.
*
* This object is much easier to write than the above BitStream since
* everything is byte-aligned.
*
* {ArrayBuffer} ab The ArrayBuffer object.
* {Number} opt_offset The offset into the ArrayBuffer
* {Number} opt_length The length of this BitStream
*/
bitjs.io.ByteStream = function(ab, opt_offset, opt_length) {
var offset = opt_offset || 0;
var length = opt_length || ab.byteLength;
this.bytes = new Uint8Array(ab, offset, length);
this.ptr = 0;
};
// peeks at the next n bytes as an unsigned number but does not advance the pointer
// TODO: This apparently cannot read more than 4 bytes as a number?
bitjs.io.ByteStream.prototype.peekNumber = function( n ) {
// TODO: return error if n would go past the end of the stream?
if (n <= 0 || typeof n != typeof 1)
return -1;
var result = 0;
// read from last byte to first byte and roll them in
var curByte = this.ptr + n - 1;
while (curByte >= this.ptr) {
result <<= 8;
result |= this.bytes[curByte];
--curByte;
}
return result;
};
// returns the next n bytes as an unsigned number (or -1 on error)
// and advances the stream pointer n bytes
bitjs.io.ByteStream.prototype.readNumber = function( n ) {
var num = this.peekNumber( n );
this.ptr += n;
return num;
};
// This returns n bytes as a sub-array, advancing the pointer if movePointers
// is true.
bitjs.io.ByteStream.prototype.peekBytes = function(n, movePointers) {
if (n <= 0 || typeof n != typeof 1) {
return 0;
}
var result = this.bytes.subarray(this.ptr, this.ptr + n);
if (movePointers) {
this.ptr += n;
}
return result;
};
bitjs.io.ByteStream.prototype.readBytes = function( n ) {
return this.peekBytes(n, true);
};
// peeks at the next n bytes as a string but does not advance the pointer
bitjs.io.ByteStream.prototype.peekString = function( n ) {
if (n <= 0 || typeof n != typeof 1) {
return 0;
}
var result = "";
for (var p = this.ptr, end = this.ptr + n; p < end; ++p) {
result += String.fromCharCode(this.bytes[p]);
}
return result;
};
// returns the next n bytes as a string
// and advances the stream pointer n bytes
bitjs.io.ByteStream.prototype.readString = function(n) {
var strToReturn = this.peekString(n);
this.ptr += n;
return strToReturn;
};
/**
* A write-only Byte buffer which uses a Uint8 Typed Array as a backing store.
*/
bitjs.io.ByteBuffer = function(numBytes) {
if (typeof numBytes != typeof 1 || numBytes <= 0) {
throw "Error! ByteBuffer initialized with '" + numBytes + "'";
}
this.data = new Uint8Array(numBytes);
this.ptr = 0;
};
bitjs.io.ByteBuffer.prototype.insertByte = function(b) {
// TODO: throw if byte is invalid?
this.data[this.ptr++] = b;
};
bitjs.io.ByteBuffer.prototype.insertBytes = function(bytes) {
// TODO: throw if bytes is invalid?
this.data.set(bytes, this.ptr);
this.ptr += bytes.length;
};
})();

906
lib/bitjs/unrar.js Executable file
View file

@ -0,0 +1,906 @@
/**
* unrar.js
*
* Copyright(c) 2011 Google Inc.
* Copyright(c) 2011 antimatter15
*
* Reference Documentation:
*
* http://kthoom.googlecode.com/hg/docs/unrar.html
*/
// This file expects to be invoked as a Worker (see onmessage below).
importScripts('io.js');
importScripts('archive.js');
// Progress variables.
var currentFilename = "";
var currentFileNumber = 0;
var currentBytesUnarchivedInFile = 0;
var currentBytesUnarchived = 0;
var totalUncompressedBytesInArchive = 0;
var totalFilesInArchive = 0;
// Helper functions.
var info = function(str) {
postMessage(new bitjs.archive.UnarchiveInfoEvent(str));
};
var err = function(str) {
postMessage(new bitjs.archive.UnarchiveErrorEvent(str));
};
var postProgress = function() {
postMessage(new bitjs.archive.UnarchiveProgressEvent(
currentFilename,
currentFileNumber,
currentBytesUnarchivedInFile,
currentBytesUnarchived,
totalUncompressedBytesInArchive,
totalFilesInArchive));
};
// shows a byte value as its hex representation
var nibble = "0123456789ABCDEF";
var byteValueToHexString = function(num) {
return nibble[num>>4] + nibble[num&0xF];
};
var twoByteValueToHexString = function(num) {
return nibble[(num>>12)&0xF] + nibble[(num>>8)&0xF] + nibble[(num>>4)&0xF] + nibble[num&0xF];
};
// Volume Types
var MARK_HEAD = 0x72,
MAIN_HEAD = 0x73,
FILE_HEAD = 0x74,
COMM_HEAD = 0x75,
AV_HEAD = 0x76,
SUB_HEAD = 0x77,
PROTECT_HEAD = 0x78,
SIGN_HEAD = 0x79,
NEWSUB_HEAD = 0x7a,
ENDARC_HEAD = 0x7b;
// bstream is a bit stream
var RarVolumeHeader = function(bstream) {
var headPos = bstream.bytePtr;
// byte 1,2
info("Rar Volume Header @"+bstream.bytePtr);
this.crc = bstream.readBits(16);
info(" crc=" + this.crc);
// byte 3
this.headType = bstream.readBits(8);
info(" headType=" + this.headType);
// Get flags
// bytes 4,5
this.flags = {};
this.flags.value = bstream.peekBits(16);
info(" flags=" + twoByteValueToHexString(this.flags.value));
switch (this.headType) {
case MAIN_HEAD:
this.flags.MHD_VOLUME = !!bstream.readBits(1);
this.flags.MHD_COMMENT = !!bstream.readBits(1);
this.flags.MHD_LOCK = !!bstream.readBits(1);
this.flags.MHD_SOLID = !!bstream.readBits(1);
this.flags.MHD_PACK_COMMENT = !!bstream.readBits(1);
this.flags.MHD_NEWNUMBERING = this.flags.MHD_PACK_COMMENT;
this.flags.MHD_AV = !!bstream.readBits(1);
this.flags.MHD_PROTECT = !!bstream.readBits(1);
this.flags.MHD_PASSWORD = !!bstream.readBits(1);
this.flags.MHD_FIRSTVOLUME = !!bstream.readBits(1);
this.flags.MHD_ENCRYPTVER = !!bstream.readBits(1);
bstream.readBits(6); // unused
break;
case FILE_HEAD:
this.flags.LHD_SPLIT_BEFORE = !!bstream.readBits(1); // 0x0001
this.flags.LHD_SPLIT_AFTER = !!bstream.readBits(1); // 0x0002
this.flags.LHD_PASSWORD = !!bstream.readBits(1); // 0x0004
this.flags.LHD_COMMENT = !!bstream.readBits(1); // 0x0008
this.flags.LHD_SOLID = !!bstream.readBits(1); // 0x0010
bstream.readBits(3); // unused
this.flags.LHD_LARGE = !!bstream.readBits(1); // 0x0100
this.flags.LHD_UNICODE = !!bstream.readBits(1); // 0x0200
this.flags.LHD_SALT = !!bstream.readBits(1); // 0x0400
this.flags.LHD_VERSION = !!bstream.readBits(1); // 0x0800
this.flags.LHD_EXTTIME = !!bstream.readBits(1); // 0x1000
this.flags.LHD_EXTFLAGS = !!bstream.readBits(1); // 0x2000
bstream.readBits(2); // unused
info(" LHD_SPLIT_BEFORE = " + this.flags.LHD_SPLIT_BEFORE);
break;
default:
bstream.readBits(16);
}
// byte 6,7
this.headSize = bstream.readBits(16);
info(" headSize=" + this.headSize);
switch (this.headType) {
case MAIN_HEAD:
this.highPosAv = bstream.readBits(16);
this.posAv = bstream.readBits(32);
if (this.flags.MHD_ENCRYPTVER) {
this.encryptVer = bstream.readBits(8);
}
info("Found MAIN_HEAD with highPosAv=" + this.highPosAv + ", posAv=" + this.posAv);
break;
case FILE_HEAD:
this.packSize = bstream.readBits(32);
this.unpackedSize = bstream.readBits(32);
this.hostOS = bstream.readBits(8);
this.fileCRC = bstream.readBits(32);
this.fileTime = bstream.readBits(32);
this.unpVer = bstream.readBits(8);
this.method = bstream.readBits(8);
this.nameSize = bstream.readBits(16);
this.fileAttr = bstream.readBits(32);
if (this.flags.LHD_LARGE) {
info("Warning: Reading in LHD_LARGE 64-bit size values");
this.HighPackSize = bstream.readBits(32);
this.HighUnpSize = bstream.readBits(32);
} else {
this.HighPackSize = 0;
this.HighUnpSize = 0;
if (this.unpackedSize == 0xffffffff) {
this.HighUnpSize = 0x7fffffff
this.unpackedSize = 0xffffffff;
}
}
this.fullPackSize = 0;
this.fullUnpackSize = 0;
this.fullPackSize |= this.HighPackSize;
this.fullPackSize <<= 32;
this.fullPackSize |= this.packSize;
// read in filename
this.filename = bstream.readBytes(this.nameSize);
for (var _i = 0, _s = ''; _i < this.filename.length; _i++) {
_s += String.fromCharCode(this.filename[_i]);
}
this.filename = _s;
if (this.flags.LHD_SALT) {
info("Warning: Reading in 64-bit salt value");
this.salt = bstream.readBits(64); // 8 bytes
}
if (this.flags.LHD_EXTTIME) {
// 16-bit flags
var extTimeFlags = bstream.readBits(16);
// this is adapted straight out of arcread.cpp, Archive::ReadHeader()
for (var I = 0; I < 4; ++I) {
var rmode = extTimeFlags >> ((3-I)*4);
if ((rmode & 8)==0)
continue;
if (I!=0)
bstream.readBits(16);
var count = (rmode&3);
for (var J = 0; J < count; ++J)
bstream.readBits(8);
}
}
if (this.flags.LHD_COMMENT) {
info("Found a LHD_COMMENT");
}
while(headPos + this.headSize > bstream.bytePtr) bstream.readBits(1);
info("Found FILE_HEAD with packSize=" + this.packSize + ", unpackedSize= " + this.unpackedSize + ", hostOS=" + this.hostOS + ", unpVer=" + this.unpVer + ", method=" + this.method + ", filename=" + this.filename);
break;
default:
info("Found a header of type 0x" + byteValueToHexString(this.headType));
// skip the rest of the header bytes (for now)
bstream.readBytes( this.headSize - 7 );
break;
}
};
var BLOCK_LZ = 0,
BLOCK_PPM = 1;
var rLDecode = [0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224],
rLBits = [0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5],
rDBitLengthCounts = [4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,14,0,12],
rSDDecode = [0,4,8,16,32,64,128,192],
rSDBits = [2,2,3, 4, 5, 6, 6, 6];
var rDDecode = [0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32,
48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072,
4096, 6144, 8192, 12288, 16384, 24576, 32768, 49152, 65536, 98304,
131072, 196608, 262144, 327680, 393216, 458752, 524288, 589824,
655360, 720896, 786432, 851968, 917504, 983040];
var rDBits = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5,
5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14,
15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16];
var rLOW_DIST_REP_COUNT = 16;
var rNC = 299,
rDC = 60,
rLDC = 17,
rRC = 28,
rBC = 20,
rHUFF_TABLE_SIZE = (rNC+rDC+rRC+rLDC);
var UnpBlockType = BLOCK_LZ;
var UnpOldTable = new Array(rHUFF_TABLE_SIZE);
var BD = { //bitdecode
DecodeLen: new Array(16),
DecodePos: new Array(16),
DecodeNum: new Array(rBC)
};
var LD = { //litdecode
DecodeLen: new Array(16),
DecodePos: new Array(16),
DecodeNum: new Array(rNC)
};
var DD = { //distdecode
DecodeLen: new Array(16),
DecodePos: new Array(16),
DecodeNum: new Array(rDC)
};
var LDD = { //low dist decode
DecodeLen: new Array(16),
DecodePos: new Array(16),
DecodeNum: new Array(rLDC)
};
var RD = { //rep decode
DecodeLen: new Array(16),
DecodePos: new Array(16),
DecodeNum: new Array(rRC)
};
var rBuffer;
// read in Huffman tables for RAR
function RarReadTables(bstream) {
var BitLength = new Array(rBC),
Table = new Array(rHUFF_TABLE_SIZE);
// before we start anything we need to get byte-aligned
bstream.readBits( (8 - bstream.bitPtr) & 0x7 );
if (bstream.readBits(1)) {
info("Error! PPM not implemented yet");
return;
}
if (!bstream.readBits(1)) { //discard old table
for (var i = UnpOldTable.length; i--;) UnpOldTable[i] = 0;
}
// read in bit lengths
for (var I = 0; I < rBC; ++I) {
var Length = bstream.readBits(4);
if (Length == 15) {
var ZeroCount = bstream.readBits(4);
if (ZeroCount == 0) {
BitLength[I] = 15;
}
else {
ZeroCount += 2;
while (ZeroCount-- > 0 && I < rBC)
BitLength[I++] = 0;
--I;
}
}
else {
BitLength[I] = Length;
}
}
// now all 20 bit lengths are obtained, we construct the Huffman Table:
RarMakeDecodeTables(BitLength, 0, BD, rBC);
var TableSize = rHUFF_TABLE_SIZE;
//console.log(DecodeLen, DecodePos, DecodeNum);
for (var i = 0; i < TableSize;) {
var num = RarDecodeNumber(bstream, BD);
if (num < 16) {
Table[i] = (num + UnpOldTable[i]) & 0xf;
i++;
} else if(num < 18) {
var N = (num == 16) ? (bstream.readBits(3) + 3) : (bstream.readBits(7) + 11);
while (N-- > 0 && i < TableSize) {
Table[i] = Table[i - 1];
i++;
}
} else {
var N = (num == 18) ? (bstream.readBits(3) + 3) : (bstream.readBits(7) + 11);
while (N-- > 0 && i < TableSize) {
Table[i++] = 0;
}
}
}
RarMakeDecodeTables(Table, 0, LD, rNC);
RarMakeDecodeTables(Table, rNC, DD, rDC);
RarMakeDecodeTables(Table, rNC + rDC, LDD, rLDC);
RarMakeDecodeTables(Table, rNC + rDC + rLDC, RD, rRC);
for (var i = UnpOldTable.length; i--;) {
UnpOldTable[i] = Table[i];
}
return true;
}
function RarDecodeNumber(bstream, dec) {
var DecodeLen = dec.DecodeLen, DecodePos = dec.DecodePos, DecodeNum = dec.DecodeNum;
var bitField = bstream.getBits() & 0xfffe;
//some sort of rolled out binary search
var bits = ((bitField < DecodeLen[8])?
((bitField < DecodeLen[4])?
((bitField < DecodeLen[2])?
((bitField < DecodeLen[1])?1:2)
:((bitField < DecodeLen[3])?3:4))
:(bitField < DecodeLen[6])?
((bitField < DecodeLen[5])?5:6)
:((bitField < DecodeLen[7])?7:8))
:((bitField < DecodeLen[12])?
((bitField < DecodeLen[10])?
((bitField < DecodeLen[9])?9:10)
:((bitField < DecodeLen[11])?11:12))
:(bitField < DecodeLen[14])?
((bitField < DecodeLen[13])?13:14)
:15));
bstream.readBits(bits);
var N = DecodePos[bits] + ((bitField - DecodeLen[bits -1]) >>> (16 - bits));
return DecodeNum[N];
}
function RarMakeDecodeTables(BitLength, offset, dec, size) {
var DecodeLen = dec.DecodeLen, DecodePos = dec.DecodePos, DecodeNum = dec.DecodeNum;
var LenCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
TmpPos = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
N = 0, M = 0;
for (var i = DecodeNum.length; i--;) DecodeNum[i] = 0;
for (var i = 0; i < size; i++) {
LenCount[BitLength[i + offset] & 0xF]++;
}
LenCount[0] = 0;
TmpPos[0] = 0;
DecodePos[0] = 0;
DecodeLen[0] = 0;
for (var I = 1; I < 16; ++I) {
N = 2 * (N+LenCount[I]);
M = (N << (15-I));
if (M > 0xFFFF)
M = 0xFFFF;
DecodeLen[I] = M;
DecodePos[I] = DecodePos[I-1] + LenCount[I-1];
TmpPos[I] = DecodePos[I];
}
for (I = 0; I < size; ++I)
if (BitLength[I + offset] != 0)
DecodeNum[ TmpPos[ BitLength[offset + I] & 0xF ]++] = I;
}
// TODO: implement
function Unpack15(bstream, Solid) {
info("ERROR! RAR 1.5 compression not supported");
}
function Unpack20(bstream, Solid) {
var destUnpSize = rBuffer.data.length;
var oldDistPtr = 0;
RarReadTables20(bstream);
while (destUnpSize > rBuffer.ptr) {
var num = RarDecodeNumber(bstream, LD);
if (num < 256) {
rBuffer.insertByte(num);
continue;
}
if (num > 269) {
var Length = rLDecode[num -= 270] + 3;
if ((Bits = rLBits[num]) > 0) {
Length += bstream.readBits(Bits);
}
var DistNumber = RarDecodeNumber(bstream, DD);
var Distance = rDDecode[DistNumber] + 1;
if ((Bits = rDBits[DistNumber]) > 0) {
Distance += bstream.readBits(Bits);
}
if (Distance >= 0x2000) {
Length++;
if(Distance >= 0x40000) Length++;
}
lastLength = Length;
lastDist = rOldDist[oldDistPtr++ & 3] = Distance;
RarCopyString(Length, Distance);
continue;
}
if (num == 269) {
RarReadTables20(bstream);
RarUpdateProgress()
continue;
}
if (num == 256) {
lastDist = rOldDist[oldDistPtr++ & 3] = lastDist;
RarCopyString(lastLength, lastDist);
continue;
}
if (num < 261) {
var Distance = rOldDist[(oldDistPtr - (num - 256)) & 3];
var LengthNumber = RarDecodeNumber(bstream, RD);
var Length = rLDecode[LengthNumber] +2;
if ((Bits = rLBits[LengthNumber]) > 0) {
Length += bstream.readBits(Bits);
}
if (Distance >= 0x101) {
Length++;
if (Distance >= 0x2000) {
Length++
if (Distance >= 0x40000) Length++;
}
}
lastLength = Length;
lastDist = rOldDist[oldDistPtr++ & 3] = Distance;
RarCopyString(Length, Distance);
continue;
}
if (num < 270) {
var Distance = rSDDecode[num -= 261] + 1;
if ((Bits = rSDBits[num]) > 0) {
Distance += bstream.readBits(Bits);
}
lastLength = 2;
lastDist = rOldDist[oldDistPtr++ & 3] = Distance;
RarCopyString(2, Distance);
continue;
}
}
RarUpdateProgress()
}
function RarUpdateProgress() {
var change = rBuffer.ptr - currentBytesUnarchivedInFile;
currentBytesUnarchivedInFile = rBuffer.ptr;
currentBytesUnarchived += change;
postProgress();
}
var rNC20 = 298,
rDC20 = 48,
rRC20 = 28,
rBC20 = 19,
rMC20 = 257;
var UnpOldTable20 = new Array(rMC20 * 4);
function RarReadTables20(bstream) {
var BitLength = new Array(rBC20);
var Table = new Array(rMC20 * 4);
var TableSize, N, I;
var AudioBlock = bstream.readBits(1);
if (!bstream.readBits(1))
for (var i = UnpOldTable20.length; i--;) UnpOldTable20[i] = 0;
TableSize = rNC20 + rDC20 + rRC20;
for (var I = 0; I < rBC20; I++)
BitLength[I] = bstream.readBits(4);
RarMakeDecodeTables(BitLength, 0, BD, rBC20);
I = 0;
while (I < TableSize) {
var num = RarDecodeNumber(bstream, BD);
if (num < 16) {
Table[I] = num + UnpOldTable20[I] & 0xf;
I++;
} else if(num == 16) {
N = bstream.readBits(2) + 3;
while (N-- > 0 && I < TableSize) {
Table[I] = Table[I - 1];
I++;
}
} else {
if (num == 17) {
N = bstream.readBits(3) + 3;
} else {
N = bstream.readBits(7) + 11;
}
while (N-- > 0 && I < TableSize) {
Table[I++] = 0;
}
}
}
RarMakeDecodeTables(Table, 0, LD, rNC20);
RarMakeDecodeTables(Table, rNC20, DD, rDC20);
RarMakeDecodeTables(Table, rNC20 + rDC20, RD, rRC20);
for (var i = UnpOldTable20.length; i--;) UnpOldTable20[i] = Table[i];
}
var lowDistRepCount = 0, prevLowDist = 0;
var rOldDist = [0,0,0,0];
var lastDist;
var lastLength;
function Unpack29(bstream, Solid) {
// lazy initialize rDDecode and rDBits
var DDecode = new Array(rDC);
var DBits = new Array(rDC);
var Dist=0,BitLength=0,Slot=0;
for (var I = 0; I < rDBitLengthCounts.length; I++,BitLength++) {
for (var J = 0; J < rDBitLengthCounts[I]; J++,Slot++,Dist+=(1<<BitLength)) {
DDecode[Slot]=Dist;
DBits[Slot]=BitLength;
}
}
var Bits;
//tablesRead = false;
rOldDist = [0,0,0,0]
lastDist = 0;
lastLength = 0;
for (var i = UnpOldTable.length; i--;) UnpOldTable[i] = 0;
// read in Huffman tables
RarReadTables(bstream);
while (true) {
var num = RarDecodeNumber(bstream, LD);
if (num < 256) {
rBuffer.insertByte(num);
continue;
}
if (num >= 271) {
var Length = rLDecode[num -= 271] + 3;
if ((Bits = rLBits[num]) > 0) {
Length += bstream.readBits(Bits);
}
var DistNumber = RarDecodeNumber(bstream, DD);
var Distance = DDecode[DistNumber]+1;
if ((Bits = DBits[DistNumber]) > 0) {
if (DistNumber > 9) {
if (Bits > 4) {
Distance += ((bstream.getBits() >>> (20 - Bits)) << 4);
bstream.readBits(Bits - 4);
//todo: check this
}
if (lowDistRepCount > 0) {
lowDistRepCount--;
Distance += prevLowDist;
} else {
var LowDist = RarDecodeNumber(bstream, LDD);
if (LowDist == 16) {
lowDistRepCount = rLOW_DIST_REP_COUNT - 1;
Distance += prevLowDist;
} else {
Distance += LowDist;
prevLowDist = LowDist;
}
}
} else {
Distance += bstream.readBits(Bits);
}
}
if (Distance >= 0x2000) {
Length++;
if (Distance >= 0x40000) {
Length++;
}
}
RarInsertOldDist(Distance);
RarInsertLastMatch(Length, Distance);
RarCopyString(Length, Distance);
continue;
}
if (num == 256) {
if (!RarReadEndOfBlock(bstream)) break;
continue;
}
if (num == 257) {
//console.log("READVMCODE");
if (!RarReadVMCode(bstream)) break;
continue;
}
if (num == 258) {
if (lastLength != 0) {
RarCopyString(lastLength, lastDist);
}
continue;
}
if (num < 263) {
var DistNum = num - 259;
var Distance = rOldDist[DistNum];
for (var I = DistNum; I > 0; I--) {
rOldDist[I] = rOldDist[I-1];
}
rOldDist[0] = Distance;
var LengthNumber = RarDecodeNumber(bstream, RD);
var Length = rLDecode[LengthNumber] + 2;
if ((Bits = rLBits[LengthNumber]) > 0) {
Length += bstream.readBits(Bits);
}
RarInsertLastMatch(Length, Distance);
RarCopyString(Length, Distance);
continue;
}
if (num < 272) {
var Distance = rSDDecode[num -= 263] + 1;
if ((Bits = rSDBits[num]) > 0) {
Distance += bstream.readBits(Bits);
}
RarInsertOldDist(Distance);
RarInsertLastMatch(2, Distance);
RarCopyString(2, Distance);
continue;
}
}
RarUpdateProgress()
}
function RarReadEndOfBlock(bstream) {
RarUpdateProgress()
var NewTable = false, NewFile = false;
if (bstream.readBits(1)) {
NewTable = true;
} else {
NewFile = true;
NewTable = !!bstream.readBits(1);
}
//tablesRead = !NewTable;
return !(NewFile || NewTable && !RarReadTables(bstream));
}
function RarReadVMCode(bstream) {
var FirstByte = bstream.readBits(8);
var Length = (FirstByte & 7) + 1;
if (Length == 7) {
Length = bstream.readBits(8) + 7;
} else if(Length == 8) {
Length = bstream.readBits(16);
}
var vmCode = [];
for(var I = 0; I < Length; I++) {
//do something here with cheking readbuf
vmCode.push(bstream.readBits(8));
}
return RarAddVMCode(FirstByte, vmCode, Length);
}
function RarAddVMCode(firstByte, vmCode, length) {
//console.log(vmCode);
if (vmCode.length > 0) {
info("Error! RarVM not supported yet!");
}
return true;
}
function RarInsertLastMatch(length, distance) {
lastDist = distance;
lastLength = length;
}
function RarInsertOldDist(distance) {
rOldDist.splice(3,1);
rOldDist.splice(0,0,distance);
}
//this is the real function, the other one is for debugging
function RarCopyString(length, distance) {
var destPtr = rBuffer.ptr - distance;
if(destPtr < 0){
var l = rOldBuffers.length;
while(destPtr < 0){
destPtr = rOldBuffers[--l].data.length + destPtr
}
//TODO: lets hope that it never needs to read beyond file boundaries
while(length--) rBuffer.insertByte(rOldBuffers[l].data[destPtr++]);
}
if (length > distance) {
while(length--) rBuffer.insertByte(rBuffer.data[destPtr++]);
} else {
rBuffer.insertBytes(rBuffer.data.subarray(destPtr, destPtr + length));
}
}
var rOldBuffers = []
// v must be a valid RarVolume
function unpack(v) {
// TODO: implement what happens when unpVer is < 15
var Ver = v.header.unpVer <= 15 ? 15 : v.header.unpVer,
Solid = v.header.LHD_SOLID,
bstream = new bitjs.io.BitStream(v.fileData.buffer, true /* rtl */, v.fileData.byteOffset, v.fileData.byteLength );
rBuffer = new bitjs.io.ByteBuffer(v.header.unpackedSize);
info("Unpacking "+v.filename+" RAR v"+Ver);
switch(Ver) {
case 15: // rar 1.5 compression
Unpack15(bstream, Solid);
break;
case 20: // rar 2.x compression
case 26: // files larger than 2GB
Unpack20(bstream, Solid);
break;
case 29: // rar 3.x compression
case 36: // alternative hash
Unpack29(bstream, Solid);
break;
} // switch(method)
rOldBuffers.push(rBuffer);
//TODO: clear these old buffers when there's over 4MB of history
return rBuffer.data;
}
// bstream is a bit stream
var RarLocalFile = function(bstream) {
this.header = new RarVolumeHeader(bstream);
this.filename = this.header.filename;
if (this.header.headType != FILE_HEAD && this.header.headType != ENDARC_HEAD) {
this.isValid = false;
info("Error! RAR Volume did not include a FILE_HEAD header ");
}
else {
// read in the compressed data
this.fileData = null;
if (this.header.packSize > 0) {
this.fileData = bstream.readBytes(this.header.packSize);
this.isValid = true;
}
}
};
RarLocalFile.prototype.unrar = function() {
if (!this.header.flags.LHD_SPLIT_BEFORE) {
// unstore file
if (this.header.method == 0x30) {
info("Unstore "+this.filename);
this.isValid = true;
currentBytesUnarchivedInFile += this.fileData.length;
currentBytesUnarchived += this.fileData.length;
} else {
this.isValid = true;
this.fileData = unpack(this);
}
}
}
var unrar = function(arrayBuffer) {
currentFilename = "";
currentFileNumber = 0;
currentBytesUnarchivedInFile = 0;
currentBytesUnarchived = 0;
totalUncompressedBytesInArchive = 0;
totalFilesInArchive = 0;
postMessage(new bitjs.archive.UnarchiveStartEvent());
var bstream = new bitjs.io.BitStream(arrayBuffer, false /* rtl */);
var header = new RarVolumeHeader(bstream);
if (header.crc == 0x6152 &&
header.headType == 0x72 &&
header.flags.value == 0x1A21 &&
header.headSize == 7) {
info("Found RAR signature");
var mhead = new RarVolumeHeader(bstream);
if (mhead.headType != MAIN_HEAD) {
info("Error! RAR did not include a MAIN_HEAD header");
}
else {
var localFiles = [],
localFile = null;
do {
try {
localFile = new RarLocalFile(bstream);
info("RAR localFile isValid=" + localFile.isValid + ", volume packSize=" + localFile.header.packSize);
if (localFile && localFile.isValid && localFile.header.packSize > 0) {
totalUncompressedBytesInArchive += localFile.header.unpackedSize;
localFiles.push(localFile);
} else if (localFile.header.packSize == 0 && localFile.header.unpackedSize == 0) {
localFile.isValid = true;
}
} catch(err) {
break;
}
//info("bstream" + bstream.bytePtr+"/"+bstream.bytes.length);
} while( localFile.isValid );
totalFilesInArchive = localFiles.length;
// now we have all information but things are unpacked
// TODO: unpack
localFiles = localFiles.sort(function(a,b) {
// extract the number at the end of both filenames
var aname = a.filename;
var bname = b.filename;
return aname > bname ? 1 : -1;
/*
var aindex = aname.length, bindex = bname.length;
// Find the last number character from the back of the filename.
while (aname[aindex-1] < '0' || aname[aindex-1] > '9') --aindex;
while (bname[bindex-1] < '0' || bname[bindex-1] > '9') --bindex;
// Find the first number character from the back of the filename
while (aname[aindex-1] >= '0' && aname[aindex-1] <= '9') --aindex;
while (bname[bindex-1] >= '0' && bname[bindex-1] <= '9') --bindex;
// parse them into numbers and return comparison
var anum = parseInt(aname.substr(aindex), 10),
bnum = parseInt(bname.substr(bindex), 10);
return bnum - anum;*/
});
info(localFiles.map(function(a){return a.filename}).join(', '));
for (var i = 0; i < localFiles.length; ++i) {
var localfile = localFiles[i];
// update progress
currentFilename = localfile.header.filename;
currentBytesUnarchivedInFile = 0;
// actually do the unzipping
localfile.unrar();
if (localfile.isValid) {
postMessage(new bitjs.archive.UnarchiveExtractEvent(localfile));
postProgress();
}
}
postProgress();
}
}
else {
err("Invalid RAR file");
}
postMessage(new bitjs.archive.UnarchiveFinishEvent());
};
// event.data.file has the ArrayBuffer.
onmessage = function(event) {
var ab = event.data.file;
unrar(ab, true);
};

182
lib/bitjs/untar.js Executable file
View file

@ -0,0 +1,182 @@
/**
* untar.js
*
* Copyright(c) 2011 Google Inc.
*
* Reference Documentation:
*
* TAR format: http://www.gnu.org/software/automake/manual/tar/Standard.html
*/
// This file expects to be invoked as a Worker (see onmessage below).
importScripts('io.js');
importScripts('archive.js');
// Progress variables.
var currentFilename = "";
var currentFileNumber = 0;
var currentBytesUnarchivedInFile = 0;
var currentBytesUnarchived = 0;
var totalUncompressedBytesInArchive = 0;
var totalFilesInArchive = 0;
// Helper functions.
var info = function(str) {
postMessage(new bitjs.archive.UnarchiveInfoEvent(str));
};
var err = function(str) {
postMessage(new bitjs.archive.UnarchiveErrorEvent(str));
};
var postProgress = function() {
postMessage(new bitjs.archive.UnarchiveProgressEvent(
currentFilename,
currentFileNumber,
currentBytesUnarchivedInFile,
currentBytesUnarchived,
totalUncompressedBytesInArchive,
totalFilesInArchive));
};
// Removes all characters from the first zero-byte in the string onwards.
var readCleanString = function(bstr, numBytes) {
var str = bstr.readString(numBytes);
var zIndex = str.indexOf(String.fromCharCode(0));
return zIndex != -1 ? str.substr(0, zIndex) : str;
};
// takes a ByteStream and parses out the local file information
var TarLocalFile = function(bstream) {
this.isValid = false;
// Read in the header block
this.name = readCleanString(bstream, 100);
this.mode = readCleanString(bstream, 8);
this.uid = readCleanString(bstream, 8);
this.gid = readCleanString(bstream, 8);
this.size = parseInt(readCleanString(bstream, 12), 8);
this.mtime = readCleanString(bstream, 12);
this.chksum = readCleanString(bstream, 8);
this.typeflag = readCleanString(bstream, 1);
this.linkname = readCleanString(bstream, 100);
this.maybeMagic = readCleanString(bstream, 6);
if (this.maybeMagic == "ustar") {
this.version = readCleanString(bstream, 2);
this.uname = readCleanString(bstream, 32);
this.gname = readCleanString(bstream, 32);
this.devmajor = readCleanString(bstream, 8);
this.devminor = readCleanString(bstream, 8);
this.prefix = readCleanString(bstream, 155);
if (this.prefix.length) {
this.name = this.prefix + this.name;
}
bstream.readBytes(12); // 512 - 500
} else {
bstream.readBytes(255); // 512 - 257
}
// Done header, now rest of blocks are the file contents.
this.filename = this.name;
this.fileData = null;
info("Untarring file '" + this.filename + "'");
info(" size = " + this.size);
info(" typeflag = " + this.typeflag);
// A regular file.
if (this.typeflag == 0) {
info(" This is a regular file.");
var sizeInBytes = parseInt(this.size);
this.fileData = new Uint8Array(bstream.bytes.buffer, bstream.ptr, this.size);
if (this.name.length > 0 && this.size > 0 && this.fileData && this.fileData.buffer) {
this.isValid = true;
}
bstream.readBytes(this.size);
// Round up to 512-byte blocks.
var remaining = 512 - this.size % 512;
if (remaining > 0 && remaining < 512) {
bstream.readBytes(remaining);
}
} else if (this.typeflag == 5) {
info(" This is a directory.")
}
};
// Takes an ArrayBuffer of a tar file in
// returns null on error
// returns an array of DecompressedFile objects on success
var untar = function(arrayBuffer) {
currentFilename = "";
currentFileNumber = 0;
currentBytesUnarchivedInFile = 0;
currentBytesUnarchived = 0;
totalUncompressedBytesInArchive = 0;
totalFilesInArchive = 0;
postMessage(new bitjs.archive.UnarchiveStartEvent());
var bstream = new bitjs.io.ByteStream(arrayBuffer);
var localFiles = [];
// While we don't encounter an empty block, keep making TarLocalFiles.
while (bstream.peekNumber(4) != 0) {
var oneLocalFile = new TarLocalFile(bstream);
if (oneLocalFile && oneLocalFile.isValid) {
localFiles.push(oneLocalFile);
totalUncompressedBytesInArchive += oneLocalFile.size;
}
}
totalFilesInArchive = localFiles.length;
// got all local files, now sort them
localFiles.sort(function(a,b) {
// extract the number at the end of both filenames
var aname = a.filename;
var bname = b.filename;
var aindex = aname.length, bindex = bname.length;
// Find the last number character from the back of the filename.
while (aname[aindex-1] < '0' || aname[aindex-1] > '9') --aindex;
while (bname[bindex-1] < '0' || bname[bindex-1] > '9') --bindex;
// Find the first number character from the back of the filename
while (aname[aindex-1] >= '0' && aname[aindex-1] <= '9') --aindex;
while (bname[bindex-1] >= '0' && bname[bindex-1] <= '9') --bindex;
// parse them into numbers and return comparison
var anum = parseInt(aname.substr(aindex), 10),
bnum = parseInt(bname.substr(bindex), 10);
return anum - bnum;
});
// report # files and total length
if (localFiles.length > 0) {
postProgress();
}
// now do the shipping of each file
for (var i = 0; i < localFiles.length; ++i) {
var localfile = localFiles[i];
info("Sending file '" + localfile.filename + "' up");
// update progress
currentFilename = localfile.filename;
currentFileNumber = i;
currentBytesUnarchivedInFile = localfile.size;
currentBytesUnarchived += localfile.size;
postMessage(new bitjs.archive.UnarchiveExtractEvent(localfile));
postProgress();
}
postProgress();
postMessage(new bitjs.archive.UnarchiveFinishEvent());
};
// event.data.file has the ArrayBuffer.
onmessage = function(event) {
var ab = event.data.file;
untar(ab);
};

631
lib/bitjs/unzip.js Executable file
View file

@ -0,0 +1,631 @@
/**
* unzip.js
*
* Copyright(c) 2011 Google Inc.
* Copyright(c) 2011 antimatter15
*
* Reference Documentation:
*
* ZIP format: http://www.pkware.com/documents/casestudies/APPNOTE.TXT
* DEFLATE format: http://tools.ietf.org/html/rfc1951
*/
// This file expects to be invoked as a Worker (see onmessage below).
importScripts('io.js');
importScripts('archive.js');
// Progress variables.
var currentFilename = "";
var currentFileNumber = 0;
var currentBytesUnarchivedInFile = 0;
var currentBytesUnarchived = 0;
var totalUncompressedBytesInArchive = 0;
var totalFilesInArchive = 0;
// Helper functions.
var info = function(str) {
postMessage(new bitjs.archive.UnarchiveInfoEvent(str));
};
var err = function(str) {
postMessage(new bitjs.archive.UnarchiveErrorEvent(str));
};
var postProgress = function() {
postMessage(new bitjs.archive.UnarchiveProgressEvent(
currentFilename,
currentFileNumber,
currentBytesUnarchivedInFile,
currentBytesUnarchived,
totalUncompressedBytesInArchive,
totalFilesInArchive));
};
var zLocalFileHeaderSignature = 0x04034b50;
var zArchiveExtraDataSignature = 0x08064b50;
var zCentralFileHeaderSignature = 0x02014b50;
var zDigitalSignatureSignature = 0x05054b50;
var zEndOfCentralDirSignature = 0x06064b50;
var zEndOfCentralDirLocatorSignature = 0x07064b50;
// takes a ByteStream and parses out the local file information
var ZipLocalFile = function(bstream) {
if (typeof bstream != typeof {} || !bstream.readNumber || typeof bstream.readNumber != typeof function(){}) {
return null;
}
bstream.readNumber(4); // swallow signature
this.version = bstream.readNumber(2);
this.generalPurpose = bstream.readNumber(2);
this.compressionMethod = bstream.readNumber(2);
this.lastModFileTime = bstream.readNumber(2);
this.lastModFileDate = bstream.readNumber(2);
this.crc32 = bstream.readNumber(4);
this.compressedSize = bstream.readNumber(4);
this.uncompressedSize = bstream.readNumber(4);
this.fileNameLength = bstream.readNumber(2);
this.extraFieldLength = bstream.readNumber(2);
this.filename = null;
if (this.fileNameLength > 0) {
this.filename = bstream.readString(this.fileNameLength);
}
info("Zip Local File Header:");
info(" version=" + this.version);
info(" general purpose=" + this.generalPurpose);
info(" compression method=" + this.compressionMethod);
info(" last mod file time=" + this.lastModFileTime);
info(" last mod file date=" + this.lastModFileDate);
info(" crc32=" + this.crc32);
info(" compressed size=" + this.compressedSize);
info(" uncompressed size=" + this.uncompressedSize);
info(" file name length=" + this.fileNameLength);
info(" extra field length=" + this.extraFieldLength);
info(" filename = '" + this.filename + "'");
this.extraField = null;
if (this.extraFieldLength > 0) {
this.extraField = bstream.readString(this.extraFieldLength);
info(" extra field=" + this.extraField);
}
// read in the compressed data
this.fileData = null;
if (this.compressedSize > 0) {
this.fileData = new Uint8Array(bstream.bytes.buffer, bstream.ptr, this.compressedSize);
bstream.ptr += this.compressedSize;
}
// TODO: deal with data descriptor if present (we currently assume no data descriptor!)
// "This descriptor exists only if bit 3 of the general purpose bit flag is set"
// But how do you figure out how big the file data is if you don't know the compressedSize
// from the header?!?
if ((this.generalPurpose & bitjs.BIT[3]) != 0) {
this.crc32 = bstream.readNumber(4);
this.compressedSize = bstream.readNumber(4);
this.uncompressedSize = bstream.readNumber(4);
}
};
// determine what kind of compressed data we have and decompress
ZipLocalFile.prototype.unzip = function() {
// Zip Version 1.0, no compression (store only)
if (this.compressionMethod == 0 ) {
info("ZIP v"+this.version+", store only: " + this.filename + " (" + this.compressedSize + " bytes)");
currentBytesUnarchivedInFile = this.compressedSize;
currentBytesUnarchived += this.compressedSize;
}
// version == 20, compression method == 8 (DEFLATE)
else if (this.compressionMethod == 8) {
info("ZIP v2.0, DEFLATE: " + this.filename + " (" + this.compressedSize + " bytes)");
this.fileData = inflate(this.fileData, this.uncompressedSize);
}
else {
err("UNSUPPORTED VERSION/FORMAT: ZIP v" + this.version + ", compression method=" + this.compressionMethod + ": " + this.filename + " (" + this.compressedSize + " bytes)");
this.fileData = null;
}
};
// Takes an ArrayBuffer of a zip file in
// returns null on error
// returns an array of DecompressedFile objects on success
var unzip = function(arrayBuffer) {
postMessage(new bitjs.archive.UnarchiveStartEvent());
currentFilename = "";
currentFileNumber = 0;
currentBytesUnarchivedInFile = 0;
currentBytesUnarchived = 0;
totalUncompressedBytesInArchive = 0;
totalFilesInArchive = 0;
currentBytesUnarchived = 0;
var bstream = new bitjs.io.ByteStream(arrayBuffer);
// detect local file header signature or return null
if (bstream.peekNumber(4) == zLocalFileHeaderSignature) {
var localFiles = [];
// loop until we don't see any more local files
while (bstream.peekNumber(4) == zLocalFileHeaderSignature) {
var oneLocalFile = new ZipLocalFile(bstream);
// this should strip out directories/folders
if (oneLocalFile && oneLocalFile.uncompressedSize > 0 && oneLocalFile.fileData) {
localFiles.push(oneLocalFile);
totalUncompressedBytesInArchive += oneLocalFile.uncompressedSize;
}
}
totalFilesInArchive = localFiles.length;
// got all local files, now sort them
localFiles.sort(function(a,b) {
// extract the number at the end of both filenames
var aname = a.filename;
var bname = b.filename;
var aindex = aname.length, bindex = bname.length;
// Find the last number character from the back of the filename.
while (aname[aindex-1] < '0' || aname[aindex-1] > '9') --aindex;
while (bname[bindex-1] < '0' || bname[bindex-1] > '9') --bindex;
// Find the first number character from the back of the filename
while (aname[aindex-1] >= '0' && aname[aindex-1] <= '9') --aindex;
while (bname[bindex-1] >= '0' && bname[bindex-1] <= '9') --bindex;
// parse them into numbers and return comparison
var anum = parseInt(aname.substr(aindex), 10),
bnum = parseInt(bname.substr(bindex), 10);
return anum - bnum;
});
// archive extra data record
if (bstream.peekNumber(4) == zArchiveExtraDataSignature) {
info(" Found an Archive Extra Data Signature");
// skipping this record for now
bstream.readNumber(4);
var archiveExtraFieldLength = bstream.readNumber(4);
bstream.readString(archiveExtraFieldLength);
}
// central directory structure
// TODO: handle the rest of the structures (Zip64 stuff)
if (bstream.peekNumber(4) == zCentralFileHeaderSignature) {
info(" Found a Central File Header");
// read all file headers
while (bstream.peekNumber(4) == zCentralFileHeaderSignature) {
bstream.readNumber(4); // signature
bstream.readNumber(2); // version made by
bstream.readNumber(2); // version needed to extract
bstream.readNumber(2); // general purpose bit flag
bstream.readNumber(2); // compression method
bstream.readNumber(2); // last mod file time
bstream.readNumber(2); // last mod file date
bstream.readNumber(4); // crc32
bstream.readNumber(4); // compressed size
bstream.readNumber(4); // uncompressed size
var fileNameLength = bstream.readNumber(2); // file name length
var extraFieldLength = bstream.readNumber(2); // extra field length
var fileCommentLength = bstream.readNumber(2); // file comment length
bstream.readNumber(2); // disk number start
bstream.readNumber(2); // internal file attributes
bstream.readNumber(4); // external file attributes
bstream.readNumber(4); // relative offset of local header
bstream.readString(fileNameLength); // file name
bstream.readString(extraFieldLength); // extra field
bstream.readString(fileCommentLength); // file comment
}
}
// digital signature
if (bstream.peekNumber(4) == zDigitalSignatureSignature) {
info(" Found a Digital Signature");
bstream.readNumber(4);
var sizeOfSignature = bstream.readNumber(2);
bstream.readString(sizeOfSignature); // digital signature data
}
// report # files and total length
if (localFiles.length > 0) {
postProgress();
}
// now do the unzipping of each file
for (var i = 0; i < localFiles.length; ++i) {
var localfile = localFiles[i];
// update progress
currentFilename = localfile.filename;
currentFileNumber = i;
currentBytesUnarchivedInFile = 0;
// actually do the unzipping
localfile.unzip();
if (localfile.fileData != null) {
postMessage(new bitjs.archive.UnarchiveExtractEvent(localfile));
postProgress();
}
}
postProgress();
postMessage(new bitjs.archive.UnarchiveFinishEvent());
}
}
// returns a table of Huffman codes
// each entry's index is its code and its value is a JavaScript object
// containing {length: 6, symbol: X}
function getHuffmanCodes(bitLengths) {
// ensure bitLengths is an array containing at least one element
if (typeof bitLengths != typeof [] || bitLengths.length < 1) {
err("Error! getHuffmanCodes() called with an invalid array");
return null;
}
// Reference: http://tools.ietf.org/html/rfc1951#page-8
var numLengths = bitLengths.length,
bl_count = [],
MAX_BITS = 1;
// Step 1: count up how many codes of each length we have
for (var i = 0; i < numLengths; ++i) {
var length = bitLengths[i];
// test to ensure each bit length is a positive, non-zero number
if (typeof length != typeof 1 || length < 0) {
err("bitLengths contained an invalid number in getHuffmanCodes(): " + length + " of type " + (typeof length));
return null;
}
// increment the appropriate bitlength count
if (bl_count[length] == undefined) bl_count[length] = 0;
// a length of zero means this symbol is not participating in the huffman coding
if (length > 0) bl_count[length]++;
if (length > MAX_BITS) MAX_BITS = length;
}
// Step 2: Find the numerical value of the smallest code for each code length
var next_code = [],
code = 0;
for (var bits = 1; bits <= MAX_BITS; ++bits) {
var length = bits-1;
// ensure undefined lengths are zero
if (bl_count[length] == undefined) bl_count[length] = 0;
code = (code + bl_count[bits-1]) << 1;
next_code[bits] = code;
}
// Step 3: Assign numerical values to all codes
var table = {}, tableLength = 0;
for (var n = 0; n < numLengths; ++n) {
var len = bitLengths[n];
if (len != 0) {
table[next_code[len]] = { length: len, symbol: n }; //, bitstring: binaryValueToString(next_code[len],len) };
tableLength++;
next_code[len]++;
}
}
table.maxLength = tableLength;
return table;
}
/*
The Huffman codes for the two alphabets are fixed, and are not
represented explicitly in the data. The Huffman code lengths
for the literal/length alphabet are:
Lit Value Bits Codes
--------- ---- -----
0 - 143 8 00110000 through
10111111
144 - 255 9 110010000 through
111111111
256 - 279 7 0000000 through
0010111
280 - 287 8 11000000 through
11000111
*/
// fixed Huffman codes go from 7-9 bits, so we need an array whose index can hold up to 9 bits
var fixedHCtoLiteral = null;
var fixedHCtoDistance = null;
function getFixedLiteralTable() {
// create once
if (!fixedHCtoLiteral) {
var bitlengths = new Array(288);
for (var i = 0; i <= 143; ++i) bitlengths[i] = 8;
for (i = 144; i <= 255; ++i) bitlengths[i] = 9;
for (i = 256; i <= 279; ++i) bitlengths[i] = 7;
for (i = 280; i <= 287; ++i) bitlengths[i] = 8;
// get huffman code table
fixedHCtoLiteral = getHuffmanCodes(bitlengths);
}
return fixedHCtoLiteral;
}
function getFixedDistanceTable() {
// create once
if (!fixedHCtoDistance) {
var bitlengths = new Array(32);
for (var i = 0; i < 32; ++i) { bitlengths[i] = 5; }
// get huffman code table
fixedHCtoDistance = getHuffmanCodes(bitlengths);
}
return fixedHCtoDistance;
}
// extract one bit at a time until we find a matching Huffman Code
// then return that symbol
function decodeSymbol(bstream, hcTable) {
var code = 0, len = 0;
var match = false;
// loop until we match
for (;;) {
// read in next bit
var bit = bstream.readBits(1);
code = (code<<1) | bit;
++len;
// check against Huffman Code table and break if found
if (hcTable.hasOwnProperty(code) && hcTable[code].length == len) {
break;
}
if (len > hcTable.maxLength) {
err("Bit stream out of sync, didn't find a Huffman Code, length was " + len +
" and table only max code length of " + hcTable.maxLength);
break;
}
}
return hcTable[code].symbol;
}
var CodeLengthCodeOrder = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
/*
Extra Extra Extra
Code Bits Length(s) Code Bits Lengths Code Bits Length(s)
---- ---- ------ ---- ---- ------- ---- ---- -------
257 0 3 267 1 15,16 277 4 67-82
258 0 4 268 1 17,18 278 4 83-98
259 0 5 269 2 19-22 279 4 99-114
260 0 6 270 2 23-26 280 4 115-130
261 0 7 271 2 27-30 281 5 131-162
262 0 8 272 2 31-34 282 5 163-194
263 0 9 273 3 35-42 283 5 195-226
264 0 10 274 3 43-50 284 5 227-257
265 1 11,12 275 3 51-58 285 0 258
266 1 13,14 276 3 59-66
*/
var LengthLookupTable = [
[0,3], [0,4], [0,5], [0,6],
[0,7], [0,8], [0,9], [0,10],
[1,11], [1,13], [1,15], [1,17],
[2,19], [2,23], [2,27], [2,31],
[3,35], [3,43], [3,51], [3,59],
[4,67], [4,83], [4,99], [4,115],
[5,131], [5,163], [5,195], [5,227],
[0,258]
];
/*
Extra Extra Extra
Code Bits Dist Code Bits Dist Code Bits Distance
---- ---- ---- ---- ---- ------ ---- ---- --------
0 0 1 10 4 33-48 20 9 1025-1536
1 0 2 11 4 49-64 21 9 1537-2048
2 0 3 12 5 65-96 22 10 2049-3072
3 0 4 13 5 97-128 23 10 3073-4096
4 1 5,6 14 6 129-192 24 11 4097-6144
5 1 7,8 15 6 193-256 25 11 6145-8192
6 2 9-12 16 7 257-384 26 12 8193-12288
7 2 13-16 17 7 385-512 27 12 12289-16384
8 3 17-24 18 8 513-768 28 13 16385-24576
9 3 25-32 19 8 769-1024 29 13 24577-32768
*/
var DistLookupTable = [
[0,1], [0,2], [0,3], [0,4],
[1,5], [1,7],
[2,9], [2,13],
[3,17], [3,25],
[4,33], [4,49],
[5,65], [5,97],
[6,129], [6,193],
[7,257], [7,385],
[8,513], [8,769],
[9,1025], [9,1537],
[10,2049], [10,3073],
[11,4097], [11,6145],
[12,8193], [12,12289],
[13,16385], [13,24577]
];
function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) {
/*
loop (until end of block code recognized)
decode literal/length value from input stream
if value < 256
copy value (literal byte) to output stream
otherwise
if value = end of block (256)
break from loop
otherwise (value = 257..285)
decode distance from input stream
move backwards distance bytes in the output
stream, and copy length bytes from this
position to the output stream.
*/
var numSymbols = 0, blockSize = 0;
for (;;) {
var symbol = decodeSymbol(bstream, hcLiteralTable);
++numSymbols;
if (symbol < 256) {
// copy literal byte to output
buffer.insertByte(symbol);
blockSize++;
}
else {
// end of block reached
if (symbol == 256) {
break;
}
else {
var lengthLookup = LengthLookupTable[symbol-257],
length = lengthLookup[1] + bstream.readBits(lengthLookup[0]),
distLookup = DistLookupTable[decodeSymbol(bstream, hcDistanceTable)],
distance = distLookup[1] + bstream.readBits(distLookup[0]);
// now apply length and distance appropriately and copy to output
// TODO: check that backward distance < data.length?
// http://tools.ietf.org/html/rfc1951#page-11
// "Note also that the referenced string may overlap the current
// position; for example, if the last 2 bytes decoded have values
// X and Y, a string reference with <length = 5, distance = 2>
// adds X,Y,X,Y,X to the output stream."
//
// loop for each character
var ch = buffer.ptr - distance;
blockSize += length;
if(length > distance) {
var data = buffer.data;
while (length--) {
buffer.insertByte(data[ch++]);
}
} else {
buffer.insertBytes(buffer.data.subarray(ch, ch + length))
}
} // length-distance pair
} // length-distance pair or end-of-block
} // loop until we reach end of block
return blockSize;
}
// {Uint8Array} compressedData A Uint8Array of the compressed file data.
// compression method 8
// deflate: http://tools.ietf.org/html/rfc1951
function inflate(compressedData, numDecompressedBytes) {
// Bit stream representing the compressed data.
var bstream = new bitjs.io.BitStream(compressedData.buffer,
false /* rtl */,
compressedData.byteOffset,
compressedData.byteLength);
var buffer = new bitjs.io.ByteBuffer(numDecompressedBytes);
var numBlocks = 0, blockSize = 0;
// block format: http://tools.ietf.org/html/rfc1951#page-9
do {
var bFinal = bstream.readBits(1),
bType = bstream.readBits(2);
blockSize = 0;
++numBlocks;
// no compression
if (bType == 0) {
// skip remaining bits in this byte
while (bstream.bitPtr != 0) bstream.readBits(1);
var len = bstream.readBits(16),
nlen = bstream.readBits(16);
// TODO: check if nlen is the ones-complement of len?
if(len > 0) buffer.insertBytes(bstream.readBytes(len));
blockSize = len;
}
// fixed Huffman codes
else if(bType == 1) {
blockSize = inflateBlockData(bstream, getFixedLiteralTable(), getFixedDistanceTable(), buffer);
}
// dynamic Huffman codes
else if(bType == 2) {
var numLiteralLengthCodes = bstream.readBits(5) + 257;
var numDistanceCodes = bstream.readBits(5) + 1,
numCodeLengthCodes = bstream.readBits(4) + 4;
// populate the array of code length codes (first de-compaction)
var codeLengthsCodeLengths = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
for (var i = 0; i < numCodeLengthCodes; ++i) {
codeLengthsCodeLengths[ CodeLengthCodeOrder[i] ] = bstream.readBits(3);
}
// get the Huffman Codes for the code lengths
var codeLengthsCodes = getHuffmanCodes(codeLengthsCodeLengths);
// now follow this mapping
/*
0 - 15: Represent code lengths of 0 - 15
16: Copy the previous code length 3 - 6 times.
The next 2 bits indicate repeat length
(0 = 3, ... , 3 = 6)
Example: Codes 8, 16 (+2 bits 11),
16 (+2 bits 10) will expand to
12 code lengths of 8 (1 + 6 + 5)
17: Repeat a code length of 0 for 3 - 10 times.
(3 bits of length)
18: Repeat a code length of 0 for 11 - 138 times
(7 bits of length)
*/
// to generate the true code lengths of the Huffman Codes for the literal
// and distance tables together
var literalCodeLengths = [];
var prevCodeLength = 0;
while (literalCodeLengths.length < numLiteralLengthCodes + numDistanceCodes) {
var symbol = decodeSymbol(bstream, codeLengthsCodes);
if (symbol <= 15) {
literalCodeLengths.push(symbol);
prevCodeLength = symbol;
}
else if (symbol == 16) {
var repeat = bstream.readBits(2) + 3;
while (repeat--) {
literalCodeLengths.push(prevCodeLength);
}
}
else if (symbol == 17) {
var repeat = bstream.readBits(3) + 3;
while (repeat--) {
literalCodeLengths.push(0);
}
}
else if (symbol == 18) {
var repeat = bstream.readBits(7) + 11;
while (repeat--) {
literalCodeLengths.push(0);
}
}
}
// now split the distance code lengths out of the literal code array
var distanceCodeLengths = literalCodeLengths.splice(numLiteralLengthCodes, numDistanceCodes);
// now generate the true Huffman Code tables using these code lengths
var hcLiteralTable = getHuffmanCodes(literalCodeLengths),
hcDistanceTable = getHuffmanCodes(distanceCodeLengths);
blockSize = inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer);
}
// error
else {
err("Error! Encountered deflate block of type 3");
return null;
}
// update progress
currentBytesUnarchivedInFile += blockSize;
currentBytesUnarchived += blockSize;
postProgress();
} while (bFinal != 1);
// we are done reading blocks if the bFinal bit was set for this block
// return the buffer data bytes
return buffer.data;
}
// event.data.file has the ArrayBuffer.
onmessage = function(event) {
unzip(event.data.file, true);
};

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,760 +0,0 @@
// Code can be found at: http://www.calormen.com/polyfill/typedarray.js
/*
$LicenseInfo:firstyear=2010&license=mit$
Copyright (c) 2010, Linden Research, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
$/LicenseInfo$
*/
// Original can be found at: https://bitbucket.org/lindenlab/llsd
// Modifications by Joshua Bell inexorabletash@hotmail.com
// * Restructure the creation of types and exporting to global namespace
// * Allow no arguments to DataView constructor
// * Work cross-frame with native arrays/shimmed DataView
// * Corrected Object.defineProperty shim for IE8
// ES3/ES5 implementation of the Krhonos TypedArray Working Draft (work in progress):
// Ref: https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/TypedArray-spec.html
// Date: 2011-02-01
//
// Variations:
// * Float/Double -> Float32/Float64, per WebGL-Public mailing list conversations (post 5/17)
// * Allows typed_array.get/set() as alias for subscripts (typed_array[])
(function(global) {
"use strict";
var USE_NATIVE_IF_AVAILABLE = true;
// Approximations of internal ECMAScript conversion functions
var ECMAScript = (function() {
// Stash a copy in case other scripts modify these
var opts = Object.prototype.toString, ophop = Object.prototype.hasOwnProperty;
return {
// Class returns internal [[Class]] property, used to avoid cross-frame instanceof issues:
Class : function(v) {
return opts.call(v).replace(/^\[object *|\]$/g, '');
},
HasProperty : function(o, p) {
return p in o;
},
HasOwnProperty : function(o, p) {
return ophop.call(o, p);
},
IsCallable : function(o) {
return typeof o === 'function';
},
ToInt32 : function(v) {
return v >> 0;
},
ToUint32 : function(v) {
return v >>> 0;
}
};
}());
// Create an INDEX_SIZE_ERR event - intentionally induces a DOM error if possible
function new_INDEX_SIZE_ERR() {
try {
if (document) {
// raises DOMException(INDEX_SIZE_ERR)
document.createTextNode("").splitText(1);
}
return new RangeError("INDEX_SIZE_ERR");
} catch (e) {
return e;
}
}
// ES5: lock down object properties
function configureProperties(obj) {
if (Object.getOwnPropertyNames && Object.defineProperty) {
var props = Object.getOwnPropertyNames(obj), i;
for (i = 0; i < props.length; i += 1) {
Object.defineProperty(obj, props[i], {
value : obj[props[i]],
writable : false,
enumerable : false,
configurable : false
});
}
}
}
// emulate ES5 getter/setter API using legacy APIs
// http://blogs.msdn.com/b/ie/archive/2010/09/07/transitioning-existing-code-to-the-es5-getter-setter-apis.aspx
// (second clause tests for Object.defineProperty() in IE<9 that only supports extending DOM prototypes, but
// note that IE<9 does not support __defineGetter__ or __defineSetter__ so it just renders the method harmless)
if (!Object.defineProperty || !(function() {
try {
Object.defineProperty({}, 'x', {});
return true;
} catch (e) {
return false;
}
}())) {
Object.defineProperty = function(o, p, desc) {
if (!o === Object(o)) {
throw new TypeError("Object.defineProperty called on non-object");
}
if (ECMAScript.HasProperty(desc, 'get') && Object.prototype.__defineGetter__) {
Object.prototype.__defineGetter__.call(o, p, desc.get);
}
if (ECMAScript.HasProperty(desc, 'set') && Object.prototype.__defineSetter__) {
Object.prototype.__defineSetter__.call(o, p, desc.set);
}
if (ECMAScript.HasProperty(desc, 'value')) {
o[p] = desc.value;
}
return o;
};
}
if (!Object.getOwnPropertyNames) {
Object.getOwnPropertyNames = function getOwnPropertyNames(o) {
if (o !== Object(o)) {
throw new TypeError("Object.getOwnPropertyNames called on non-object");
}
var props = [], p;
for (p in o) {
if (ECMAScript.HasOwnProperty(o, p)) {
props.push(p);
}
}
return props;
};
}
// ES5: Make obj[index] an alias for obj._getter(index)/obj._setter(index, value)
// for index in 0 ... obj.length
function makeArrayAccessors(obj) {
if (!Object.defineProperty) {
return;
}
function makeArrayAccessor(index) {
Object.defineProperty(obj, index, {
'get' : function() {
return obj._getter(index);
},
'set' : function(v) {
obj._setter(index, v);
},
enumerable : true,
configurable : false
});
}
var i;
for (i = 0; i < obj.length; i += 1) {
makeArrayAccessor(i);
}
}
// Internal conversion functions:
// pack<Type>() - take a number (interpreted as Type), output a byte array
// unpack<Type>() - take a byte array, output a Type-like number
function as_signed(value, bits) {
var s = 32 - bits;
return (value << s) >> s;
}
function as_unsigned(value, bits) {
var s = 32 - bits;
return (value << s) >>> s;
}
function packInt8(n) {
return [ n & 0xff ];
}
function unpackInt8(bytes) {
return as_signed(bytes[0], 8);
}
function packUint8(n) {
return [ n & 0xff ];
}
function unpackUint8(bytes) {
return as_unsigned(bytes[0], 8);
}
function packInt16(n) {
return [ (n >> 8) & 0xff, n & 0xff ];
}
function unpackInt16(bytes) {
return as_signed(bytes[0] << 8 | bytes[1], 16);
}
function packUint16(n) {
return [ (n >> 8) & 0xff, n & 0xff ];
}
function unpackUint16(bytes) {
return as_unsigned(bytes[0] << 8 | bytes[1], 16);
}
function packInt32(n) {
return [ (n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff ];
}
function unpackInt32(bytes) {
return as_signed(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], 32);
}
function packUint32(n) {
return [ (n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff ];
}
function unpackUint32(bytes) {
return as_unsigned(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], 32);
}
function packIEEE754(v, ebits, fbits) {
var bias = (1 << (ebits - 1)) - 1, s, e, f, ln, i, bits, str, bytes;
// Compute sign, exponent, fraction
if (v !== v) {
// NaN
// http://dev.w3.org/2006/webapi/WebIDL/#es-type-mapping
e = (1 << bias) - 1;
f = Math.pow(2, fbits - 1);
s = 0;
} else if (v === Infinity || v === -Infinity) {
e = (1 << bias) - 1;
f = 0;
s = (v < 0) ? 1 : 0;
} else if (v === 0) {
e = 0;
f = 0;
s = (1 / v === -Infinity) ? 1 : 0;
} else {
s = v < 0;
v = Math.abs(v);
if (v >= Math.pow(2, 1 - bias)) {
// Normalized
ln = Math.min(Math.floor(Math.log(v) / Math.LN2), bias);
e = ln + bias;
f = Math.round(v * Math.pow(2, fbits - ln) - Math.pow(2, fbits));
} else {
// Denormalized
e = 0;
f = Math.round(v / Math.pow(2, 1 - bias - fbits));
}
}
// Pack sign, exponent, fraction
bits = [];
for (i = fbits; i; i -= 1) {
bits.push(f % 2 ? 1 : 0);
f = Math.floor(f / 2);
}
for (i = ebits; i; i -= 1) {
bits.push(e % 2 ? 1 : 0);
e = Math.floor(e / 2);
}
bits.push(s ? 1 : 0);
bits.reverse();
str = bits.join('');
// Bits to bytes
bytes = [];
while (str.length) {
bytes.push(parseInt(str.substring(0, 8), 2));
str = str.substring(8);
}
return bytes;
}
function unpackIEEE754(bytes, ebits, fbits) {
// Bytes to bits
var bits = [], i, j, b, str, bias, s, e, f;
for (i = bytes.length; i; i -= 1) {
b = bytes[i - 1];
for (j = 8; j; j -= 1) {
bits.push(b % 2 ? 1 : 0);
b = b >> 1;
}
}
bits.reverse();
str = bits.join('');
// Unpack sign, exponent, fraction
bias = (1 << (ebits - 1)) - 1;
s = parseInt(str.substring(0, 1), 2) ? -1 : 1;
e = parseInt(str.substring(1, 1 + ebits), 2);
f = parseInt(str.substring(1 + ebits), 2);
// Produce number
if (e === (1 << ebits) - 1) {
return f !== 0 ? NaN : s * Infinity;
} else if (e > 0) {
// Normalized
return s * Math.pow(2, e - bias) * (1 + f / Math.pow(2, fbits));
} else if (f !== 0) {
// Denormalized
return s * Math.pow(2, -(bias - 1)) * (f / Math.pow(2, fbits));
} else {
return s < 0 ? -0 : 0;
}
}
function unpackFloat64(b) {
return unpackIEEE754(b, 11, 52);
}
function packFloat64(v) {
return packIEEE754(v, 11, 52);
}
function unpackFloat32(b) {
return unpackIEEE754(b, 8, 23);
}
function packFloat32(v) {
return packIEEE754(v, 8, 23);
}
//
// 3 The ArrayBuffer Type
//
(function() {
/** @constructor */
var ArrayBuffer = function ArrayBuffer(length) {
length = ECMAScript.ToInt32(length);
if (length < 0) {
throw new RangeError('ArrayBuffer size is not a small enough positive integer.');
}
this.byteLength = length;
this._bytes = [];
this._bytes.length = length;
var i;
for (i = 0; i < this.byteLength; i += 1) {
this._bytes[i] = 0;
}
configureProperties(this);
};
//
// 4 The ArrayBufferView Type
//
// NOTE: this constructor is not exported
/** @constructor */
var ArrayBufferView = function ArrayBufferView() {
// this.buffer = null;
// this.byteOffset = 0;
// this.byteLength = 0;
};
//
// 5 The Typed Array View Types
//
function makeTypedArrayConstructor(bytesPerElement, pack, unpack) {
// Each TypedArray type requires a distinct constructor instance with
// identical logic, which this produces.
var ctor;
ctor = function(buffer, byteOffset, length) {
var array, sequence, i, s;
if (!arguments.length || typeof arguments[0] === 'number') {
// Constructor(unsigned long length)
this.length = ECMAScript.ToInt32(arguments[0]);
if (length < 0) {
throw new RangeError('ArrayBufferView size is not a small enough positive integer.');
}
this.byteLength = this.length * this.BYTES_PER_ELEMENT;
this.buffer = new ArrayBuffer(this.byteLength);
this.byteOffset = 0;
} else if (typeof arguments[0] === 'object' && arguments[0].constructor === ctor) {
// Constructor(TypedArray array)
array = arguments[0];
this.length = array.length;
this.byteLength = this.length * this.BYTES_PER_ELEMENT;
this.buffer = new ArrayBuffer(this.byteLength);
this.byteOffset = 0;
for (i = 0; i < this.length; i += 1) {
this._setter(i, array._getter(i));
}
} else if (typeof arguments[0] === 'object' && !(arguments[0] instanceof ArrayBuffer || ECMAScript.Class(arguments[0]) === 'ArrayBuffer')) {
// Constructor(sequence<type> array)
sequence = arguments[0];
this.length = ECMAScript.ToUint32(sequence.length);
this.byteLength = this.length * this.BYTES_PER_ELEMENT;
this.buffer = new ArrayBuffer(this.byteLength);
this.byteOffset = 0;
for (i = 0; i < this.length; i += 1) {
s = sequence[i];
this._setter(i, Number(s));
}
} else if (typeof arguments[0] === 'object' && (arguments[0] instanceof ArrayBuffer || ECMAScript.Class(arguments[0]) === 'ArrayBuffer')) {
// Constructor(ArrayBuffer buffer,
// optional unsigned long byteOffset, optional unsigned long length)
this.buffer = buffer;
this.byteOffset = ECMAScript.ToUint32(byteOffset);
if (this.byteOffset > this.buffer.byteLength) {
throw new_INDEX_SIZE_ERR(); // byteOffset out of range
}
if (this.byteOffset % this.BYTES_PER_ELEMENT) {
// The given byteOffset must be a multiple of the element
// size of the specific type, otherwise an exception is raised.
// throw new_INDEX_SIZE_ERR();
throw new RangeError("ArrayBuffer length minus the byteOffset is not a multiple of the element size.");
}
if (arguments.length < 3) {
this.byteLength = this.buffer.byteLength - this.byteOffset;
if (this.byteLength % this.BYTES_PER_ELEMENT) {
throw new_INDEX_SIZE_ERR(); // length of buffer minus byteOffset not a multiple of the element size
}
this.length = this.byteLength / this.BYTES_PER_ELEMENT;
} else {
this.length = ECMAScript.ToUint32(length);
this.byteLength = this.length * this.BYTES_PER_ELEMENT;
}
if ((this.byteOffset + this.byteLength) > this.buffer.byteLength) {
throw new_INDEX_SIZE_ERR(); // byteOffset and length reference an area beyond the end of the buffer
}
} else {
throw new TypeError("Unexpected argument type(s)");
}
this.constructor = ctor;
configureProperties(this);
makeArrayAccessors(this);
};
ctor.prototype = new ArrayBufferView();
ctor.prototype.BYTES_PER_ELEMENT = bytesPerElement;
ctor.prototype._pack = pack;
ctor.prototype._unpack = unpack;
ctor.BYTES_PER_ELEMENT = bytesPerElement;
// getter type (unsigned long index);
ctor.prototype._getter = function(index) {
if (arguments.length < 1) {
throw new SyntaxError("Not enough arguments");
}
index = ECMAScript.ToUint32(index);
if (index >= this.length) {
// throw new_INDEX_SIZE_ERR(); // Array index out of range
return (void 0); // undefined
}
var bytes = [], i, o;
for (i = 0, o = this.byteOffset + index * this.BYTES_PER_ELEMENT; i < this.BYTES_PER_ELEMENT; i += 1, o += 1) {
bytes.push(this.buffer._bytes[o]);
}
return this._unpack(bytes);
};
// NONSTANDARD: convenience alias for getter: type get(unsigned long index);
ctor.prototype.get = ctor.prototype._getter;
// setter void (unsigned long index, type value);
ctor.prototype._setter = function(index, value) {
if (arguments.length < 2) {
throw new SyntaxError("Not enough arguments");
}
index = ECMAScript.ToUint32(index);
if (index >= this.length) {
// throw new_INDEX_SIZE_ERR(); // Array index out of range
return;
}
var bytes = this._pack(value), i, o;
for (i = 0, o = this.byteOffset + index * this.BYTES_PER_ELEMENT; i < this.BYTES_PER_ELEMENT; i += 1, o += 1) {
this.buffer._bytes[o] = bytes[i];
}
};
// void set(TypedArray array, optional unsigned long offset);
// void set(sequence<type> array, optional unsigned long offset);
ctor.prototype.set = function(index, value) {
if (arguments.length < 1) {
throw new SyntaxError("Not enough arguments");
}
var array, sequence, offset, len, i, s, d, byteOffset, byteLength, tmp;
if (typeof arguments[0] === 'object' && arguments[0].constructor === this.constructor) {
// void set(TypedArray array, optional unsigned long offset);
array = arguments[0];
offset = ECMAScript.ToUint32(arguments[1]);
if (offset + array.length > this.length) {
throw new_INDEX_SIZE_ERR(); // Offset plus length of array is out of range
}
byteOffset = this.byteOffset + offset * this.BYTES_PER_ELEMENT;
byteLength = array.length * this.BYTES_PER_ELEMENT;
if (array.buffer === this.buffer) {
tmp = [];
for (i = 0, s = array.byteOffset; i < byteLength; i += 1, s += 1) {
tmp[i] = array.buffer._bytes[s];
}
for (i = 0, d = byteOffset; i < byteLength; i += 1, d += 1) {
this.buffer._bytes[d] = tmp[i];
}
} else {
for (i = 0, s = array.byteOffset, d = byteOffset; i < byteLength; i += 1, s += 1, d += 1) {
this.buffer._bytes[d] = array.buffer._bytes[s];
}
}
} else if (typeof arguments[0] === 'object' && typeof arguments[0].length !== 'undefined') {
// void set(sequence<type> array, optional unsigned long offset);
sequence = arguments[0];
len = ECMAScript.ToUint32(sequence.length);
offset = ECMAScript.ToUint32(arguments[1]);
if (offset + len > this.length) {
throw new_INDEX_SIZE_ERR(); // Offset plus length of array is out of range
}
for (i = 0; i < len; i += 1) {
s = sequence[i];
this._setter(offset + i, Number(s));
}
} else {
throw new TypeError("Unexpected argument type(s)");
}
};
// TypedArray subarray(long begin, optional long end);
ctor.prototype.subarray = function(start, end) {
function clamp(v, min, max) {
return v < min ? min : v > max ? max : v;
}
start = ECMAScript.ToInt32(start);
end = ECMAScript.ToInt32(end);
if (arguments.length < 1) {
start = 0;
}
if (arguments.length < 2) {
end = this.length;
}
if (start < 0) {
start = this.length + start;
}
if (end < 0) {
end = this.length + end;
}
start = clamp(start, 0, this.length);
end = clamp(end, 0, this.length);
var len = end - start;
if (len < 0) {
len = 0;
}
return new this.constructor(this.buffer, start * this.BYTES_PER_ELEMENT, len);
};
return ctor;
}
var Int8Array = makeTypedArrayConstructor(1, packInt8, unpackInt8);
var Uint8Array = makeTypedArrayConstructor(1, packUint8, unpackUint8);
var Int16Array = makeTypedArrayConstructor(2, packInt16, unpackInt16);
var Uint16Array = makeTypedArrayConstructor(2, packUint16, unpackUint16);
var Int32Array = makeTypedArrayConstructor(4, packInt32, unpackInt32);
var Uint32Array = makeTypedArrayConstructor(4, packUint32, unpackUint32);
var Float32Array = makeTypedArrayConstructor(4, packFloat32, unpackFloat32);
var Float64Array = makeTypedArrayConstructor(8, packFloat64, unpackFloat64);
if (USE_NATIVE_IF_AVAILABLE) {
global.ArrayBuffer = global.ArrayBuffer || ArrayBuffer;
global.Int8Array = global.Int8Array || Int8Array;
global.Uint8Array = global.Uint8Array || Uint8Array;
global.Int16Array = global.Int16Array || Int16Array;
global.Uint16Array = global.Uint16Array || Uint16Array;
global.Int32Array = global.Int32Array || Int32Array;
global.Uint32Array = global.Uint32Array || Uint32Array;
global.Float32Array = global.Float32Array || Float32Array;
global.Float64Array = global.Float64Array || Float64Array;
} else {
global.ArrayBuffer = ArrayBuffer;
global.Int8Array = Int8Array;
global.Uint8Array = Uint8Array;
global.Int16Array = Int16Array;
global.Uint16Array = Uint16Array;
global.Int32Array = Int32Array;
global.Uint32Array = Uint32Array;
global.Float32Array = Float32Array;
global.Float64Array = Float64Array;
}
}());
//
// 6 The DataView View Type
//
(function() {
function r(array, index) {
return ECMAScript.IsCallable(array.get) ? array.get(index) : array[index];
}
var IS_BIG_ENDIAN = (function() {
var u16array = new Uint16Array([ 0x1234 ]), u8array = new Uint8Array(u16array.buffer);
return r(u8array, 0) === 0x12;
}());
// Constructor(ArrayBuffer buffer,
// optional unsigned long byteOffset,
// optional unsigned long byteLength)
/** @constructor */
var DataView = function DataView(buffer, byteOffset, byteLength) {
if (arguments.length === 0) {
buffer = new ArrayBuffer(0);
} else if (!(buffer instanceof ArrayBuffer || ECMAScript.Class(buffer) === 'ArrayBuffer')) {
throw new TypeError("TypeError");
}
this.buffer = buffer || new ArrayBuffer(0);
this.byteOffset = ECMAScript.ToUint32(byteOffset);
if (this.byteOffset > this.buffer.byteLength) {
throw new_INDEX_SIZE_ERR(); // byteOffset out of range
}
if (arguments.length < 3) {
this.byteLength = this.buffer.byteLength - this.byteOffset;
} else {
this.byteLength = ECMAScript.ToUint32(byteLength);
}
if ((this.byteOffset + this.byteLength) > this.buffer.byteLength) {
throw new_INDEX_SIZE_ERR(); // byteOffset and length reference an area beyond the end of the buffer
}
configureProperties(this);
};
// TODO: Reintroduce this to get correct hierarchy
// if (typeof ArrayBufferView === 'function') {
// DataView.prototype = new ArrayBufferView();
// }
function makeDataView_getter(arrayType) {
return function(byteOffset, littleEndian) {
byteOffset = ECMAScript.ToUint32(byteOffset);
if (byteOffset + arrayType.BYTES_PER_ELEMENT > this.byteLength) {
throw new_INDEX_SIZE_ERR(); // Array index out of range
}
byteOffset += this.byteOffset;
var uint8Array = new Uint8Array(this.buffer, byteOffset, arrayType.BYTES_PER_ELEMENT), bytes = [], i;
for (i = 0; i < arrayType.BYTES_PER_ELEMENT; i += 1) {
bytes.push(r(uint8Array, i));
}
if (Boolean(littleEndian) === Boolean(IS_BIG_ENDIAN)) {
bytes.reverse();
}
return r(new arrayType(new Uint8Array(bytes).buffer), 0);
};
}
DataView.prototype.getUint8 = makeDataView_getter(Uint8Array);
DataView.prototype.getInt8 = makeDataView_getter(Int8Array);
DataView.prototype.getUint16 = makeDataView_getter(Uint16Array);
DataView.prototype.getInt16 = makeDataView_getter(Int16Array);
DataView.prototype.getUint32 = makeDataView_getter(Uint32Array);
DataView.prototype.getInt32 = makeDataView_getter(Int32Array);
DataView.prototype.getFloat32 = makeDataView_getter(Float32Array);
DataView.prototype.getFloat64 = makeDataView_getter(Float64Array);
function makeDataView_setter(arrayType) {
return function(byteOffset, value, littleEndian) {
byteOffset = ECMAScript.ToUint32(byteOffset);
if (byteOffset + arrayType.BYTES_PER_ELEMENT > this.byteLength) {
throw new_INDEX_SIZE_ERR(); // Array index out of range
}
// Get bytes
var typeArray = new arrayType([ value ]), byteArray = new Uint8Array(typeArray.buffer), bytes = [], i, byteView;
for (i = 0; i < arrayType.BYTES_PER_ELEMENT; i += 1) {
bytes.push(r(byteArray, i));
}
// Flip if necessary
if (Boolean(littleEndian) === Boolean(IS_BIG_ENDIAN)) {
bytes.reverse();
}
// Write them
byteView = new Uint8Array(this.buffer, byteOffset, arrayType.BYTES_PER_ELEMENT);
byteView.set(bytes);
};
}
DataView.prototype.setUint8 = makeDataView_setter(Uint8Array);
DataView.prototype.setInt8 = makeDataView_setter(Int8Array);
DataView.prototype.setUint16 = makeDataView_setter(Uint16Array);
DataView.prototype.setInt16 = makeDataView_setter(Int16Array);
DataView.prototype.setUint32 = makeDataView_setter(Uint32Array);
DataView.prototype.setInt32 = makeDataView_setter(Int32Array);
DataView.prototype.setFloat32 = makeDataView_setter(Float32Array);
DataView.prototype.setFloat64 = makeDataView_setter(Float64Array);
if (USE_NATIVE_IF_AVAILABLE) {
global.DataView = global.DataView || DataView;
} else {
global.DataView = DataView;
}
}());
}(this));

View file

@ -1,58 +0,0 @@
/// Code can be found at: https://gist.github.com/1284012
(function() {
var a64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', a256 = {
indexOf : function(c) {
return c.charCodeAt(0);
},
charAt : String.fromCharCode
};
function code(s, discard, alpha, beta, w1, w2) {
s = String(s);
var b = 0, x = '', i, c, bs = 1, sb = 1, length = s.length, tmp;
for (i = 0; i < length || (!discard && sb > 1); i += 1) {
b *= w1;
bs *= w1;
if (i < length) {
c = alpha.indexOf(s.charAt(i));
if (c <= -1 || c >= w1) {
throw new RangeError();
}
sb *= w1;
b += c;
}
while (bs >= w2) {
bs /= w2;
if (sb > 1) {
tmp = b;
b %= bs;
x += beta.charAt((tmp - b) / bs);
sb /= w2;
}
}
}
return x;
}
if (!("btoa" in window))
window.btoa = function(s) {
s = code(s, false, a256, a64, 256, 64);
return s + '===='.slice((s.length % 4) || 4);
};
if (!("atob" in window))
window.atob = function(s) {
var i;
s = String(s).split('=');
for (i = s.length - 1; i >= 0; i -= 1) {
if (s[i].length % 4 === 1) {
throw new RangeError();
}
s[i] = code(s[i], true, a64, a256, 64, 256);
}
return s.join('');
};
})();

View file

@ -1,212 +0,0 @@
/*
* DataView.js:
* An implementation of the DataView class on top of typed arrays.
* Useful for Firefox 4 which implements TypedArrays but not DataView.
*
* Copyright 2011, David Flanagan
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
(function(global) {
// If DataView already exists, do nothing
if (global.DataView) return;
// If ArrayBuffer is not supported, fail with an error
if (!global.ArrayBuffer) fail("ArrayBuffer not supported");
// If ES5 is not supported, fail
if (!Object.defineProperties) fail("This module requires ECMAScript 5");
// Figure if the platform is natively little-endian.
// If the integer 0x00000001 is arranged in memory as 01 00 00 00 then
// we're on a little endian platform. On a big-endian platform we'd get
// get bytes 00 00 00 01 instead.
var nativele = new Int8Array(new Int32Array([1]).buffer)[0] === 1;
// A temporary array for copying or reversing bytes into.
// Since js is single-threaded, we only need this one static copy
var temp = new Uint8Array(8);
// The DataView() constructor
global.DataView = function DataView(buffer, offset, length) {
if (!(buffer instanceof ArrayBuffer)) fail("Bad ArrayBuffer");
// Default values for omitted arguments
offset = offset || 0;
length = length || (buffer.byteLength - offset);
if (offset < 0 || length < 0 || offset + length > buffer.byteLength) fail("Illegal offset and/or length");
// Define the 3 read-only, non-enumerable ArrayBufferView properties
Object.defineProperties(this, {
buffer: {
value: buffer,
enumerable: false,
writable: false,
configurable: false
},
byteOffset: {
value: offset,
enumerable: false,
writable: false,
configurable: false
},
byteLength: {
value: length,
enumerable: false,
writable: false,
configurable: false
},
_bytes: {
value: new Uint8Array(buffer, offset, length),
enumerable: false,
writable: false,
configurable: false
}
});
}
// The DataView prototype object
global.DataView.prototype = {
constructor: DataView,
getInt8: function getInt8(offset) {
return get(this, Int8Array, 1, offset);
},
getUint8: function getUint8(offset) {
return get(this, Uint8Array, 1, offset);
},
getInt16: function getInt16(offset, le) {
return get(this, Int16Array, 2, offset, le);
},
getUint16: function getUint16(offset, le) {
return get(this, Uint16Array, 2, offset, le);
},
getInt32: function getInt32(offset, le) {
return get(this, Int32Array, 4, offset, le);
},
getUint32: function getUint32(offset, le) {
return get(this, Uint32Array, 4, offset, le);
},
getFloat32: function getFloat32(offset, le) {
return get(this, Float32Array, 4, offset, le);
},
getFloat64: function getFloat32(offset, le) {
return get(this, Float64Array, 8, offset, le);
},
setInt8: function setInt8(offset, value) {
set(this, Int8Array, 1, offset, value);
},
setUint8: function setUint8(offset, value) {
set(this, Uint8Array, 1, offset, value);
},
setInt16: function setInt16(offset, value, le) {
set(this, Int16Array, 2, offset, value, le);
},
setUint16: function setUint16(offset, value, le) {
set(this, Uint16Array, 2, offset, value, le);
},
setInt32: function setInt32(offset, value, le) {
set(this, Int32Array, 4, offset, value, le);
},
setUint32: function setUint32(offset, value, le) {
set(this, Uint32Array, 4, offset, value, le);
},
setFloat32: function setFloat32(offset, value, le) {
set(this, Float32Array, 4, offset, value, le);
},
setFloat64: function setFloat64(offset, value, le) {
set(this, Float64Array, 8, offset, value, le);
}
};
// The get() utility function used by the get methods
function get(view, type, size, offset, le) {
if (offset === undefined) fail("Missing required offset argument");
if (offset < 0 || offset + size > view.byteLength) fail("Invalid index: " + offset);
if (size === 1 || !! le === nativele) {
// This is the easy case: the desired endianness
// matches the native endianness.
// Typed arrays require proper alignment. DataView does not.
if ((view.byteOffset + offset) % size === 0) return (new type(view.buffer, view.byteOffset + offset, 1))[0];
else {
// Copy bytes into the temp array, to fix alignment
for (var i = 0; i < size; i++)
temp[i] = view._bytes[offset + i];
// Now wrap that buffer with an array of the desired type
return (new type(temp.buffer))[0];
}
} else {
// If the native endianness doesn't match the desired, then
// we have to reverse the bytes
for (var i = 0; i < size; i++)
temp[size - i - 1] = view._bytes[offset + i];
return (new type(temp.buffer))[0];
}
}
// The set() utility function used by the set methods
function set(view, type, size, offset, value, le) {
if (offset === undefined) fail("Missing required offset argument");
if (value === undefined) fail("Missing required value argument");
if (offset < 0 || offset + size > view.byteLength) fail("Invalid index: " + offset);
if (size === 1 || !! le === nativele) {
// This is the easy case: the desired endianness
// matches the native endianness.
if ((view.byteOffset + offset) % size === 0) {
(new type(view.buffer, view.byteOffset + offset, 1))[0] = value;
} else {
(new type(temp.buffer))[0] = value;
// Now copy the bytes into the view's buffer
for (var i = 0; i < size; i++)
view._bytes[i + offset] = temp[i];
}
} else {
// If the native endianness doesn't match the desired, then
// we have to reverse the bytes
// Store the value into our temporary buffer
(new type(temp.buffer))[0] = value;
// Now copy the bytes, in reverse order, into the view's buffer
for (var i = 0; i < size; i++)
view._bytes[offset + i] = temp[size - 1 - i];
}
}
function fail(msg) {
throw new Error(msg);
}
}(this));

View file

@ -1 +0,0 @@
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo Blob</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../mime-types.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test1.js"></script>
</body>
</html>

View file

@ -1,46 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var FILENAME = "lorem.txt";
var blob;
function onerror(message) {
console.error(message);
}
function zipBlob(blob, callback) {
zip.createWriter(new zip.BlobWriter("application/zip"), function(zipWriter) {
zipWriter.add(FILENAME, new zip.BlobReader(blob), function() {
zipWriter.close(callback);
});
}, onerror);
}
function unzipBlob(blob, callback) {
zip.createReader(new zip.BlobReader(blob), function(zipReader) {
zipReader.getEntries(function(entries) {
entries[0].getData(new zip.BlobWriter(zip.getMimeType(entries[0].filename)), function(data) {
zipReader.close();
callback(data);
});
});
}, onerror);
}
function logBlobText(blob) {
var reader = new FileReader();
reader.onload = function(e) {
console.log(e.target.result);
console.log("--------------");
};
reader.readAsText(blob);
}
zip.workerScriptsPath = "../";
blob = new Blob([ TEXT_CONTENT ], {
type : zip.getMimeType(FILENAME)
});
logBlobText(blob);
zipBlob(blob, function(zippedBlob) {
unzipBlob(zippedBlob, function(unzippedBlob) {
logBlobText(unzippedBlob);
});
});

View file

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo STORE unzip</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="../zip-ext.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test10.js"></script>
</body>
</html>

View file

@ -1,34 +0,0 @@
var URL = "lorem_store.zip";
var zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function zipImportedZip(callback) {
var directory = zipFs.root.addDirectory("import");
directory.importHttpContent(URL, false, function() {
zipFs.exportBlob(callback);
}, onerror);
}
function unzipBlob(blob, callback) {
zipFs.importBlob(blob, function() {
var directory = zipFs.root.getChildByName("import");
var firstEntry = directory.children[0];
firstEntry.getText(callback);
}, onerror);
}
function logText(text) {
console.log(text);
console.log("--------------");
}
zip.workerScriptsPath = "../";
zipImportedZip(function(zippedBlob) {
unzipBlob(zippedBlob, function(unzippedText) {
logText(unzippedText);
});
});

View file

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo ZipEntry.prototype.getFileEntry (File)</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="../zip-ext.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test11.js"></script>
</body>
</html>

View file

@ -1,67 +0,0 @@
var requestFileSystem = window.webkitRequestFileSystem || window.mozRequestFileSystem || window.msRequestFileSystem || window.requestFileSystem;
var URL = "lorem.zip", FILENAME = "lorem.txt";
var filesystem, zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function removeRecursively(entry, onend, onerror) {
var rootReader = entry.createReader();
rootReader.readEntries(function(entries) {
var i = 0;
function next() {
i++;
removeNextEntry();
}
function removeNextEntry() {
var entry = entries[i];
if (entry) {
if (entry.isDirectory)
removeRecursively(entry, next, onerror);
if (entry.isFile)
entry.remove(next, onerror);
} else
onend();
}
removeNextEntry();
}, onerror);
}
function importZipToFilesystem(callback) {
zipFs.importHttpContent(URL, false, function() {
filesystem.root.getFile(FILENAME, {
create : true
}, function(fileEntry) {
var zippedFile = zipFs.root.getChildByName(FILENAME);
zippedFile.getFileEntry(fileEntry, callback, null, onerror);
}, onerror);
}, onerror);
}
function logFile(file) {
var reader = new FileReader();
reader.onload = function(event) {
console.log(event.target.result);
console.log("--------------");
};
reader.onerror = onerror;
reader.readAsText(file);
}
function test() {
importZipToFilesystem(function() {
filesystem.root.getFile(FILENAME, null, function(fileEntry) {
fileEntry.file(logFile, onerror);
}, onerror);
}, onerror);
}
zip.workerScriptsPath = "../";
requestFileSystem(TEMPORARY, 4 * 1024 * 1024 * 1024, function(fs) {
filesystem = fs;
removeRecursively(filesystem.root, test, onerror);
}, onerror);

View file

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo ZipEntry.prototype.getFileEntry (Directory)</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="../zip-ext.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test12.js"></script>
</body>
</html>

View file

@ -1,66 +0,0 @@
var requestFileSystem = window.webkitRequestFileSystem || window.mozRequestFileSystem || window.msRequestFileSystem || window.requestFileSystem;
var URL = "lorem2.zip";
var filesystem, zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function removeRecursively(entry, onend, onerror) {
var rootReader = entry.createReader();
rootReader.readEntries(function(entries) {
var i = 0;
function next() {
i++;
removeNextEntry();
}
function removeNextEntry() {
var entry = entries[i];
if (entry) {
if (entry.isDirectory)
removeRecursively(entry, next, onerror);
if (entry.isFile)
entry.remove(next, onerror);
} else
onend();
}
removeNextEntry();
}, onerror);
}
function importZipToFilesystem(callback) {
zipFs.importHttpContent(URL, false, function() {
zipFs.root.getFileEntry(filesystem.root, callback, null, onerror);
}, onerror);
}
function logFile(file) {
var reader = new FileReader();
reader.onload = function(event) {
console.log(event.target.result);
console.log("--------------");
};
reader.onerror = onerror;
reader.readAsText(file);
}
function test() {
importZipToFilesystem(function() {
filesystem.root.getDirectory("aaa", null, function(directoryEntry) {
directoryEntry.getDirectory("ccc", null, function(directoryEntry) {
directoryEntry.getFile("lorem.txt", null, function(fileEntry) {
fileEntry.file(logFile, onerror);
}, onerror);
}, onerror);
}, onerror);
}, onerror);
}
zip.workerScriptsPath = "../";
requestFileSystem(TEMPORARY, 4 * 1024 * 1024 * 1024, function(fs) {
filesystem = fs;
removeRecursively(filesystem.root, test, test);
}, onerror);

View file

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo ZipEntry.prototype.addFileEntry (File)</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test13.js"></script>
</body>
</html>

View file

@ -1,74 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var requestFileSystem = window.webkitRequestFileSystem || window.mozRequestFileSystem || window.msRequestFileSystem || window.requestFileSystem;
var filesystem, zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function removeRecursively(entry, onend, onerror) {
var rootReader = entry.createReader();
rootReader.readEntries(function(entries) {
var i = 0;
function next() {
i++;
removeNextEntry();
}
function removeNextEntry() {
var entry = entries[i];
if (entry) {
if (entry.isDirectory)
removeRecursively(entry, next, onerror);
if (entry.isFile)
entry.remove(next, onerror);
} else
onend();
}
removeNextEntry();
}, onerror);
}
function addFileEntryAndReadFile(fileEntry, callback) {
zipFs.root.addFileEntry(fileEntry, function() {
var zipEntry = zipFs.root.getChildByName("lorem.txt");
zipEntry.getText(callback);
}, onerror);
}
function logText(text) {
console.log(text);
console.log("--------------");
}
function initFileSystem(callback) {
filesystem.root.getFile("lorem.txt", {
create : true
}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.onwrite = function() {
callback(fileEntry);
};
writer.onerror = onerror;
writer.write(new Blob([ TEXT_CONTENT ], {
type : "text/plain"
}));
}, onerror);
}, onerror);
}
function test() {
initFileSystem(function(fileEntry) {
addFileEntryAndReadFile(fileEntry, function(text) {
logText(text);
}, onerror);
});
}
zip.workerScriptsPath = "../";
requestFileSystem(TEMPORARY, 4 * 1024 * 1024 * 1024, function(fs) {
filesystem = fs;
removeRecursively(filesystem.root, test, onerror);
}, onerror);

View file

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo ZipEntry.prototype.addFileEntry (Directory)</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test14.js"></script>
</body>
</html>

View file

@ -1,80 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var requestFileSystem = window.webkitRequestFileSystem || window.mozRequestFileSystem || window.msRequestFileSystem || window.requestFileSystem;
var filesystem, zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function removeRecursively(entry, onend, onerror) {
var rootReader = entry.createReader();
rootReader.readEntries(function(entries) {
var i = 0;
function next() {
i++;
removeNextEntry();
}
function removeNextEntry() {
var entry = entries[i];
if (entry) {
if (entry.isDirectory)
removeRecursively(entry, next, onerror);
if (entry.isFile)
entry.remove(next, onerror);
} else
onend();
}
removeNextEntry();
}, onerror);
}
function addDirectoryAndReadFile(callback) {
zipFs.root.addFileEntry(filesystem.root, function() {
var zipEntry = zipFs.root.getChildByName("aaa").getChildByName("ccc").getChildByName("lorem.txt");
zipEntry.getText(callback);
}, onerror);
}
function logText(text) {
console.log(text);
console.log("--------------");
}
function initFileSystem(callback) {
filesystem.root.getDirectory("aaa", {
create : true
}, function(directoryEntry) {
directoryEntry.getDirectory("ccc", {
create : true
}, function(directoryEntry) {
directoryEntry.getFile("lorem.txt", {
create : true
}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.onwrite = callback;
writer.onerror = onerror;
writer.write(new Blob([ TEXT_CONTENT ], {
type : "text/plain"
}));
}, onerror);
}, onerror);
}, onerror);
}, onerror);
}
function test() {
initFileSystem(function() {
addDirectoryAndReadFile(function(text) {
logText(text);
}, onerror);
});
}
zip.workerScriptsPath = "../";
requestFileSystem(TEMPORARY, 4 * 1024 * 1024 * 1024, function(fs) {
filesystem = fs;
removeRecursively(filesystem.root, test, onerror);
}, onerror);

View file

@ -1,12 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo parallel reads</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test15.js"></script>
</body>
</html>

View file

@ -1,60 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
function onerror(message) {
console.error(message);
}
function zipBlobs(blobs, callback) {
zip.createWriter(new zip.BlobWriter("application/zip"), function(zipWriter) {
var index = 0;
function next() {
if (index < blobs.length)
zipWriter.add(blobs[index].name, new zip.BlobReader(blobs[index].blob), function() {
index++;
next();
});
else
zipWriter.close(callback);
}
next();
}, onerror);
}
function unzipBlob(blob) {
zip.createReader(new zip.BlobReader(blob), function(zipReader) {
zipReader.getEntries(function(entries) {
var i;
for (i = 0; i < entries.length; i++)
entries[i].getData(new zip.TextWriter(), function(text) {
logText(text);
});
});
}, onerror);
}
function getBlob() {
return new Blob([ TEXT_CONTENT ], {
type : "text/plain"
});
}
function logText(text) {
console.log(text);
console.log("--------------");
}
zip.workerScriptsPath = "../";
var blobs = [ {
name : "lorem1.txt",
blob : getBlob()
}, {
name : "lorem2.txt",
blob : getBlob()
} ];
zipBlobs(blobs, function(zippedBlob) {
unzipBlob(zippedBlob);
});

View file

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo without web workers</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../deflate.js"></script>
<script type="text/javascript" src="../inflate.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test16.js"></script>
</body>
</html>

View file

@ -1,46 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var FILENAME = "lorem.txt";
var blob;
function onerror(message) {
console.error(message);
}
function zipBlob(blob, callback) {
zip.createWriter(new zip.BlobWriter("application/zip"), function(zipWriter) {
zipWriter.add(FILENAME, new zip.BlobReader(blob), function() {
zipWriter.close(callback);
});
}, onerror);
}
function unzipBlob(blob, callback) {
zip.createReader(new zip.BlobReader(blob), function(zipReader) {
zipReader.getEntries(function(entries) {
entries[0].getData(new zip.BlobWriter("text/plain"), function(data) {
zipReader.close();
callback(data);
});
});
}, onerror);
}
function logBlobText(blob) {
var reader = new FileReader();
reader.onload = function(e) {
console.log(e.target.result);
console.log("--------------");
};
reader.readAsText(blob);
}
zip.useWebWorkers = false;
blob = new Blob([ TEXT_CONTENT ], {
type : "text/plain"
});
logBlobText(blob);
zipBlob(blob, function(zippedBlob) {
unzipBlob(zippedBlob, function(unzippedBlob) {
logBlobText(unzippedBlob);
});
});

View file

@ -1,15 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo base 64 (compatible with IE, Firefox, Chrome, Safari)</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../deflate.js"></script>
<script type="text/javascript" src="../inflate.js"></script>
<script type="text/javascript" src="arraybuffer.js"></script>
<script type="text/javascript" src="base64.js"></script>
<script type="text/javascript" src="test17.js"></script>
</body>
</html>

View file

@ -1,41 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var FILENAME = "lorem.txt";
var dataURI = "data:text/plain;base64," + btoa(TEXT_CONTENT);
function onerror(message) {
console.error(message);
}
function zipDataURI(dataURI, callback) {
zip.createWriter(new zip.Data64URIWriter("application/zip"), function(zipWriter) {
zipWriter.add(FILENAME, new zip.Data64URIReader(dataURI), function() {
zipWriter.close(callback);
});
}, onerror);
}
function unzipDataURI(dataURI, callback) {
zip.createReader(new zip.Data64URIReader(dataURI), function(zipReader) {
zipReader.getEntries(function(entries) {
entries[0].getData(new zip.Data64URIWriter("text/plain"), function(data) {
zipReader.close();
callback(data);
});
});
}, onerror);
}
function logDataURI(dataURI) {
console.log(dataURI);
console.log("--------------");
}
zip.useWebWorkers = false;
logDataURI(dataURI);
zipDataURI(dataURI, function(zippedData64) {
logDataURI(zippedData64);
unzipDataURI(zippedData64, function(unzippedDataURI) {
logDataURI(unzippedDataURI);
});
});

View file

@ -1,16 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo ArrayBuffer</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-ext.js"></script>
<script type="text/javascript" src="../deflate.js"></script>
<script type="text/javascript" src="../inflate.js"></script>
<script type="text/javascript" src="arraybuffer.js"></script>
<script type="text/javascript" src="base64.js"></script>
<script type="text/javascript" src="test18.js"></script>
</body>
</html>

View file

@ -1,46 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var FILENAME = "lorem.txt";
var arrayBuffer;
function onerror(message) {
console.error(message);
}
function zipArrayBuffer(arrayBuffer, callback) {
zip.createWriter(new zip.ArrayBufferWriter(), function(zipWriter) {
zipWriter.add(FILENAME, new zip.ArrayBufferReader(arrayBuffer), function() {
zipWriter.close(callback);
});
}, onerror);
}
function unzipArrayBuffer(arrayBuffer, callback) {
zip.createReader(new zip.ArrayBufferReader(arrayBuffer), function(zipReader) {
zipReader.getEntries(function(entries) {
entries[0].getData(new zip.ArrayBufferWriter(), function(data) {
zipReader.close();
callback(data);
});
});
}, onerror);
}
function logArrayBufferText(arrayBuffer) {
var array = new Uint8Array(arrayBuffer);
var str = "";
Array.prototype.forEach.call(array, function(code) {
str += String.fromCharCode(code);
});
console.log(str);
}
zip.workerScriptsPath = "../";
arrayBuffer = new Uint8Array(Array.prototype.map.call(TEXT_CONTENT, function(c) {
return c.charCodeAt(0);
})).buffer;
logArrayBufferText(arrayBuffer);
zipArrayBuffer(arrayBuffer, function(zippedArrayBuffer) {
unzipArrayBuffer(zippedArrayBuffer, function(unzippedArrayBuffer) {
logArrayBufferText(unzippedArrayBuffer);
});
});

View file

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo File</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-ext.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="util.js"></script>
<script type="text/javascript" src="test2.js"></script>
</body>
</html>

View file

@ -1,49 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var FILENAME = "lorem.txt";
var blob, requestFileSystem = this.webkitRequestFileSystem || this.mozRequestFileSystem || this.requestFileSystem;
function onerror(message) {
console.error(message);
}
function zipBlob(blob, callback) {
createTempFile(function(fileEntry) {
zip.createWriter(new zip.FileWriter(fileEntry, "application/zip"), function(zipWriter) {
zipWriter.add(FILENAME, new zip.BlobReader(blob), function() {
zipWriter.close(callback);
});
}, onerror);
});
}
function unzipBlob(blob, callback) {
zip.createReader(new zip.BlobReader(blob), function(zipReader) {
zipReader.getEntries(function(entries) {
entries[0].getData(new zip.BlobWriter("text/plain"), function(data) {
zipReader.close();
callback(data);
});
});
}, onerror);
}
function logBlobText(blob) {
var reader = new FileReader();
reader.onload = function(e) {
console.log(e.target.result);
console.log("--------------");
};
reader.readAsText(blob);
}
zip.workerScriptsPath = "../";
blob = new Blob([TEXT_CONTENT], {
type: "text/plain"
});
logBlobText(blob);
zipBlob(blob, function(zippedBlob) {
unzipBlob(zippedBlob, function(unzippedBlob) {
logBlobText(unzippedBlob);
});
});

View file

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo filesystem API</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="../mime-types.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test3.js"></script>
</body>
</html>

View file

@ -1,40 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var FILENAME = "lorem.txt";
var blob, zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function zipBlob(blob, callback) {
zipFs.root.addBlob(FILENAME, blob);
zipFs.exportBlob(callback);
}
function unzipBlob(blob, callback) {
zipFs.importBlob(blob, function() {
var firstEntry = zipFs.root.children[0];
firstEntry.getBlob(zip.getMimeType(firstEntry.name), callback);
}, onerror);
}
function logBlobText(blob) {
var reader = new FileReader();
reader.onload = function(e) {
console.log(e.target.result);
console.log("--------------");
};
reader.readAsText(blob);
}
zip.workerScriptsPath = "../";
blob = new Blob([TEXT_CONTENT], {
type : zip.getMimeType(FILENAME)
});
logBlobText(blob);
zipBlob(blob, function(zippedBlob) {
unzipBlob(zippedBlob, function(unzippedBlob) {
logBlobText(unzippedBlob);
});
});

View file

@ -1,12 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo base 64</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test4.js"></script>
</body>
</html>

View file

@ -1,40 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var FILENAME = "lorem.txt";
var dataURI = "data:text/plain;base64," + btoa(TEXT_CONTENT);
function onerror(message) {
console.error(message);
}
function zipDataURI(dataURI, callback) {
zip.createWriter(new zip.BlobWriter("application/zip"), function(zipWriter) {
zipWriter.add(FILENAME, new zip.Data64URIReader(dataURI), function() {
zipWriter.close(callback);
});
}, onerror);
}
function unzipBlob(blob, callback) {
zip.createReader(new zip.BlobReader(blob), function(zipReader) {
zipReader.getEntries(function(entries) {
entries[0].getData(new zip.Data64URIWriter("text/plain"), function(data) {
zipReader.close();
callback(data);
});
});
}, onerror);
}
function logDataURI(dataURI) {
console.log(dataURI);
console.log("--------------");
}
zip.workerScriptsPath = "../";
logDataURI(dataURI);
zipDataURI(dataURI, function(zippedBlob) {
unzipBlob(zippedBlob, function(unzippedDataURI) {
logDataURI(unzippedDataURI);
});
});

View file

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo filesystem API with base64</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test5.js"></script>
</body>
</html>

View file

@ -1,33 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var FILENAME = "lorem.txt";
var dataURI = "data:text/plain;base64," + btoa(TEXT_CONTENT), zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function zipDataURI(dataURI, callback) {
zipFs.root.addData64URI(FILENAME, dataURI);
zipFs.exportData64URI(callback);
}
function unzipDataURI(dataURI, callback) {
zipFs.importData64URI(dataURI, function() {
var firstEntry = zipFs.root.children[0];
firstEntry.getData64URI("text/plain", callback, null, true);
}, onerror);
}
function logDataURI(dataURI) {
console.log(dataURI);
console.log("--------------");
}
zip.workerScriptsPath = "../";
logDataURI(dataURI);
zipDataURI(dataURI, function(zippedDataURI) {
unzipDataURI(zippedDataURI, function(unzippedDataURI) {
logDataURI(unzippedDataURI);
});
});

View file

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo filesystem API with base64</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test6.js"></script>
</body>
</html>

View file

@ -1,33 +0,0 @@
var TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
var FILENAME = "lorem.txt";
var zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function zipText(text, callback) {
zipFs.root.addText(FILENAME, text);
zipFs.exportBlob(callback);
}
function unzipBlob(blob, callback) {
zipFs.importBlob(blob, function() {
var firstEntry = zipFs.root.children[0];
firstEntry.getText(callback);
}, onerror);
}
function logText(text) {
console.log(text);
console.log("--------------");
}
zip.workerScriptsPath = "../";
logText(TEXT_CONTENT);
zipText(TEXT_CONTENT, function(zippedBlob) {
unzipBlob(zippedBlob, function(unzippedText) {
logText(unzippedText);
});
});

View file

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo HttpReader</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="../zip-ext.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test7.js"></script>
</body>
</html>

View file

@ -1,18 +0,0 @@
var zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function logText(text) {
console.log(text);
console.log("--------------");
}
zip.workerScriptsPath = "../";
zipFs.importHttpContent("lorem.zip", false, function() {
var firstEntry = zipFs.root.children[0];
firstEntry.getText(function(data) {
logText(data);
});
}, onerror);

View file

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo FileHTTP</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="../zip-ext.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test8.js"></script>
</body>
</html>

View file

@ -1,31 +0,0 @@
var FILENAME = "lorem.txt", URL = "lorem.txt";
var zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function zipText(callback) {
zipFs.root.addHttpContent(FILENAME, URL);
zipFs.exportBlob(callback);
}
function unzipBlob(blob, callback) {
zipFs.importBlob(blob, function() {
var firstEntry = zipFs.root.children[0];
firstEntry.getText(callback);
}, onerror);
}
function logText(text) {
console.log(text);
console.log("--------------");
}
zip.workerScriptsPath = "../";
zipText(function(zippedBlob) {
unzipBlob(zippedBlob, function(unzippedText) {
logText(unzippedText);
});
});

View file

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Demo FileHTTP import</title>
</head>
<body>
<script type="text/javascript" src="../zip.js"></script>
<script type="text/javascript" src="../zip-fs.js"></script>
<script type="text/javascript" src="../zip-ext.js"></script>
<script type="text/javascript" src="dataview.js"></script>
<script type="text/javascript" src="test9.js"></script>
</body>
</html>

View file

@ -1,34 +0,0 @@
var URL = "lorem.zip";
var zipFs = new zip.fs.FS();
function onerror(message) {
console.error(message);
}
function zipImportedZip(callback) {
var directory = zipFs.root.addDirectory("import");
directory.importHttpContent(URL, false, function() {
zipFs.exportBlob(callback);
}, onerror);
}
function unzipBlob(blob, callback) {
zipFs.importBlob(blob, function() {
var directory = zipFs.root.getChildByName("import");
var firstEntry = directory.children[0];
firstEntry.getText(callback);
}, onerror);
}
function logText(text) {
console.log(text);
console.log("--------------");
}
zip.workerScriptsPath = "../";
zipImportedZip(function(zippedBlob) {
unzipBlob(zippedBlob, function(unzippedText) {
logText(unzippedText);
});
});

View file

@ -1,16 +0,0 @@
function createTempFile(callback) {
var TMP_FILENAME = "file.tmp";
requestFileSystem(TEMPORARY, 4 * 1024 * 1024 * 1024, function(filesystem) {
function create() {
filesystem.root.getFile(TMP_FILENAME, {
create : true
}, function(entry) {
callback(entry);
}, onerror);
}
filesystem.root.getFile(TMP_FILENAME, null, function(entry) {
entry.remove(create, create);
}, create);
});
}

View file

@ -1,241 +0,0 @@
/*
Copyright (c) 2013 Gildas Lormeau. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
(function() {
var ERR_HTTP_RANGE = "HTTP Range not supported.";
var Reader = zip.Reader;
var Writer = zip.Writer;
var ZipDirectoryEntry;
var appendABViewSupported;
try {
appendABViewSupported = new Blob([ new DataView(new ArrayBuffer(0)) ]).size === 0;
} catch (e) {
}
function HttpReader(url) {
var that = this;
function getData(callback, onerror) {
var request;
if (!that.data) {
request = new XMLHttpRequest();
request.addEventListener("load", function() {
if (!that.size)
that.size = Number(request.getResponseHeader("Content-Length"));
that.data = new Uint8Array(request.response);
callback();
}, false);
request.addEventListener("error", onerror, false);
request.open("GET", url);
request.responseType = "arraybuffer";
request.send();
} else
callback();
}
function init(callback, onerror) {
var request = new XMLHttpRequest();
request.addEventListener("load", function() {
that.size = Number(request.getResponseHeader("Content-Length"));
callback();
}, false);
request.addEventListener("error", onerror, false);
request.open("HEAD", url);
request.send();
}
function readUint8Array(index, length, callback, onerror) {
getData(function() {
callback(new Uint8Array(that.data.subarray(index, index + length)));
}, onerror);
}
that.size = 0;
that.init = init;
that.readUint8Array = readUint8Array;
}
HttpReader.prototype = new Reader();
HttpReader.prototype.constructor = HttpReader;
function HttpRangeReader(url) {
var that = this;
function init(callback, onerror) {
var request = new XMLHttpRequest();
request.addEventListener("load", function() {
that.size = Number(request.getResponseHeader("Content-Length"));
if (request.getResponseHeader("Accept-Ranges") == "bytes")
callback();
else
onerror(ERR_HTTP_RANGE);
}, false);
request.addEventListener("error", onerror, false);
request.open("HEAD", url);
request.send();
}
function readArrayBuffer(index, length, callback, onerror) {
var request = new XMLHttpRequest();
request.open("GET", url);
request.responseType = "arraybuffer";
request.setRequestHeader("Range", "bytes=" + index + "-" + (index + length - 1));
request.addEventListener("load", function() {
callback(request.response);
}, false);
request.addEventListener("error", onerror, false);
request.send();
}
function readUint8Array(index, length, callback, onerror) {
readArrayBuffer(index, length, function(arraybuffer) {
callback(new Uint8Array(arraybuffer));
}, onerror);
}
that.size = 0;
that.init = init;
that.readUint8Array = readUint8Array;
}
HttpRangeReader.prototype = new Reader();
HttpRangeReader.prototype.constructor = HttpRangeReader;
function ArrayBufferReader(arrayBuffer) {
var that = this;
function init(callback, onerror) {
that.size = arrayBuffer.byteLength;
callback();
}
function readUint8Array(index, length, callback, onerror) {
callback(new Uint8Array(arrayBuffer.slice(index, index + length)));
}
that.size = 0;
that.init = init;
that.readUint8Array = readUint8Array;
}
ArrayBufferReader.prototype = new Reader();
ArrayBufferReader.prototype.constructor = ArrayBufferReader;
function ArrayBufferWriter() {
var array, that = this;
function init(callback, onerror) {
array = new Uint8Array();
callback();
}
function writeUint8Array(arr, callback, onerror) {
var tmpArray = new Uint8Array(array.length + arr.length);
tmpArray.set(array);
tmpArray.set(arr, array.length);
array = tmpArray;
callback();
}
function getData(callback) {
callback(array.buffer);
}
that.init = init;
that.writeUint8Array = writeUint8Array;
that.getData = getData;
}
ArrayBufferWriter.prototype = new Writer();
ArrayBufferWriter.prototype.constructor = ArrayBufferWriter;
function FileWriter(fileEntry, contentType) {
var writer, that = this;
function init(callback, onerror) {
fileEntry.createWriter(function(fileWriter) {
writer = fileWriter;
callback();
}, onerror);
}
function writeUint8Array(array, callback, onerror) {
var blob = new Blob([ appendABViewSupported ? array : array.buffer ], {
type : contentType
});
writer.onwrite = function() {
writer.onwrite = null;
callback();
};
writer.onerror = onerror;
writer.write(blob);
}
function getData(callback) {
fileEntry.file(callback);
}
that.init = init;
that.writeUint8Array = writeUint8Array;
that.getData = getData;
}
FileWriter.prototype = new Writer();
FileWriter.prototype.constructor = FileWriter;
zip.FileWriter = FileWriter;
zip.HttpReader = HttpReader;
zip.HttpRangeReader = HttpRangeReader;
zip.ArrayBufferReader = ArrayBufferReader;
zip.ArrayBufferWriter = ArrayBufferWriter;
if (zip.fs) {
ZipDirectoryEntry = zip.fs.ZipDirectoryEntry;
ZipDirectoryEntry.prototype.addHttpContent = function(name, URL, useRangeHeader) {
function addChild(parent, name, params, directory) {
if (parent.directory)
return directory ? new ZipDirectoryEntry(parent.fs, name, params, parent) : new zip.fs.ZipFileEntry(parent.fs, name, params, parent);
else
throw "Parent entry is not a directory.";
}
return addChild(this, name, {
data : URL,
Reader : useRangeHeader ? HttpRangeReader : HttpReader
});
};
ZipDirectoryEntry.prototype.importHttpContent = function(URL, useRangeHeader, onend, onerror) {
this.importZip(useRangeHeader ? new HttpRangeReader(URL) : new HttpReader(URL), onend, onerror);
};
zip.fs.FS.prototype.importHttpContent = function(URL, useRangeHeader, onend, onerror) {
this.entries = [];
this.root = new ZipDirectoryEntry(this);
this.root.importHttpContent(URL, useRangeHeader, onend, onerror);
};
}
})();

View file

@ -1,538 +0,0 @@
/*
Copyright (c) 2013 Gildas Lormeau. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
(function() {
var CHUNK_SIZE = 512 * 1024;
var TextWriter = zip.TextWriter, //
BlobWriter = zip.BlobWriter, //
Data64URIWriter = zip.Data64URIWriter, //
Reader = zip.Reader, //
TextReader = zip.TextReader, //
BlobReader = zip.BlobReader, //
Data64URIReader = zip.Data64URIReader, //
createReader = zip.createReader, //
createWriter = zip.createWriter;
function ZipBlobReader(entry) {
var that = this, blobReader;
function init(callback) {
this.size = entry.uncompressedSize;
callback();
}
function getData(callback) {
if (that.data)
callback();
else
entry.getData(new BlobWriter(), function(data) {
that.data = data;
blobReader = new BlobReader(data);
callback();
}, null, that.checkCrc32);
}
function readUint8Array(index, length, callback, onerror) {
getData(function() {
blobReader.readUint8Array(index, length, callback, onerror);
}, onerror);
}
that.size = 0;
that.init = init;
that.readUint8Array = readUint8Array;
}
ZipBlobReader.prototype = new Reader();
ZipBlobReader.prototype.constructor = ZipBlobReader;
ZipBlobReader.prototype.checkCrc32 = false;
function getTotalSize(entry) {
var size = 0;
function process(entry) {
size += entry.uncompressedSize || 0;
entry.children.forEach(process);
}
process(entry);
return size;
}
function initReaders(entry, onend, onerror) {
var index = 0;
function next() {
index++;
if (index < entry.children.length)
process(entry.children[index]);
else
onend();
}
function process(child) {
if (child.directory)
initReaders(child, next, onerror);
else {
child.reader = new child.Reader(child.data, onerror);
child.reader.init(function() {
child.uncompressedSize = child.reader.size;
next();
});
}
}
if (entry.children.length)
process(entry.children[index]);
else
onend();
}
function detach(entry) {
var children = entry.parent.children;
children.forEach(function(child, index) {
if (child.id == entry.id)
children.splice(index, 1);
});
}
function exportZip(zipWriter, entry, onend, onprogress, totalSize) {
var currentIndex = 0;
function process(zipWriter, entry, onend, onprogress, totalSize) {
var childIndex = 0;
function exportChild() {
var child = entry.children[childIndex];
if (child)
zipWriter.add(child.getFullname(), child.reader, function() {
currentIndex += child.uncompressedSize || 0;
process(zipWriter, child, function() {
childIndex++;
exportChild();
}, onprogress, totalSize);
}, function(index) {
if (onprogress)
onprogress(currentIndex + index, totalSize);
}, {
directory : child.directory,
version : child.zipVersion
});
else
onend();
}
exportChild();
}
process(zipWriter, entry, onend, onprogress, totalSize);
}
function addFileEntry(zipEntry, fileEntry, onend, onerror) {
function getChildren(fileEntry, callback) {
if (fileEntry.isDirectory)
fileEntry.createReader().readEntries(callback);
if (fileEntry.isFile)
callback([]);
}
function process(zipEntry, fileEntry, onend) {
getChildren(fileEntry, function(children) {
var childIndex = 0;
function addChild(child) {
function nextChild(childFileEntry) {
process(childFileEntry, child, function() {
childIndex++;
processChild();
});
}
if (child.isDirectory)
nextChild(zipEntry.addDirectory(child.name));
if (child.isFile)
child.file(function(file) {
var childZipEntry = zipEntry.addBlob(child.name, file);
childZipEntry.uncompressedSize = file.size;
nextChild(childZipEntry);
}, onerror);
}
function processChild() {
var child = children[childIndex];
if (child)
addChild(child);
else
onend();
}
processChild();
});
}
if (fileEntry.isDirectory)
process(zipEntry, fileEntry, onend);
else
fileEntry.file(function(file) {
zipEntry.addBlob(fileEntry.name, file);
onend();
}, onerror);
}
function getFileEntry(fileEntry, entry, onend, onprogress, onerror, totalSize, checkCrc32) {
var currentIndex = 0;
function process(fileEntry, entry, onend, onprogress, onerror, totalSize) {
var childIndex = 0;
function addChild(child) {
function nextChild(childFileEntry) {
currentIndex += child.uncompressedSize || 0;
process(childFileEntry, child, function() {
childIndex++;
processChild();
}, onprogress, onerror, totalSize);
}
if (child.directory)
fileEntry.getDirectory(child.name, {
create : true
}, nextChild, onerror);
else
fileEntry.getFile(child.name, {
create : true
}, function(file) {
child.getData(new zip.FileWriter(file, zip.getMimeType(child.name)), nextChild, function(index) {
if (onprogress)
onprogress(currentIndex + index, totalSize);
}, checkCrc32);
}, onerror);
}
function processChild() {
var child = entry.children[childIndex];
if (child)
addChild(child);
else
onend();
}
processChild();
}
if (entry.directory)
process(fileEntry, entry, onend, onprogress, onerror, totalSize);
else
entry.getData(new zip.FileWriter(fileEntry, zip.getMimeType(entry.name)), onend, onprogress, checkCrc32);
}
function resetFS(fs) {
fs.entries = [];
fs.root = new ZipDirectoryEntry(fs);
}
function bufferedCopy(reader, writer, onend, onprogress, onerror) {
var chunkIndex = 0;
function stepCopy() {
var index = chunkIndex * CHUNK_SIZE;
if (onprogress)
onprogress(index, reader.size);
if (index < reader.size)
reader.readUint8Array(index, Math.min(CHUNK_SIZE, reader.size - index), function(array) {
writer.writeUint8Array(new Uint8Array(array), function() {
chunkIndex++;
stepCopy();
});
}, onerror);
else
writer.getData(onend);
}
stepCopy();
}
function getEntryData(writer, onend, onprogress, onerror) {
var that = this;
if (!writer || (writer.constructor == that.Writer && that.data))
onend(that.data);
else {
if (!that.reader)
that.reader = new that.Reader(that.data, onerror);
that.reader.init(function() {
writer.init(function() {
bufferedCopy(that.reader, writer, onend, onprogress, onerror);
}, onerror);
});
}
}
function addChild(parent, name, params, directory) {
if (parent.directory)
return directory ? new ZipDirectoryEntry(parent.fs, name, params, parent) : new ZipFileEntry(parent.fs, name, params, parent);
else
throw "Parent entry is not a directory.";
}
function ZipEntry() {
}
ZipEntry.prototype = {
init : function(fs, name, params, parent) {
var that = this;
if (fs.root && parent && parent.getChildByName(name))
throw "Entry filename already exists.";
if (!params)
params = {};
that.fs = fs;
that.name = name;
that.id = fs.entries.length;
that.parent = parent;
that.children = [];
that.zipVersion = params.zipVersion || 0x14;
that.uncompressedSize = 0;
fs.entries.push(that);
if (parent)
that.parent.children.push(that);
},
getFileEntry : function(fileEntry, onend, onprogress, onerror, checkCrc32) {
var that = this;
initReaders(that, function() {
getFileEntry(fileEntry, that, onend, onprogress, onerror, getTotalSize(that), checkCrc32);
}, onerror);
},
moveTo : function(target) {
var that = this;
if (target.directory) {
if (!target.isDescendantOf(that)) {
if (that != target) {
if (target.getChildByName(that.name))
throw "Entry filename already exists.";
detach(that);
that.parent = target;
target.children.push(that);
}
} else
throw "Entry is a ancestor of target entry.";
} else
throw "Target entry is not a directory.";
},
getFullname : function() {
var that = this, fullname = that.name, entry = that.parent;
while (entry) {
fullname = (entry.name ? entry.name + "/" : "") + fullname;
entry = entry.parent;
}
return fullname;
},
isDescendantOf : function(ancestor) {
var entry = this.parent;
while (entry && entry.id != ancestor.id)
entry = entry.parent;
return !!entry;
}
};
ZipEntry.prototype.constructor = ZipEntry;
var ZipFileEntryProto;
function ZipFileEntry(fs, name, params, parent) {
var that = this;
ZipEntry.prototype.init.call(that, fs, name, params, parent);
that.Reader = params.Reader;
that.Writer = params.Writer;
that.data = params.data;
that.getData = params.getData || getEntryData;
}
ZipFileEntry.prototype = ZipFileEntryProto = new ZipEntry();
ZipFileEntryProto.constructor = ZipFileEntry;
ZipFileEntryProto.getText = function(onend, onprogress, checkCrc32, encoding) {
this.getData(new TextWriter(encoding), onend, onprogress, checkCrc32);
};
ZipFileEntryProto.getBlob = function(mimeType, onend, onprogress, checkCrc32) {
this.getData(new BlobWriter(mimeType), onend, onprogress, checkCrc32);
};
ZipFileEntryProto.getData64URI = function(mimeType, onend, onprogress, checkCrc32) {
this.getData(new Data64URIWriter(mimeType), onend, onprogress, checkCrc32);
};
var ZipDirectoryEntryProto;
function ZipDirectoryEntry(fs, name, params, parent) {
var that = this;
ZipEntry.prototype.init.call(that, fs, name, params, parent);
that.directory = true;
}
ZipDirectoryEntry.prototype = ZipDirectoryEntryProto = new ZipEntry();
ZipDirectoryEntryProto.constructor = ZipDirectoryEntry;
ZipDirectoryEntryProto.addDirectory = function(name) {
return addChild(this, name, null, true);
};
ZipDirectoryEntryProto.addText = function(name, text) {
return addChild(this, name, {
data : text,
Reader : TextReader,
Writer : TextWriter
});
};
ZipDirectoryEntryProto.addBlob = function(name, blob) {
return addChild(this, name, {
data : blob,
Reader : BlobReader,
Writer : BlobWriter
});
};
ZipDirectoryEntryProto.addData64URI = function(name, dataURI) {
return addChild(this, name, {
data : dataURI,
Reader : Data64URIReader,
Writer : Data64URIWriter
});
};
ZipDirectoryEntryProto.addFileEntry = function(fileEntry, onend, onerror) {
addFileEntry(this, fileEntry, onend, onerror);
};
ZipDirectoryEntryProto.addData = function(name, params) {
return addChild(this, name, params);
};
ZipDirectoryEntryProto.importBlob = function(blob, onend, onerror) {
this.importZip(new BlobReader(blob), onend, onerror);
};
ZipDirectoryEntryProto.importText = function(text, onend, onerror) {
this.importZip(new TextReader(text), onend, onerror);
};
ZipDirectoryEntryProto.importData64URI = function(dataURI, onend, onerror) {
this.importZip(new Data64URIReader(dataURI), onend, onerror);
};
ZipDirectoryEntryProto.exportBlob = function(onend, onprogress, onerror) {
this.exportZip(new BlobWriter("application/zip"), onend, onprogress, onerror);
};
ZipDirectoryEntryProto.exportText = function(onend, onprogress, onerror) {
this.exportZip(new TextWriter(), onend, onprogress, onerror);
};
ZipDirectoryEntryProto.exportFileEntry = function(fileEntry, onend, onprogress, onerror) {
this.exportZip(new zip.FileWriter(fileEntry, "application/zip"), onend, onprogress, onerror);
};
ZipDirectoryEntryProto.exportData64URI = function(onend, onprogress, onerror) {
this.exportZip(new Data64URIWriter("application/zip"), onend, onprogress, onerror);
};
ZipDirectoryEntryProto.importZip = function(reader, onend, onerror) {
var that = this;
createReader(reader, function(zipReader) {
zipReader.getEntries(function(entries) {
entries.forEach(function(entry) {
var parent = that, path = entry.filename.split("/"), name = path.pop();
path.forEach(function(pathPart) {
parent = parent.getChildByName(pathPart) || new ZipDirectoryEntry(that.fs, pathPart, null, parent);
});
if (!entry.directory)
addChild(parent, name, {
data : entry,
Reader : ZipBlobReader
});
});
onend();
});
}, onerror);
};
ZipDirectoryEntryProto.exportZip = function(writer, onend, onprogress, onerror) {
var that = this;
initReaders(that, function() {
createWriter(writer, function(zipWriter) {
exportZip(zipWriter, that, function() {
zipWriter.close(onend);
}, onprogress, getTotalSize(that));
}, onerror);
}, onerror);
};
ZipDirectoryEntryProto.getChildByName = function(name) {
var childIndex, child, that = this;
for (childIndex = 0; childIndex < that.children.length; childIndex++) {
child = that.children[childIndex];
if (child.name == name)
return child;
}
};
function FS() {
resetFS(this);
}
FS.prototype = {
remove : function(entry) {
detach(entry);
this.entries[entry.id] = null;
},
find : function(fullname) {
var index, path = fullname.split("/"), node = this.root;
for (index = 0; node && index < path.length; index++)
node = node.getChildByName(path[index]);
return node;
},
getById : function(id) {
return this.entries[id];
},
importBlob : function(blob, onend, onerror) {
resetFS(this);
this.root.importBlob(blob, onend, onerror);
},
importText : function(text, onend, onerror) {
resetFS(this);
this.root.importText(text, onend, onerror);
},
importData64URI : function(dataURI, onend, onerror) {
resetFS(this);
this.root.importData64URI(dataURI, onend, onerror);
},
exportBlob : function(onend, onprogress, onerror) {
this.root.exportBlob(onend, onprogress, onerror);
},
exportText : function(onend, onprogress, onerror) {
this.root.exportText(onend, onprogress, onerror);
},
exportFileEntry : function(fileEntry, onend, onprogress, onerror) {
this.root.exportFileEntry(fileEntry, onend, onprogress, onerror);
},
exportData64URI : function(onend, onprogress, onerror) {
this.root.exportData64URI(onend, onprogress, onerror);
}
};
zip.fs = {
FS : FS,
ZipDirectoryEntry : ZipDirectoryEntry,
ZipFileEntry : ZipFileEntry
};
zip.getMimeType = function() {
return "application/octet-stream";
};
})();

View file

@ -1,801 +0,0 @@
/*
Copyright (c) 2013 Gildas Lormeau. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
(function(obj) {
var ERR_BAD_FORMAT = "File format is not recognized.";
var ERR_ENCRYPTED = "File contains encrypted entry.";
var ERR_ZIP64 = "File is using Zip64 (4gb+ file size).";
var ERR_READ = "Error while reading zip file.";
var ERR_WRITE = "Error while writing zip file.";
var ERR_WRITE_DATA = "Error while writing file data.";
var ERR_READ_DATA = "Error while reading file data.";
var ERR_DUPLICATED_NAME = "File already exists.";
var CHUNK_SIZE = 512 * 1024;
var INFLATE_JS = "inflate.js";
var DEFLATE_JS = "deflate.js";
var TEXT_PLAIN = "text/plain";
var MESSAGE_EVENT = "message";
var appendABViewSupported;
try {
appendABViewSupported = new Blob([ new DataView(new ArrayBuffer(0)) ]).size === 0;
} catch (e) {
}
function Crc32() {
var crc = -1, that = this;
that.append = function(data) {
var offset, table = that.table;
for (offset = 0; offset < data.length; offset++)
crc = (crc >>> 8) ^ table[(crc ^ data[offset]) & 0xFF];
};
that.get = function() {
return ~crc;
};
}
Crc32.prototype.table = (function() {
var i, j, t, table = [];
for (i = 0; i < 256; i++) {
t = i;
for (j = 0; j < 8; j++)
if (t & 1)
t = (t >>> 1) ^ 0xEDB88320;
else
t = t >>> 1;
table[i] = t;
}
return table;
})();
function blobSlice(blob, index, length) {
if (blob.slice)
return blob.slice(index, index + length);
else if (blob.webkitSlice)
return blob.webkitSlice(index, index + length);
else if (blob.mozSlice)
return blob.mozSlice(index, index + length);
else if (blob.msSlice)
return blob.msSlice(index, index + length);
}
function getDataHelper(byteLength, bytes) {
var dataBuffer, dataArray;
dataBuffer = new ArrayBuffer(byteLength);
dataArray = new Uint8Array(dataBuffer);
if (bytes)
dataArray.set(bytes, 0);
return {
buffer : dataBuffer,
array : dataArray,
view : new DataView(dataBuffer)
};
}
// Readers
function Reader() {
}
function TextReader(text) {
var that = this, blobReader;
function init(callback, onerror) {
var blob = new Blob([ text ], {
type : TEXT_PLAIN
});
blobReader = new BlobReader(blob);
blobReader.init(function() {
that.size = blobReader.size;
callback();
}, onerror);
}
function readUint8Array(index, length, callback, onerror) {
blobReader.readUint8Array(index, length, callback, onerror);
}
that.size = 0;
that.init = init;
that.readUint8Array = readUint8Array;
}
TextReader.prototype = new Reader();
TextReader.prototype.constructor = TextReader;
function Data64URIReader(dataURI) {
var that = this, dataStart;
function init(callback) {
var dataEnd = dataURI.length;
while (dataURI.charAt(dataEnd - 1) == "=")
dataEnd--;
dataStart = dataURI.indexOf(",") + 1;
that.size = Math.floor((dataEnd - dataStart) * 0.75);
callback();
}
function readUint8Array(index, length, callback) {
var i, data = getDataHelper(length);
var start = Math.floor(index / 3) * 4;
var end = Math.ceil((index + length) / 3) * 4;
var bytes = obj.atob(dataURI.substring(start + dataStart, end + dataStart));
var delta = index - Math.floor(start / 4) * 3;
for (i = delta; i < delta + length; i++)
data.array[i - delta] = bytes.charCodeAt(i);
callback(data.array);
}
that.size = 0;
that.init = init;
that.readUint8Array = readUint8Array;
}
Data64URIReader.prototype = new Reader();
Data64URIReader.prototype.constructor = Data64URIReader;
function BlobReader(blob) {
var that = this;
function init(callback) {
this.size = blob.size;
callback();
}
function readUint8Array(index, length, callback, onerror) {
var reader = new FileReader();
reader.onload = function(e) {
callback(new Uint8Array(e.target.result));
};
reader.onerror = onerror;
reader.readAsArrayBuffer(blobSlice(blob, index, length));
}
that.size = 0;
that.init = init;
that.readUint8Array = readUint8Array;
}
BlobReader.prototype = new Reader();
BlobReader.prototype.constructor = BlobReader;
// Writers
function Writer() {
}
Writer.prototype.getData = function(callback) {
callback(this.data);
};
function TextWriter(encoding) {
var that = this, blob;
function init(callback) {
blob = new Blob([], {
type : TEXT_PLAIN
});
callback();
}
function writeUint8Array(array, callback) {
blob = new Blob([ blob, appendABViewSupported ? array : array.buffer ], {
type : TEXT_PLAIN
});
callback();
}
function getData(callback, onerror) {
var reader = new FileReader();
reader.onload = function(e) {
callback(e.target.result);
};
reader.onerror = onerror;
reader.readAsText(blob, encoding);
}
that.init = init;
that.writeUint8Array = writeUint8Array;
that.getData = getData;
}
TextWriter.prototype = new Writer();
TextWriter.prototype.constructor = TextWriter;
function Data64URIWriter(contentType) {
var that = this, data = "", pending = "";
function init(callback) {
data += "data:" + (contentType || "") + ";base64,";
callback();
}
function writeUint8Array(array, callback) {
var i, delta = pending.length, dataString = pending;
pending = "";
for (i = 0; i < (Math.floor((delta + array.length) / 3) * 3) - delta; i++)
dataString += String.fromCharCode(array[i]);
for (; i < array.length; i++)
pending += String.fromCharCode(array[i]);
if (dataString.length > 2)
data += obj.btoa(dataString);
else
pending = dataString;
callback();
}
function getData(callback) {
callback(data + obj.btoa(pending));
}
that.init = init;
that.writeUint8Array = writeUint8Array;
that.getData = getData;
}
Data64URIWriter.prototype = new Writer();
Data64URIWriter.prototype.constructor = Data64URIWriter;
function BlobWriter(contentType) {
var blob, that = this;
function init(callback) {
blob = new Blob([], {
type : contentType
});
callback();
}
function writeUint8Array(array, callback) {
blob = new Blob([ blob, appendABViewSupported ? array : array.buffer ], {
type : contentType
});
callback();
}
function getData(callback) {
callback(blob);
}
that.init = init;
that.writeUint8Array = writeUint8Array;
that.getData = getData;
}
BlobWriter.prototype = new Writer();
BlobWriter.prototype.constructor = BlobWriter;
// inflate/deflate core functions
function launchWorkerProcess(worker, reader, writer, offset, size, onappend, onprogress, onend, onreaderror, onwriteerror) {
var chunkIndex = 0, index, outputSize;
function onflush() {
worker.removeEventListener(MESSAGE_EVENT, onmessage, false);
onend(outputSize);
}
function onmessage(event) {
var message = event.data, data = message.data;
if (message.onappend) {
outputSize += data.length;
writer.writeUint8Array(data, function() {
onappend(false, data);
step();
}, onwriteerror);
}
if (message.onflush)
if (data) {
outputSize += data.length;
writer.writeUint8Array(data, function() {
onappend(false, data);
onflush();
}, onwriteerror);
} else
onflush();
if (message.progress && onprogress)
onprogress(index + message.current, size);
}
function step() {
index = chunkIndex * CHUNK_SIZE;
if (index < size)
reader.readUint8Array(offset + index, Math.min(CHUNK_SIZE, size - index), function(array) {
worker.postMessage({
append : true,
data : array
});
chunkIndex++;
if (onprogress)
onprogress(index, size);
onappend(true, array);
}, onreaderror);
else
worker.postMessage({
flush : true
});
}
outputSize = 0;
worker.addEventListener(MESSAGE_EVENT, onmessage, false);
step();
}
function launchProcess(process, reader, writer, offset, size, onappend, onprogress, onend, onreaderror, onwriteerror) {
var chunkIndex = 0, index, outputSize = 0;
function step() {
var outputData;
index = chunkIndex * CHUNK_SIZE;
if (index < size)
reader.readUint8Array(offset + index, Math.min(CHUNK_SIZE, size - index), function(inputData) {
var outputData = process.append(inputData, function() {
if (onprogress)
onprogress(offset + index, size);
});
outputSize += outputData.length;
onappend(true, inputData);
writer.writeUint8Array(outputData, function() {
onappend(false, outputData);
chunkIndex++;
setTimeout(step, 1);
}, onwriteerror);
if (onprogress)
onprogress(index, size);
}, onreaderror);
else {
outputData = process.flush();
if (outputData) {
outputSize += outputData.length;
writer.writeUint8Array(outputData, function() {
onappend(false, outputData);
onend(outputSize);
}, onwriteerror);
} else
onend(outputSize);
}
}
step();
}
function inflate(reader, writer, offset, size, computeCrc32, onend, onprogress, onreaderror, onwriteerror) {
var worker, crc32 = new Crc32();
function oninflateappend(sending, array) {
if (computeCrc32 && !sending)
crc32.append(array);
}
function oninflateend(outputSize) {
onend(outputSize, crc32.get());
}
if (obj.zip.useWebWorkers) {
worker = new Worker(obj.zip.workerScriptsPath + INFLATE_JS);
launchWorkerProcess(worker, reader, writer, offset, size, oninflateappend, onprogress, oninflateend, onreaderror, onwriteerror);
} else
launchProcess(new obj.zip.Inflater(), reader, writer, offset, size, oninflateappend, onprogress, oninflateend, onreaderror, onwriteerror);
return worker;
}
function deflate(reader, writer, level, onend, onprogress, onreaderror, onwriteerror) {
var worker, crc32 = new Crc32();
function ondeflateappend(sending, array) {
if (sending)
crc32.append(array);
}
function ondeflateend(outputSize) {
onend(outputSize, crc32.get());
}
function onmessage() {
worker.removeEventListener(MESSAGE_EVENT, onmessage, false);
launchWorkerProcess(worker, reader, writer, 0, reader.size, ondeflateappend, onprogress, ondeflateend, onreaderror, onwriteerror);
}
if (obj.zip.useWebWorkers) {
worker = new Worker(obj.zip.workerScriptsPath + DEFLATE_JS);
worker.addEventListener(MESSAGE_EVENT, onmessage, false);
worker.postMessage({
init : true,
level : level
});
} else
launchProcess(new obj.zip.Deflater(), reader, writer, 0, reader.size, ondeflateappend, onprogress, ondeflateend, onreaderror, onwriteerror);
return worker;
}
function copy(reader, writer, offset, size, computeCrc32, onend, onprogress, onreaderror, onwriteerror) {
var chunkIndex = 0, crc32 = new Crc32();
function step() {
var index = chunkIndex * CHUNK_SIZE;
if (index < size)
reader.readUint8Array(offset + index, Math.min(CHUNK_SIZE, size - index), function(array) {
if (computeCrc32)
crc32.append(array);
if (onprogress)
onprogress(index, size, array);
writer.writeUint8Array(array, function() {
chunkIndex++;
step();
}, onwriteerror);
}, onreaderror);
else
onend(size, crc32.get());
}
step();
}
// ZipReader
function decodeASCII(str) {
var i, out = "", charCode, extendedASCII = [ '\u00C7', '\u00FC', '\u00E9', '\u00E2', '\u00E4', '\u00E0', '\u00E5', '\u00E7', '\u00EA', '\u00EB',
'\u00E8', '\u00EF', '\u00EE', '\u00EC', '\u00C4', '\u00C5', '\u00C9', '\u00E6', '\u00C6', '\u00F4', '\u00F6', '\u00F2', '\u00FB', '\u00F9',
'\u00FF', '\u00D6', '\u00DC', '\u00F8', '\u00A3', '\u00D8', '\u00D7', '\u0192', '\u00E1', '\u00ED', '\u00F3', '\u00FA', '\u00F1', '\u00D1',
'\u00AA', '\u00BA', '\u00BF', '\u00AE', '\u00AC', '\u00BD', '\u00BC', '\u00A1', '\u00AB', '\u00BB', '_', '_', '_', '\u00A6', '\u00A6',
'\u00C1', '\u00C2', '\u00C0', '\u00A9', '\u00A6', '\u00A6', '+', '+', '\u00A2', '\u00A5', '+', '+', '-', '-', '+', '-', '+', '\u00E3',
'\u00C3', '+', '+', '-', '-', '\u00A6', '-', '+', '\u00A4', '\u00F0', '\u00D0', '\u00CA', '\u00CB', '\u00C8', 'i', '\u00CD', '\u00CE',
'\u00CF', '+', '+', '_', '_', '\u00A6', '\u00CC', '_', '\u00D3', '\u00DF', '\u00D4', '\u00D2', '\u00F5', '\u00D5', '\u00B5', '\u00FE',
'\u00DE', '\u00DA', '\u00DB', '\u00D9', '\u00FD', '\u00DD', '\u00AF', '\u00B4', '\u00AD', '\u00B1', '_', '\u00BE', '\u00B6', '\u00A7',
'\u00F7', '\u00B8', '\u00B0', '\u00A8', '\u00B7', '\u00B9', '\u00B3', '\u00B2', '_', ' ' ];
for (i = 0; i < str.length; i++) {
charCode = str.charCodeAt(i) & 0xFF;
if (charCode > 127)
out += extendedASCII[charCode - 128];
else
out += String.fromCharCode(charCode);
}
return out;
}
function decodeUTF8(string) {
return decodeURIComponent(escape(string));
}
function getString(bytes) {
var i, str = "";
for (i = 0; i < bytes.length; i++)
str += String.fromCharCode(bytes[i]);
return str;
}
function getDate(timeRaw) {
var date = (timeRaw & 0xffff0000) >> 16, time = timeRaw & 0x0000ffff;
try {
return new Date(1980 + ((date & 0xFE00) >> 9), ((date & 0x01E0) >> 5) - 1, date & 0x001F, (time & 0xF800) >> 11, (time & 0x07E0) >> 5,
(time & 0x001F) * 2, 0);
} catch (e) {
}
}
function readCommonHeader(entry, data, index, centralDirectory, onerror) {
entry.version = data.view.getUint16(index, true);
entry.bitFlag = data.view.getUint16(index + 2, true);
entry.compressionMethod = data.view.getUint16(index + 4, true);
entry.lastModDateRaw = data.view.getUint32(index + 6, true);
entry.lastModDate = getDate(entry.lastModDateRaw);
if ((entry.bitFlag & 0x01) === 0x01) {
onerror(ERR_ENCRYPTED);
return;
}
if (centralDirectory || (entry.bitFlag & 0x0008) != 0x0008) {
entry.crc32 = data.view.getUint32(index + 10, true);
entry.compressedSize = data.view.getUint32(index + 14, true);
entry.uncompressedSize = data.view.getUint32(index + 18, true);
}
if (entry.compressedSize === 0xFFFFFFFF || entry.uncompressedSize === 0xFFFFFFFF) {
onerror(ERR_ZIP64);
return;
}
entry.filenameLength = data.view.getUint16(index + 22, true);
entry.extraFieldLength = data.view.getUint16(index + 24, true);
}
function createZipReader(reader, onerror) {
function Entry() {
}
Entry.prototype.getData = function(writer, onend, onprogress, checkCrc32) {
var that = this, worker;
function terminate(callback, param) {
if (worker)
worker.terminate();
worker = null;
if (callback)
callback(param);
}
function testCrc32(crc32) {
var dataCrc32 = getDataHelper(4);
dataCrc32.view.setUint32(0, crc32);
return that.crc32 == dataCrc32.view.getUint32(0);
}
function getWriterData(uncompressedSize, crc32) {
if (checkCrc32 && !testCrc32(crc32))
onreaderror();
else
writer.getData(function(data) {
terminate(onend, data);
});
}
function onreaderror() {
terminate(onerror, ERR_READ_DATA);
}
function onwriteerror() {
terminate(onerror, ERR_WRITE_DATA);
}
reader.readUint8Array(that.offset, 30, function(bytes) {
var data = getDataHelper(bytes.length, bytes), dataOffset;
if (data.view.getUint32(0) != 0x504b0304) {
onerror(ERR_BAD_FORMAT);
return;
}
readCommonHeader(that, data, 4, false, onerror);
dataOffset = that.offset + 30 + that.filenameLength + that.extraFieldLength;
writer.init(function() {
if (that.compressionMethod === 0)
copy(reader, writer, dataOffset, that.compressedSize, checkCrc32, getWriterData, onprogress, onreaderror, onwriteerror);
else
worker = inflate(reader, writer, dataOffset, that.compressedSize, checkCrc32, getWriterData, onprogress, onreaderror, onwriteerror);
}, onwriteerror);
}, onreaderror);
};
function seekEOCDR(offset, entriesCallback) {
reader.readUint8Array(reader.size - offset, offset, function(bytes) {
var dataView = getDataHelper(bytes.length, bytes).view;
if (dataView.getUint32(0) != 0x504b0506) {
seekEOCDR(offset + 1, entriesCallback);
} else {
entriesCallback(dataView);
}
}, function() {
onerror(ERR_READ);
});
}
return {
getEntries : function(callback) {
if (reader.size < 22) {
onerror(ERR_BAD_FORMAT);
return;
}
// look for End of central directory record
seekEOCDR(22, function(dataView) {
var datalength, fileslength;
datalength = dataView.getUint32(16, true);
fileslength = dataView.getUint16(8, true);
reader.readUint8Array(datalength, reader.size - datalength, function(bytes) {
var i, index = 0, entries = [], entry, filename, comment, data = getDataHelper(bytes.length, bytes);
for (i = 0; i < fileslength; i++) {
entry = new Entry();
if (data.view.getUint32(index) != 0x504b0102) {
onerror(ERR_BAD_FORMAT);
return;
}
readCommonHeader(entry, data, index + 6, true, onerror);
entry.commentLength = data.view.getUint16(index + 32, true);
entry.directory = ((data.view.getUint8(index + 38) & 0x10) == 0x10);
entry.offset = data.view.getUint32(index + 42, true);
filename = getString(data.array.subarray(index + 46, index + 46 + entry.filenameLength));
entry.filename = ((entry.bitFlag & 0x0800) === 0x0800) ? decodeUTF8(filename) : decodeASCII(filename);
if (!entry.directory && entry.filename.charAt(entry.filename.length - 1) == "/")
entry.directory = true;
comment = getString(data.array.subarray(index + 46 + entry.filenameLength + entry.extraFieldLength, index + 46
+ entry.filenameLength + entry.extraFieldLength + entry.commentLength));
entry.comment = ((entry.bitFlag & 0x0800) === 0x0800) ? decodeUTF8(comment) : decodeASCII(comment);
entries.push(entry);
index += 46 + entry.filenameLength + entry.extraFieldLength + entry.commentLength;
}
callback(entries);
}, function() {
onerror(ERR_READ);
});
});
},
close : function(callback) {
if (callback)
callback();
}
};
}
// ZipWriter
function encodeUTF8(string) {
return unescape(encodeURIComponent(string));
}
function getBytes(str) {
var i, array = [];
for (i = 0; i < str.length; i++)
array.push(str.charCodeAt(i));
return array;
}
function createZipWriter(writer, onerror, dontDeflate) {
var worker, files = {}, filenames = [], datalength = 0;
function terminate(callback, message) {
if (worker)
worker.terminate();
worker = null;
if (callback)
callback(message);
}
function onwriteerror() {
terminate(onerror, ERR_WRITE);
}
function onreaderror() {
terminate(onerror, ERR_READ_DATA);
}
return {
add : function(name, reader, onend, onprogress, options) {
var header, filename, date;
function writeHeader(callback) {
var data;
date = options.lastModDate || new Date();
header = getDataHelper(26);
files[name] = {
headerArray : header.array,
directory : options.directory,
filename : filename,
offset : datalength,
comment : getBytes(encodeUTF8(options.comment || ""))
};
header.view.setUint32(0, 0x14000808);
if (options.version)
header.view.setUint8(0, options.version);
if (!dontDeflate && options.level !== 0 && !options.directory)
header.view.setUint16(4, 0x0800);
header.view.setUint16(6, (((date.getHours() << 6) | date.getMinutes()) << 5) | date.getSeconds() / 2, true);
header.view.setUint16(8, ((((date.getFullYear() - 1980) << 4) | (date.getMonth() + 1)) << 5) | date.getDate(), true);
header.view.setUint16(22, filename.length, true);
data = getDataHelper(30 + filename.length);
data.view.setUint32(0, 0x504b0304);
data.array.set(header.array, 4);
data.array.set(filename, 30);
datalength += data.array.length;
writer.writeUint8Array(data.array, callback, onwriteerror);
}
function writeFooter(compressedLength, crc32) {
var footer = getDataHelper(16);
datalength += compressedLength || 0;
footer.view.setUint32(0, 0x504b0708);
if (typeof crc32 != "undefined") {
header.view.setUint32(10, crc32, true);
footer.view.setUint32(4, crc32, true);
}
if (reader) {
footer.view.setUint32(8, compressedLength, true);
header.view.setUint32(14, compressedLength, true);
footer.view.setUint32(12, reader.size, true);
header.view.setUint32(18, reader.size, true);
}
writer.writeUint8Array(footer.array, function() {
datalength += 16;
terminate(onend);
}, onwriteerror);
}
function writeFile() {
options = options || {};
name = name.trim();
if (options.directory && name.charAt(name.length - 1) != "/")
name += "/";
if (files.hasOwnProperty(name)) {
onerror(ERR_DUPLICATED_NAME);
return;
}
filename = getBytes(encodeUTF8(name));
filenames.push(name);
writeHeader(function() {
if (reader)
if (dontDeflate || options.level === 0)
copy(reader, writer, 0, reader.size, true, writeFooter, onprogress, onreaderror, onwriteerror);
else
worker = deflate(reader, writer, options.level, writeFooter, onprogress, onreaderror, onwriteerror);
else
writeFooter();
}, onwriteerror);
}
if (reader)
reader.init(writeFile, onreaderror);
else
writeFile();
},
close : function(callback) {
var data, length = 0, index = 0, indexFilename, file;
for (indexFilename = 0; indexFilename < filenames.length; indexFilename++) {
file = files[filenames[indexFilename]];
length += 46 + file.filename.length + file.comment.length;
}
data = getDataHelper(length + 22);
for (indexFilename = 0; indexFilename < filenames.length; indexFilename++) {
file = files[filenames[indexFilename]];
data.view.setUint32(index, 0x504b0102);
data.view.setUint16(index + 4, 0x1400);
data.array.set(file.headerArray, index + 6);
data.view.setUint16(index + 32, file.comment.length, true);
if (file.directory)
data.view.setUint8(index + 38, 0x10);
data.view.setUint32(index + 42, file.offset, true);
data.array.set(file.filename, index + 46);
data.array.set(file.comment, index + 46 + file.filename.length);
index += 46 + file.filename.length + file.comment.length;
}
data.view.setUint32(index, 0x504b0506);
data.view.setUint16(index + 8, filenames.length, true);
data.view.setUint16(index + 10, filenames.length, true);
data.view.setUint32(index + 12, length, true);
data.view.setUint32(index + 16, datalength, true);
writer.writeUint8Array(data.array, function() {
terminate(function() {
writer.getData(callback);
});
}, onwriteerror);
}
};
}
obj.zip = {
Reader : Reader,
Writer : Writer,
BlobReader : BlobReader,
Data64URIReader : Data64URIReader,
TextReader : TextReader,
BlobWriter : BlobWriter,
Data64URIWriter : Data64URIWriter,
TextWriter : TextWriter,
createReader : function(reader, callback, onerror) {
reader.init(function() {
callback(createZipReader(reader, onerror));
}, onerror);
},
createWriter : function(writer, callback, onerror, dontDeflate) {
writer.init(function() {
callback(createZipWriter(writer, onerror, dontDeflate));
}, onerror);
},
workerScriptsPath : "",
useWebWorkers : true
};
})(this);