info@54: /** info@54: info@54: JSZip - A Javascript class for generating and reading zip files info@54: info@54: info@54: (c) 2009-2012 Stuart Knightley info@54: Dual licenced under the MIT license or GPLv3. See LICENSE.markdown. info@54: info@54: Usage: info@54: zip = new JSZip(); info@54: zip.file("hello.txt", "Hello, World!").file("tempfile", "nothing"); info@54: zip.folder("images").file("smile.gif", base64Data, {base64: true}); info@54: zip.file("Xmas.txt", "Ho ho ho !", {date : new Date("December 25, 2007 00:00:01")}); info@54: zip.remove("tempfile"); info@54: info@54: base64zip = zip.generate(); info@54: info@54: **/ info@54: // We use strict, but it should not be placed outside of a function because info@54: // the environment is shared inside the browser. info@54: // "use strict"; info@54: info@54: /** info@54: * Representation a of zip file in js info@54: * @constructor info@54: * @param {String=|ArrayBuffer=|Uint8Array=|Buffer=} data the data to load, if any (optional). info@54: * @param {Object=} options the options for creating this objects (optional). info@54: */ info@54: var JSZip = function(data, options) { info@54: // object containing the files : info@54: // { info@54: // "folder/" : {...}, info@54: // "folder/data.txt" : {...} info@54: // } info@54: this.files = {}; info@54: info@54: // Where we are in the hierarchy info@54: this.root = ""; info@54: info@54: if (data) { info@54: this.load(data, options); info@54: } info@54: }; info@54: info@54: JSZip.signature = { info@54: LOCAL_FILE_HEADER : "\x50\x4b\x03\x04", info@54: CENTRAL_FILE_HEADER : "\x50\x4b\x01\x02", info@54: CENTRAL_DIRECTORY_END : "\x50\x4b\x05\x06", info@54: ZIP64_CENTRAL_DIRECTORY_LOCATOR : "\x50\x4b\x06\x07", info@54: ZIP64_CENTRAL_DIRECTORY_END : "\x50\x4b\x06\x06", info@54: DATA_DESCRIPTOR : "\x50\x4b\x07\x08" info@54: }; info@54: info@54: // Default properties for a new file info@54: JSZip.defaults = { info@54: base64: false, info@54: binary: false, info@54: dir: false, info@54: date: null, info@54: compression: null info@54: }; info@54: info@54: /* info@54: * List features that require a modern browser, and if the current browser support them. info@54: */ info@54: JSZip.support = { info@54: // contains true if JSZip can read/generate ArrayBuffer, false otherwise. info@54: arraybuffer : (function(){ info@54: return typeof ArrayBuffer !== "undefined" && typeof Uint8Array !== "undefined"; info@54: })(), info@54: // contains true if JSZip can read/generate nodejs Buffer, false otherwise. info@54: nodebuffer : (function(){ info@54: return typeof Buffer !== "undefined"; info@54: })(), info@54: // contains true if JSZip can read/generate Uint8Array, false otherwise. info@54: uint8array : (function(){ info@54: return typeof Uint8Array !== "undefined"; info@54: })(), info@54: // contains true if JSZip can read/generate Blob, false otherwise. info@54: blob : (function(){ info@54: // the spec started with BlobBuilder then replaced it with a construtor for Blob. info@54: // Result : we have browsers that : info@54: // * know the BlobBuilder (but with prefix) info@54: // * know the Blob constructor info@54: // * know about Blob but not about how to build them info@54: // About the "=== 0" test : if given the wrong type, it may be converted to a string. info@54: // Instead of an empty content, we will get "[object Uint8Array]" for example. info@54: if (typeof ArrayBuffer === "undefined") { info@54: return false; info@54: } info@54: var buffer = new ArrayBuffer(0); info@54: try { info@54: return new Blob([buffer], { type: "application/zip" }).size === 0; info@54: } info@54: catch(e) {} info@54: info@54: try { info@54: var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; info@54: var builder = new BlobBuilder(); info@54: builder.append(buffer); info@54: return builder.getBlob('application/zip').size === 0; info@54: } info@54: catch(e) {} info@54: info@54: return false; info@54: })() info@54: }; info@54: info@54: JSZip.prototype = (function () { info@54: var textEncoder, textDecoder; info@54: if ( info@54: JSZip.support.uint8array && info@54: typeof TextEncoder === "function" && info@54: typeof TextDecoder === "function" info@54: ) { info@54: textEncoder = new TextEncoder("utf-8"); info@54: textDecoder = new TextDecoder("utf-8"); info@54: } info@54: info@54: /** info@54: * Returns the raw data of a ZipObject, decompress the content if necessary. info@54: * @param {ZipObject} file the file to use. info@54: * @return {String|ArrayBuffer|Uint8Array|Buffer} the data. info@54: */ info@54: var getRawData = function (file) { info@54: if (file._data instanceof JSZip.CompressedObject) { info@54: file._data = file._data.getContent(); info@54: file.options.binary = true; info@54: file.options.base64 = false; info@54: info@54: if (JSZip.utils.getTypeOf(file._data) === "uint8array") { info@54: var copy = file._data; info@54: // when reading an arraybuffer, the CompressedObject mechanism will keep it and subarray() a Uint8Array. info@54: // if we request a file in the same format, we might get the same Uint8Array or its ArrayBuffer (the original zip file). info@54: file._data = new Uint8Array(copy.length); info@54: // with an empty Uint8Array, Opera fails with a "Offset larger than array size" info@54: if (copy.length !== 0) { info@54: file._data.set(copy, 0); info@54: } info@54: } info@54: } info@54: return file._data; info@54: }; info@54: info@54: /** info@54: * Returns the data of a ZipObject in a binary form. If the content is an unicode string, encode it. info@54: * @param {ZipObject} file the file to use. info@54: * @return {String|ArrayBuffer|Uint8Array|Buffer} the data. info@54: */ info@54: var getBinaryData = function (file) { info@54: var result = getRawData(file), type = JSZip.utils.getTypeOf(result); info@54: if (type === "string") { info@54: if (!file.options.binary) { info@54: // unicode text ! info@54: // unicode string => binary string is a painful process, check if we can avoid it. info@54: if (textEncoder) { info@54: return textEncoder.encode(result); info@54: } info@54: if (JSZip.support.nodebuffer) { info@54: return new Buffer(result, "utf-8"); info@54: } info@54: } info@54: return file.asBinary(); info@54: } info@54: return result; info@54: }; info@54: info@54: /** info@54: * Transform this._data into a string. info@54: * @param {function} filter a function String -> String, applied if not null on the result. info@54: * @return {String} the string representing this._data. info@54: */ info@54: var dataToString = function (asUTF8) { info@54: var result = getRawData(this); info@54: if (result === null || typeof result === "undefined") { info@54: return ""; info@54: } info@54: // if the data is a base64 string, we decode it before checking the encoding ! info@54: if (this.options.base64) { info@54: result = JSZip.base64.decode(result); info@54: } info@54: if (asUTF8 && this.options.binary) { info@54: // JSZip.prototype.utf8decode supports arrays as input info@54: // skip to array => string step, utf8decode will do it. info@54: result = JSZip.prototype.utf8decode(result); info@54: } else { info@54: // no utf8 transformation, do the array => string step. info@54: result = JSZip.utils.transformTo("string", result); info@54: } info@54: info@54: if (!asUTF8 && !this.options.binary) { info@54: result = JSZip.prototype.utf8encode(result); info@54: } info@54: return result; info@54: }; info@54: /** info@54: * A simple object representing a file in the zip file. info@54: * @constructor info@54: * @param {string} name the name of the file info@54: * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data info@54: * @param {Object} options the options of the file info@54: */ info@54: var ZipObject = function (name, data, options) { info@54: this.name = name; info@54: this._data = data; info@54: this.options = options; info@54: }; info@54: info@54: ZipObject.prototype = { info@54: /** info@54: * Return the content as UTF8 string. info@54: * @return {string} the UTF8 string. info@54: */ info@54: asText : function () { info@54: return dataToString.call(this, true); info@54: }, info@54: /** info@54: * Returns the binary content. info@54: * @return {string} the content as binary. info@54: */ info@54: asBinary : function () { info@54: return dataToString.call(this, false); info@54: }, info@54: /** info@54: * Returns the content as a nodejs Buffer. info@54: * @return {Buffer} the content as a Buffer. info@54: */ info@54: asNodeBuffer : function () { info@54: var result = getBinaryData(this); info@54: return JSZip.utils.transformTo("nodebuffer", result); info@54: }, info@54: /** info@54: * Returns the content as an Uint8Array. info@54: * @return {Uint8Array} the content as an Uint8Array. info@54: */ info@54: asUint8Array : function () { info@54: var result = getBinaryData(this); info@54: return JSZip.utils.transformTo("uint8array", result); info@54: }, info@54: /** info@54: * Returns the content as an ArrayBuffer. info@54: * @return {ArrayBuffer} the content as an ArrayBufer. info@54: */ info@54: asArrayBuffer : function () { info@54: return this.asUint8Array().buffer; info@54: } info@54: }; info@54: info@54: /** info@54: * Transform an integer into a string in hexadecimal. info@54: * @private info@54: * @param {number} dec the number to convert. info@54: * @param {number} bytes the number of bytes to generate. info@54: * @returns {string} the result. info@54: */ info@54: var decToHex = function(dec, bytes) { info@54: var hex = "", i; info@54: for(i = 0; i < bytes; i++) { info@54: hex += String.fromCharCode(dec&0xff); info@54: dec=dec>>>8; info@54: } info@54: return hex; info@54: }; info@54: info@54: /** info@54: * Merge the objects passed as parameters into a new one. info@54: * @private info@54: * @param {...Object} var_args All objects to merge. info@54: * @return {Object} a new object with the data of the others. info@54: */ info@54: var extend = function () { info@54: var result = {}, i, attr; info@54: for (i = 0; i < arguments.length; i++) { // arguments is not enumerable in some browsers info@54: for (attr in arguments[i]) { info@54: if (arguments[i].hasOwnProperty(attr) && typeof result[attr] === "undefined") { info@54: result[attr] = arguments[i][attr]; info@54: } info@54: } info@54: } info@54: return result; info@54: }; info@54: info@54: /** info@54: * Transforms the (incomplete) options from the user into the complete info@54: * set of options to create a file. info@54: * @private info@54: * @param {Object} o the options from the user. info@54: * @return {Object} the complete set of options. info@54: */ info@54: var prepareFileAttrs = function (o) { info@54: o = o || {}; info@54: /*jshint -W041 */ info@54: if (o.base64 === true && o.binary == null) { info@54: o.binary = true; info@54: } info@54: /*jshint +W041 */ info@54: o = extend(o, JSZip.defaults); info@54: o.date = o.date || new Date(); info@54: if (o.compression !== null) o.compression = o.compression.toUpperCase(); info@54: info@54: return o; info@54: }; info@54: info@54: /** info@54: * Add a file in the current folder. info@54: * @private info@54: * @param {string} name the name of the file info@54: * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data of the file info@54: * @param {Object} o the options of the file info@54: * @return {Object} the new file. info@54: */ info@54: var fileAdd = function (name, data, o) { info@54: // be sure sub folders exist info@54: var parent = parentFolder(name), dataType = JSZip.utils.getTypeOf(data); info@54: if (parent) { info@54: folderAdd.call(this, parent); info@54: } info@54: info@54: o = prepareFileAttrs(o); info@54: info@54: if (o.dir || data === null || typeof data === "undefined") { info@54: o.base64 = false; info@54: o.binary = false; info@54: data = null; info@54: } else if (dataType === "string") { info@54: if (o.binary && !o.base64) { info@54: // optimizedBinaryString == true means that the file has already been filtered with a 0xFF mask info@54: if (o.optimizedBinaryString !== true) { info@54: // this is a string, not in a base64 format. info@54: // Be sure that this is a correct "binary string" info@54: data = JSZip.utils.string2binary(data); info@54: } info@54: } info@54: } else { // arraybuffer, uint8array, ... info@54: o.base64 = false; info@54: o.binary = true; info@54: info@54: if (!dataType && !(data instanceof JSZip.CompressedObject)) { info@54: throw new Error("The data of '" + name + "' is in an unsupported format !"); info@54: } info@54: info@54: // special case : it's way easier to work with Uint8Array than with ArrayBuffer info@54: if (dataType === "arraybuffer") { info@54: data = JSZip.utils.transformTo("uint8array", data); info@54: } info@54: } info@54: info@54: var object = new ZipObject(name, data, o); info@54: this.files[name] = object; info@54: return object; info@54: }; info@54: info@54: info@54: /** info@54: * Find the parent folder of the path. info@54: * @private info@54: * @param {string} path the path to use info@54: * @return {string} the parent folder, or "" info@54: */ info@54: var parentFolder = function (path) { info@54: if (path.slice(-1) == '/') { info@54: path = path.substring(0, path.length - 1); info@54: } info@54: var lastSlash = path.lastIndexOf('/'); info@54: return (lastSlash > 0) ? path.substring(0, lastSlash) : ""; info@54: }; info@54: info@54: /** info@54: * Add a (sub) folder in the current folder. info@54: * @private info@54: * @param {string} name the folder's name info@54: * @return {Object} the new folder. info@54: */ info@54: var folderAdd = function (name) { info@54: // Check the name ends with a / info@54: if (name.slice(-1) != "/") { info@54: name += "/"; // IE doesn't like substr(-1) info@54: } info@54: info@54: // Does this folder already exist? info@54: if (!this.files[name]) { info@54: fileAdd.call(this, name, null, {dir:true}); info@54: } info@54: return this.files[name]; info@54: }; info@54: info@54: /** info@54: * Generate a JSZip.CompressedObject for a given zipOject. info@54: * @param {ZipObject} file the object to read. info@54: * @param {JSZip.compression} compression the compression to use. info@54: * @return {JSZip.CompressedObject} the compressed result. info@54: */ info@54: var generateCompressedObjectFrom = function (file, compression) { info@54: var result = new JSZip.CompressedObject(), content; info@54: info@54: // the data has not been decompressed, we might reuse things ! info@54: if (file._data instanceof JSZip.CompressedObject) { info@54: result.uncompressedSize = file._data.uncompressedSize; info@54: result.crc32 = file._data.crc32; info@54: info@54: if (result.uncompressedSize === 0 || file.options.dir) { info@54: compression = JSZip.compressions['STORE']; info@54: result.compressedContent = ""; info@54: result.crc32 = 0; info@54: } else if (file._data.compressionMethod === compression.magic) { info@54: result.compressedContent = file._data.getCompressedContent(); info@54: } else { info@54: content = file._data.getContent(); info@54: // need to decompress / recompress info@54: result.compressedContent = compression.compress(JSZip.utils.transformTo(compression.compressInputType, content)); info@54: } info@54: } else { info@54: // have uncompressed data info@54: content = getBinaryData(file); info@54: if (!content || content.length === 0 || file.options.dir) { info@54: compression = JSZip.compressions['STORE']; info@54: content = ""; info@54: } info@54: result.uncompressedSize = content.length; info@54: result.crc32 = this.crc32(content); info@54: result.compressedContent = compression.compress(JSZip.utils.transformTo(compression.compressInputType, content)); info@54: } info@54: info@54: result.compressedSize = result.compressedContent.length; info@54: result.compressionMethod = compression.magic; info@54: info@54: return result; info@54: }; info@54: info@54: /** info@54: * Generate the various parts used in the construction of the final zip file. info@54: * @param {string} name the file name. info@54: * @param {ZipObject} file the file content. info@54: * @param {JSZip.CompressedObject} compressedObject the compressed object. info@54: * @param {number} offset the current offset from the start of the zip file. info@54: * @return {object} the zip parts. info@54: */ info@54: var generateZipParts = function(name, file, compressedObject, offset) { info@54: var data = compressedObject.compressedContent, info@54: utfEncodedFileName = this.utf8encode(file.name), info@54: useUTF8 = utfEncodedFileName !== file.name, info@54: o = file.options, info@54: dosTime, info@54: dosDate; info@54: info@54: // date info@54: // @see http://www.delorie.com/djgpp/doc/rbinter/it/52/13.html info@54: // @see http://www.delorie.com/djgpp/doc/rbinter/it/65/16.html info@54: // @see http://www.delorie.com/djgpp/doc/rbinter/it/66/16.html info@54: info@54: dosTime = o.date.getHours(); info@54: dosTime = dosTime << 6; info@54: dosTime = dosTime | o.date.getMinutes(); info@54: dosTime = dosTime << 5; info@54: dosTime = dosTime | o.date.getSeconds() / 2; info@54: info@54: dosDate = o.date.getFullYear() - 1980; info@54: dosDate = dosDate << 4; info@54: dosDate = dosDate | (o.date.getMonth() + 1); info@54: dosDate = dosDate << 5; info@54: dosDate = dosDate | o.date.getDate(); info@54: info@54: info@54: var header = ""; info@54: info@54: // version needed to extract info@54: header += "\x0A\x00"; info@54: // general purpose bit flag info@54: // set bit 11 if utf8 info@54: header += useUTF8 ? "\x00\x08" : "\x00\x00"; info@54: // compression method info@54: header += compressedObject.compressionMethod; info@54: // last mod file time info@54: header += decToHex(dosTime, 2); info@54: // last mod file date info@54: header += decToHex(dosDate, 2); info@54: // crc-32 info@54: header += decToHex(compressedObject.crc32, 4); info@54: // compressed size info@54: header += decToHex(compressedObject.compressedSize, 4); info@54: // uncompressed size info@54: header += decToHex(compressedObject.uncompressedSize, 4); info@54: // file name length info@54: header += decToHex(utfEncodedFileName.length, 2); info@54: // extra field length info@54: header += "\x00\x00"; info@54: info@54: info@54: var fileRecord = JSZip.signature.LOCAL_FILE_HEADER + header + utfEncodedFileName; info@54: info@54: var dirRecord = JSZip.signature.CENTRAL_FILE_HEADER + info@54: // version made by (00: DOS) info@54: "\x14\x00" + info@54: // file header (common to file and central directory) info@54: header + info@54: // file comment length info@54: "\x00\x00" + info@54: // disk number start info@54: "\x00\x00" + info@54: // internal file attributes TODO info@54: "\x00\x00" + info@54: // external file attributes info@54: (file.options.dir===true?"\x10\x00\x00\x00":"\x00\x00\x00\x00")+ info@54: // relative offset of local header info@54: decToHex(offset, 4) + info@54: // file name info@54: utfEncodedFileName; info@54: info@54: info@54: return { info@54: fileRecord : fileRecord, info@54: dirRecord : dirRecord, info@54: compressedObject : compressedObject info@54: }; info@54: }; info@54: info@54: /** info@54: * An object to write any content to a string. info@54: * @constructor info@54: */ info@54: var StringWriter = function () { info@54: this.data = []; info@54: }; info@54: StringWriter.prototype = { info@54: /** info@54: * Append any content to the current string. info@54: * @param {Object} input the content to add. info@54: */ info@54: append : function (input) { info@54: input = JSZip.utils.transformTo("string", input); info@54: this.data.push(input); info@54: }, info@54: /** info@54: * Finalize the construction an return the result. info@54: * @return {string} the generated string. info@54: */ info@54: finalize : function () { info@54: return this.data.join(""); info@54: } info@54: }; info@54: /** info@54: * An object to write any content to an Uint8Array. info@54: * @constructor info@54: * @param {number} length The length of the array. info@54: */ info@54: var Uint8ArrayWriter = function (length) { info@54: this.data = new Uint8Array(length); info@54: this.index = 0; info@54: }; info@54: Uint8ArrayWriter.prototype = { info@54: /** info@54: * Append any content to the current array. info@54: * @param {Object} input the content to add. info@54: */ info@54: append : function (input) { info@54: if (input.length !== 0) { info@54: // with an empty Uint8Array, Opera fails with a "Offset larger than array size" info@54: input = JSZip.utils.transformTo("uint8array", input); info@54: this.data.set(input, this.index); info@54: this.index += input.length; info@54: } info@54: }, info@54: /** info@54: * Finalize the construction an return the result. info@54: * @return {Uint8Array} the generated array. info@54: */ info@54: finalize : function () { info@54: return this.data; info@54: } info@54: }; info@54: info@54: // return the actual prototype of JSZip info@54: return { info@54: /** info@54: * Read an existing zip and merge the data in the current JSZip object. info@54: * The implementation is in jszip-load.js, don't forget to include it. info@54: * @param {String|ArrayBuffer|Uint8Array|Buffer} stream The stream to load info@54: * @param {Object} options Options for loading the stream. info@54: * options.base64 : is the stream in base64 ? default : false info@54: * @return {JSZip} the current JSZip object info@54: */ info@54: load : function (stream, options) { info@54: throw new Error("Load method is not defined. Is the file jszip-load.js included ?"); info@54: }, info@54: info@54: /** info@54: * Filter nested files/folders with the specified function. info@54: * @param {Function} search the predicate to use : info@54: * function (relativePath, file) {...} info@54: * It takes 2 arguments : the relative path and the file. info@54: * @return {Array} An array of matching elements. info@54: */ info@54: filter : function (search) { info@54: var result = [], filename, relativePath, file, fileClone; info@54: for (filename in this.files) { info@54: if ( !this.files.hasOwnProperty(filename) ) { continue; } info@54: file = this.files[filename]; info@54: // return a new object, don't let the user mess with our internal objects :) info@54: fileClone = new ZipObject(file.name, file._data, extend(file.options)); info@54: relativePath = filename.slice(this.root.length, filename.length); info@54: if (filename.slice(0, this.root.length) === this.root && // the file is in the current root info@54: search(relativePath, fileClone)) { // and the file matches the function info@54: result.push(fileClone); info@54: } info@54: } info@54: return result; info@54: }, info@54: info@54: /** info@54: * Add a file to the zip file, or search a file. info@54: * @param {string|RegExp} name The name of the file to add (if data is defined), info@54: * the name of the file to find (if no data) or a regex to match files. info@54: * @param {String|ArrayBuffer|Uint8Array|Buffer} data The file data, either raw or base64 encoded info@54: * @param {Object} o File options info@54: * @return {JSZip|Object|Array} this JSZip object (when adding a file), info@54: * a file (when searching by string) or an array of files (when searching by regex). info@54: */ info@54: file : function(name, data, o) { info@54: if (arguments.length === 1) { info@54: if (JSZip.utils.isRegExp(name)) { info@54: var regexp = name; info@54: return this.filter(function(relativePath, file) { info@54: return !file.options.dir && regexp.test(relativePath); info@54: }); info@54: } else { // text info@54: return this.filter(function (relativePath, file) { info@54: return !file.options.dir && relativePath === name; info@54: })[0]||null; info@54: } info@54: } else { // more than one argument : we have data ! info@54: name = this.root+name; info@54: fileAdd.call(this, name, data, o); info@54: } info@54: return this; info@54: }, info@54: info@54: /** info@54: * Add a directory to the zip file, or search. info@54: * @param {String|RegExp} arg The name of the directory to add, or a regex to search folders. info@54: * @return {JSZip} an object with the new directory as the root, or an array containing matching folders. info@54: */ info@54: folder : function(arg) { info@54: if (!arg) { info@54: return this; info@54: } info@54: info@54: if (JSZip.utils.isRegExp(arg)) { info@54: return this.filter(function(relativePath, file) { info@54: return file.options.dir && arg.test(relativePath); info@54: }); info@54: } info@54: info@54: // else, name is a new folder info@54: var name = this.root + arg; info@54: var newFolder = folderAdd.call(this, name); info@54: info@54: // Allow chaining by returning a new object with this folder as the root info@54: var ret = this.clone(); info@54: ret.root = newFolder.name; info@54: return ret; info@54: }, info@54: info@54: /** info@54: * Delete a file, or a directory and all sub-files, from the zip info@54: * @param {string} name the name of the file to delete info@54: * @return {JSZip} this JSZip object info@54: */ info@54: remove : function(name) { info@54: name = this.root + name; info@54: var file = this.files[name]; info@54: if (!file) { info@54: // Look for any folders info@54: if (name.slice(-1) != "/") { info@54: name += "/"; info@54: } info@54: file = this.files[name]; info@54: } info@54: info@54: if (file) { info@54: if (!file.options.dir) { info@54: // file info@54: delete this.files[name]; info@54: } else { info@54: // folder info@54: var kids = this.filter(function (relativePath, file) { info@54: return file.name.slice(0, name.length) === name; info@54: }); info@54: for (var i = 0; i < kids.length; i++) { info@54: delete this.files[kids[i].name]; info@54: } info@54: } info@54: } info@54: info@54: return this; info@54: }, info@54: info@54: /** info@54: * Generate the complete zip file info@54: * @param {Object} options the options to generate the zip file : info@54: * - base64, (deprecated, use type instead) true to generate base64. info@54: * - compression, "STORE" by default. info@54: * - type, "base64" by default. Values are : string, base64, uint8array, arraybuffer, blob. info@54: * @return {String|Uint8Array|ArrayBuffer|Buffer|Blob} the zip file info@54: */ info@54: generate : function(options) { info@54: options = extend(options || {}, { info@54: base64 : true, info@54: compression : "STORE", info@54: type : "base64" info@54: }); info@54: info@54: JSZip.utils.checkSupport(options.type); info@54: info@54: var zipData = [], localDirLength = 0, centralDirLength = 0, writer, i; info@54: info@54: info@54: // first, generate all the zip parts. info@54: for (var name in this.files) { info@54: if ( !this.files.hasOwnProperty(name) ) { continue; } info@54: var file = this.files[name]; info@54: info@54: var compressionName = file.options.compression || options.compression.toUpperCase(); info@54: var compression = JSZip.compressions[compressionName]; info@54: if (!compression) { info@54: throw new Error(compressionName + " is not a valid compression method !"); info@54: } info@54: info@54: var compressedObject = generateCompressedObjectFrom.call(this, file, compression); info@54: info@54: var zipPart = generateZipParts.call(this, name, file, compressedObject, localDirLength); info@54: localDirLength += zipPart.fileRecord.length + compressedObject.compressedSize; info@54: centralDirLength += zipPart.dirRecord.length; info@54: zipData.push(zipPart); info@54: } info@54: info@54: var dirEnd = ""; info@54: info@54: // end of central dir signature info@54: dirEnd = JSZip.signature.CENTRAL_DIRECTORY_END + info@54: // number of this disk info@54: "\x00\x00" + info@54: // number of the disk with the start of the central directory info@54: "\x00\x00" + info@54: // total number of entries in the central directory on this disk info@54: decToHex(zipData.length, 2) + info@54: // total number of entries in the central directory info@54: decToHex(zipData.length, 2) + info@54: // size of the central directory 4 bytes info@54: decToHex(centralDirLength, 4) + info@54: // offset of start of central directory with respect to the starting disk number info@54: decToHex(localDirLength, 4) + info@54: // .ZIP file comment length info@54: "\x00\x00"; info@54: info@54: info@54: // we have all the parts (and the total length) info@54: // time to create a writer ! info@54: switch(options.type.toLowerCase()) { info@54: case "uint8array" : info@54: case "arraybuffer" : info@54: case "blob" : info@54: case "nodebuffer" : info@54: writer = new Uint8ArrayWriter(localDirLength + centralDirLength + dirEnd.length); info@54: break; info@54: // case "base64" : info@54: // case "string" : info@54: default : info@54: writer = new StringWriter(localDirLength + centralDirLength + dirEnd.length); info@54: break; info@54: } info@54: info@54: for (i = 0; i < zipData.length; i++) { info@54: writer.append(zipData[i].fileRecord); info@54: writer.append(zipData[i].compressedObject.compressedContent); info@54: } info@54: for (i = 0; i < zipData.length; i++) { info@54: writer.append(zipData[i].dirRecord); info@54: } info@54: info@54: writer.append(dirEnd); info@54: info@54: var zip = writer.finalize(); info@54: info@54: info@54: info@54: switch(options.type.toLowerCase()) { info@54: // case "zip is an Uint8Array" info@54: case "uint8array" : info@54: case "arraybuffer" : info@54: case "nodebuffer" : info@54: return JSZip.utils.transformTo(options.type.toLowerCase(), zip); info@54: case "blob" : info@54: return JSZip.utils.arrayBuffer2Blob(JSZip.utils.transformTo("arraybuffer", zip)); info@54: info@54: // case "zip is a string" info@54: case "base64" : info@54: return (options.base64) ? JSZip.base64.encode(zip) : zip; info@54: default : // case "string" : info@54: return zip; info@54: } info@54: }, info@54: info@54: /** info@54: * info@54: * Javascript crc32 info@54: * http://www.webtoolkit.info/ info@54: * info@54: */ info@54: crc32 : function crc32(input, crc) { info@54: if (typeof input === "undefined" || !input.length) { info@54: return 0; info@54: } info@54: info@54: var isArray = JSZip.utils.getTypeOf(input) !== "string"; info@54: info@54: var table = [ info@54: 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, info@54: 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, info@54: 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, info@54: 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, info@54: 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, info@54: 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, info@54: 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, info@54: 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, info@54: 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, info@54: 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, info@54: 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, info@54: 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, info@54: 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, info@54: 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, info@54: 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, info@54: 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, info@54: 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, info@54: 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, info@54: 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, info@54: 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, info@54: 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, info@54: 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, info@54: 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, info@54: 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, info@54: 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, info@54: 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, info@54: 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, info@54: 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, info@54: 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, info@54: 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, info@54: 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, info@54: 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, info@54: 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, info@54: 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, info@54: 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, info@54: 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, info@54: 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, info@54: 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, info@54: 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, info@54: 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, info@54: 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, info@54: 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, info@54: 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, info@54: 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, info@54: 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, info@54: 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, info@54: 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, info@54: 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, info@54: 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, info@54: 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, info@54: 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, info@54: 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, info@54: 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, info@54: 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, info@54: 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, info@54: 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, info@54: 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, info@54: 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, info@54: 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, info@54: 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, info@54: 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, info@54: 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, info@54: 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, info@54: 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D info@54: ]; info@54: info@54: if (typeof(crc) == "undefined") { crc = 0; } info@54: var x = 0; info@54: var y = 0; info@54: var byte = 0; info@54: info@54: crc = crc ^ (-1); info@54: for( var i = 0, iTop = input.length; i < iTop; i++ ) { info@54: byte = isArray ? input[i] : input.charCodeAt(i); info@54: y = ( crc ^ byte ) & 0xFF; info@54: x = table[y]; info@54: crc = ( crc >>> 8 ) ^ x; info@54: } info@54: info@54: return crc ^ (-1); info@54: }, info@54: info@54: // Inspired by http://my.opera.com/GreyWyvern/blog/show.dml/1725165 info@54: clone : function() { info@54: var newObj = new JSZip(); info@54: for (var i in this) { info@54: if (typeof this[i] !== "function") { info@54: newObj[i] = this[i]; info@54: } info@54: } info@54: return newObj; info@54: }, info@54: info@54: info@54: /** info@54: * http://www.webtoolkit.info/javascript-utf8.html info@54: */ info@54: utf8encode : function (string) { info@54: // TextEncoder + Uint8Array to binary string is faster than checking every bytes on long strings. info@54: // http://jsperf.com/utf8encode-vs-textencoder info@54: // On short strings (file names for example), the TextEncoder API is (currently) slower. info@54: if (textEncoder) { info@54: var u8 = textEncoder.encode(string); info@54: return JSZip.utils.transformTo("string", u8); info@54: } info@54: if (JSZip.support.nodebuffer) { info@54: return JSZip.utils.transformTo("string", new Buffer(string, "utf-8")); info@54: } info@54: info@54: // array.join may be slower than string concatenation but generates less objects (less time spent garbage collecting). info@54: // See also http://jsperf.com/array-direct-assignment-vs-push/31 info@54: var result = [], resIndex = 0; info@54: info@54: for (var n = 0; n < string.length; n++) { info@54: info@54: var c = string.charCodeAt(n); info@54: info@54: if (c < 128) { info@54: result[resIndex++] = String.fromCharCode(c); info@54: } else if ((c > 127) && (c < 2048)) { info@54: result[resIndex++] = String.fromCharCode((c >> 6) | 192); info@54: result[resIndex++] = String.fromCharCode((c & 63) | 128); info@54: } else { info@54: result[resIndex++] = String.fromCharCode((c >> 12) | 224); info@54: result[resIndex++] = String.fromCharCode(((c >> 6) & 63) | 128); info@54: result[resIndex++] = String.fromCharCode((c & 63) | 128); info@54: } info@54: info@54: } info@54: info@54: return result.join(""); info@54: }, info@54: info@54: /** info@54: * http://www.webtoolkit.info/javascript-utf8.html info@54: */ info@54: utf8decode : function (input) { info@54: var result = [], resIndex = 0; info@54: var type = JSZip.utils.getTypeOf(input); info@54: var isArray = type !== "string"; info@54: var i = 0; info@54: var c = 0, c1 = 0, c2 = 0, c3 = 0; info@54: info@54: // check if we can use the TextDecoder API info@54: // see http://encoding.spec.whatwg.org/#api info@54: if (textDecoder) { info@54: return textDecoder.decode( info@54: JSZip.utils.transformTo("uint8array", input) info@54: ); info@54: } info@54: if (JSZip.support.nodebuffer) { info@54: return JSZip.utils.transformTo("nodebuffer", input).toString("utf-8"); info@54: } info@54: info@54: while ( i < input.length ) { info@54: info@54: c = isArray ? input[i] : input.charCodeAt(i); info@54: info@54: if (c < 128) { info@54: result[resIndex++] = String.fromCharCode(c); info@54: i++; info@54: } else if ((c > 191) && (c < 224)) { info@54: c2 = isArray ? input[i+1] : input.charCodeAt(i+1); info@54: result[resIndex++] = String.fromCharCode(((c & 31) << 6) | (c2 & 63)); info@54: i += 2; info@54: } else { info@54: c2 = isArray ? input[i+1] : input.charCodeAt(i+1); info@54: c3 = isArray ? input[i+2] : input.charCodeAt(i+2); info@54: result[resIndex++] = String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); info@54: i += 3; info@54: } info@54: info@54: } info@54: info@54: return result.join(""); info@54: } info@54: }; info@54: }()); info@54: info@54: /* info@54: * Compression methods info@54: * This object is filled in as follow : info@54: * name : { info@54: * magic // the 2 bytes indentifying the compression method info@54: * compress // function, take the uncompressed content and return it compressed. info@54: * uncompress // function, take the compressed content and return it uncompressed. info@54: * compressInputType // string, the type accepted by the compress method. null to accept everything. info@54: * uncompressInputType // string, the type accepted by the uncompress method. null to accept everything. info@54: * } info@54: * info@54: * STORE is the default compression method, so it's included in this file. info@54: * Other methods should go to separated files : the user wants modularity. info@54: */ info@54: JSZip.compressions = { info@54: "STORE" : { info@54: magic : "\x00\x00", info@54: compress : function (content) { info@54: return content; // no compression info@54: }, info@54: uncompress : function (content) { info@54: return content; // no compression info@54: }, info@54: compressInputType : null, info@54: uncompressInputType : null info@54: } info@54: }; info@54: info@54: (function () { info@54: JSZip.utils = { info@54: /** info@54: * Convert a string to a "binary string" : a string containing only char codes between 0 and 255. info@54: * @param {string} str the string to transform. info@54: * @return {String} the binary string. info@54: */ info@54: string2binary : function (str) { info@54: var result = ""; info@54: for (var i = 0; i < str.length; i++) { info@54: result += String.fromCharCode(str.charCodeAt(i) & 0xff); info@54: } info@54: return result; info@54: }, info@54: /** info@54: * Create a Uint8Array from the string. info@54: * @param {string} str the string to transform. info@54: * @return {Uint8Array} the typed array. info@54: * @throws {Error} an Error if the browser doesn't support the requested feature. info@54: * @deprecated : use JSZip.utils.transformTo instead. info@54: */ info@54: string2Uint8Array : function (str) { info@54: return JSZip.utils.transformTo("uint8array", str); info@54: }, info@54: info@54: /** info@54: * Create a string from the Uint8Array. info@54: * @param {Uint8Array} array the array to transform. info@54: * @return {string} the string. info@54: * @throws {Error} an Error if the browser doesn't support the requested feature. info@54: * @deprecated : use JSZip.utils.transformTo instead. info@54: */ info@54: uint8Array2String : function (array) { info@54: return JSZip.utils.transformTo("string", array); info@54: }, info@54: /** info@54: * Create a blob from the given ArrayBuffer. info@54: * @param {ArrayBuffer} buffer the buffer to transform. info@54: * @return {Blob} the result. info@54: * @throws {Error} an Error if the browser doesn't support the requested feature. info@54: */ info@54: arrayBuffer2Blob : function (buffer) { info@54: JSZip.utils.checkSupport("blob"); info@54: info@54: try { info@54: // Blob constructor info@54: return new Blob([buffer], { type: "application/zip" }); info@54: } info@54: catch(e) {} info@54: info@54: try { info@54: // deprecated, browser only, old way info@54: var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; info@54: var builder = new BlobBuilder(); info@54: builder.append(buffer); info@54: return builder.getBlob('application/zip'); info@54: } info@54: catch(e) {} info@54: info@54: // well, fuck ?! info@54: throw new Error("Bug : can't construct the Blob."); info@54: }, info@54: /** info@54: * Create a blob from the given string. info@54: * @param {string} str the string to transform. info@54: * @return {Blob} the result. info@54: * @throws {Error} an Error if the browser doesn't support the requested feature. info@54: */ info@54: string2Blob : function (str) { info@54: var buffer = JSZip.utils.transformTo("arraybuffer", str); info@54: return JSZip.utils.arrayBuffer2Blob(buffer); info@54: } info@54: }; info@54: info@54: /** info@54: * The identity function. info@54: * @param {Object} input the input. info@54: * @return {Object} the same input. info@54: */ info@54: function identity(input) { info@54: return input; info@54: } info@54: info@54: /** info@54: * Fill in an array with a string. info@54: * @param {String} str the string to use. info@54: * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to fill in (will be mutated). info@54: * @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated array. info@54: */ info@54: function stringToArrayLike(str, array) { info@54: for (var i = 0; i < str.length; ++i) { info@54: array[i] = str.charCodeAt(i) & 0xFF; info@54: } info@54: return array; info@54: } info@54: info@54: /** info@54: * Transform an array-like object to a string. info@54: * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform. info@54: * @return {String} the result. info@54: */ info@54: function arrayLikeToString(array) { info@54: // Performances notes : info@54: // -------------------- info@54: // String.fromCharCode.apply(null, array) is the fastest, see info@54: // see http://jsperf.com/converting-a-uint8array-to-a-string/2 info@54: // but the stack is limited (and we can get huge arrays !). info@54: // info@54: // result += String.fromCharCode(array[i]); generate too many strings ! info@54: // info@54: // This code is inspired by http://jsperf.com/arraybuffer-to-string-apply-performance/2 info@54: var chunk = 65536; info@54: var result = [], len = array.length, type = JSZip.utils.getTypeOf(array), k = 0; info@54: info@54: var canUseApply = true; info@54: try { info@54: switch(type) { info@54: case "uint8array": info@54: String.fromCharCode.apply(null, new Uint8Array(0)); info@54: break; info@54: case "nodebuffer": info@54: String.fromCharCode.apply(null, new Buffer(0)); info@54: break; info@54: } info@54: } catch(e) { info@54: canUseApply = false; info@54: } info@54: info@54: // no apply : slow and painful algorithm info@54: // default browser on android 4.* info@54: if (!canUseApply) { info@54: var resultStr = ""; info@54: for(var i = 0; i < array.length;i++) { info@54: resultStr += String.fromCharCode(array[i]); info@54: } info@54: return resultStr; info@54: } info@54: info@54: while (k < len && chunk > 1) { info@54: try { info@54: if (type === "array" || type === "nodebuffer") { info@54: result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len)))); info@54: } else { info@54: result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len)))); info@54: } info@54: k += chunk; info@54: } catch (e) { info@54: chunk = Math.floor(chunk / 2); info@54: } info@54: } info@54: return result.join(""); info@54: } info@54: info@54: /** info@54: * Copy the data from an array-like to an other array-like. info@54: * @param {Array|ArrayBuffer|Uint8Array|Buffer} arrayFrom the origin array. info@54: * @param {Array|ArrayBuffer|Uint8Array|Buffer} arrayTo the destination array which will be mutated. info@54: * @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated destination array. info@54: */ info@54: function arrayLikeToArrayLike(arrayFrom, arrayTo) { info@54: for(var i = 0; i < arrayFrom.length; i++) { info@54: arrayTo[i] = arrayFrom[i]; info@54: } info@54: return arrayTo; info@54: } info@54: info@54: // a matrix containing functions to transform everything into everything. info@54: var transform = {}; info@54: info@54: // string to ? info@54: transform["string"] = { info@54: "string" : identity, info@54: "array" : function (input) { info@54: return stringToArrayLike(input, new Array(input.length)); info@54: }, info@54: "arraybuffer" : function (input) { info@54: return transform["string"]["uint8array"](input).buffer; info@54: }, info@54: "uint8array" : function (input) { info@54: return stringToArrayLike(input, new Uint8Array(input.length)); info@54: }, info@54: "nodebuffer" : function (input) { info@54: return stringToArrayLike(input, new Buffer(input.length)); info@54: } info@54: }; info@54: info@54: // array to ? info@54: transform["array"] = { info@54: "string" : arrayLikeToString, info@54: "array" : identity, info@54: "arraybuffer" : function (input) { info@54: return (new Uint8Array(input)).buffer; info@54: }, info@54: "uint8array" : function (input) { info@54: return new Uint8Array(input); info@54: }, info@54: "nodebuffer" : function (input) { info@54: return new Buffer(input); info@54: } info@54: }; info@54: info@54: // arraybuffer to ? info@54: transform["arraybuffer"] = { info@54: "string" : function (input) { info@54: return arrayLikeToString(new Uint8Array(input)); info@54: }, info@54: "array" : function (input) { info@54: return arrayLikeToArrayLike(new Uint8Array(input), new Array(input.byteLength)); info@54: }, info@54: "arraybuffer" : identity, info@54: "uint8array" : function (input) { info@54: return new Uint8Array(input); info@54: }, info@54: "nodebuffer" : function (input) { info@54: return new Buffer(new Uint8Array(input)); info@54: } info@54: }; info@54: info@54: // uint8array to ? info@54: transform["uint8array"] = { info@54: "string" : arrayLikeToString, info@54: "array" : function (input) { info@54: return arrayLikeToArrayLike(input, new Array(input.length)); info@54: }, info@54: "arraybuffer" : function (input) { info@54: return input.buffer; info@54: }, info@54: "uint8array" : identity, info@54: "nodebuffer" : function(input) { info@54: return new Buffer(input); info@54: } info@54: }; info@54: info@54: // nodebuffer to ? info@54: transform["nodebuffer"] = { info@54: "string" : arrayLikeToString, info@54: "array" : function (input) { info@54: return arrayLikeToArrayLike(input, new Array(input.length)); info@54: }, info@54: "arraybuffer" : function (input) { info@54: return transform["nodebuffer"]["uint8array"](input).buffer; info@54: }, info@54: "uint8array" : function (input) { info@54: return arrayLikeToArrayLike(input, new Uint8Array(input.length)); info@54: }, info@54: "nodebuffer" : identity info@54: }; info@54: info@54: /** info@54: * Transform an input into any type. info@54: * The supported output type are : string, array, uint8array, arraybuffer, nodebuffer. info@54: * If no output type is specified, the unmodified input will be returned. info@54: * @param {String} outputType the output type. info@54: * @param {String|Array|ArrayBuffer|Uint8Array|Buffer} input the input to convert. info@54: * @throws {Error} an Error if the browser doesn't support the requested output type. info@54: */ info@54: JSZip.utils.transformTo = function (outputType, input) { info@54: if (!input) { info@54: // undefined, null, etc info@54: // an empty string won't harm. info@54: input = ""; info@54: } info@54: if (!outputType) { info@54: return input; info@54: } info@54: JSZip.utils.checkSupport(outputType); info@54: var inputType = JSZip.utils.getTypeOf(input); info@54: var result = transform[inputType][outputType](input); info@54: return result; info@54: }; info@54: info@54: /** info@54: * Return the type of the input. info@54: * The type will be in a format valid for JSZip.utils.transformTo : string, array, uint8array, arraybuffer. info@54: * @param {Object} input the input to identify. info@54: * @return {String} the (lowercase) type of the input. info@54: */ info@54: JSZip.utils.getTypeOf = function (input) { info@54: if (typeof input === "string") { info@54: return "string"; info@54: } info@54: if (Object.prototype.toString.call(input) === "[object Array]") { info@54: return "array"; info@54: } info@54: if (JSZip.support.nodebuffer && Buffer.isBuffer(input)) { info@54: return "nodebuffer"; info@54: } info@54: if (JSZip.support.uint8array && input instanceof Uint8Array) { info@54: return "uint8array"; info@54: } info@54: if (JSZip.support.arraybuffer && input instanceof ArrayBuffer) { info@54: return "arraybuffer"; info@54: } info@54: }; info@54: info@54: /** info@54: * Cross-window, cross-Node-context regular expression detection info@54: * @param {Object} object Anything info@54: * @return {Boolean} true if the object is a regular expression, info@54: * false otherwise info@54: */ info@54: JSZip.utils.isRegExp = function (object) { info@54: return Object.prototype.toString.call(object) === "[object RegExp]"; info@54: }; info@54: info@54: /** info@54: * Throw an exception if the type is not supported. info@54: * @param {String} type the type to check. info@54: * @throws {Error} an Error if the browser doesn't support the requested type. info@54: */ info@54: JSZip.utils.checkSupport = function (type) { info@54: var supported = true; info@54: switch (type.toLowerCase()) { info@54: case "uint8array": info@54: supported = JSZip.support.uint8array; info@54: break; info@54: case "arraybuffer": info@54: supported = JSZip.support.arraybuffer; info@54: break; info@54: case "nodebuffer": info@54: supported = JSZip.support.nodebuffer; info@54: break; info@54: case "blob": info@54: supported = JSZip.support.blob; info@54: break; info@54: } info@54: if (!supported) { info@54: throw new Error(type + " is not supported by this browser"); info@54: } info@54: }; info@54: info@54: info@54: })(); info@54: info@54: (function (){ info@54: /** info@54: * Represents an entry in the zip. info@54: * The content may or may not be compressed. info@54: * @constructor info@54: */ info@54: JSZip.CompressedObject = function () { info@54: this.compressedSize = 0; info@54: this.uncompressedSize = 0; info@54: this.crc32 = 0; info@54: this.compressionMethod = null; info@54: this.compressedContent = null; info@54: }; info@54: info@54: JSZip.CompressedObject.prototype = { info@54: /** info@54: * Return the decompressed content in an unspecified format. info@54: * The format will depend on the decompressor. info@54: * @return {Object} the decompressed content. info@54: */ info@54: getContent : function () { info@54: return null; // see implementation info@54: }, info@54: /** info@54: * Return the compressed content in an unspecified format. info@54: * The format will depend on the compressed conten source. info@54: * @return {Object} the compressed content. info@54: */ info@54: getCompressedContent : function () { info@54: return null; // see implementation info@54: } info@54: }; info@54: })(); info@54: info@54: /** info@54: * info@54: * Base64 encode / decode info@54: * http://www.webtoolkit.info/ info@54: * info@54: * Hacked so that it doesn't utf8 en/decode everything info@54: **/ info@54: JSZip.base64 = (function() { info@54: // private property info@54: var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; info@54: info@54: return { info@54: // public method for encoding info@54: encode : function(input, utf8) { info@54: var output = ""; info@54: var chr1, chr2, chr3, enc1, enc2, enc3, enc4; info@54: var i = 0; info@54: info@54: while (i < input.length) { info@54: info@54: chr1 = input.charCodeAt(i++); info@54: chr2 = input.charCodeAt(i++); info@54: chr3 = input.charCodeAt(i++); info@54: info@54: enc1 = chr1 >> 2; info@54: enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); info@54: enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); info@54: enc4 = chr3 & 63; info@54: info@54: if (isNaN(chr2)) { info@54: enc3 = enc4 = 64; info@54: } else if (isNaN(chr3)) { info@54: enc4 = 64; info@54: } info@54: info@54: output = output + info@54: _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + info@54: _keyStr.charAt(enc3) + _keyStr.charAt(enc4); info@54: info@54: } info@54: info@54: return output; info@54: }, info@54: info@54: // public method for decoding info@54: decode : function(input, utf8) { info@54: var output = ""; info@54: var chr1, chr2, chr3; info@54: var enc1, enc2, enc3, enc4; info@54: var i = 0; info@54: info@54: input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); info@54: info@54: while (i < input.length) { info@54: info@54: enc1 = _keyStr.indexOf(input.charAt(i++)); info@54: enc2 = _keyStr.indexOf(input.charAt(i++)); info@54: enc3 = _keyStr.indexOf(input.charAt(i++)); info@54: enc4 = _keyStr.indexOf(input.charAt(i++)); info@54: info@54: chr1 = (enc1 << 2) | (enc2 >> 4); info@54: chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); info@54: chr3 = ((enc3 & 3) << 6) | enc4; info@54: info@54: output = output + String.fromCharCode(chr1); info@54: info@54: if (enc3 != 64) { info@54: output = output + String.fromCharCode(chr2); info@54: } info@54: if (enc4 != 64) { info@54: output = output + String.fromCharCode(chr3); info@54: } info@54: info@54: } info@54: info@54: return output; info@54: info@54: } info@54: }; info@54: }()); info@54: info@54: // enforcing Stuk's coding style info@54: // vim: set shiftwidth=3 softtabstop=3: