compression: null
};
+/*
+ * List features that require a modern browser, and if the current browser support them.
+ */
+JSZip.support = {
+ // contains true if JSZip can read/generate ArrayBuffer, false otherwise.
+ arraybuffer : (function(){
+ return typeof ArrayBuffer !== "undefined" && typeof Uint8Array !== "undefined";
+ })(),
+ // contains true if JSZip can read/generate nodejs Buffer, false otherwise.
+ nodebuffer : (function(){
+ return typeof Buffer !== "undefined";
+ })(),
+ // contains true if JSZip can read/generate Uint8Array, false otherwise.
+ uint8array : (function(){
+ return typeof Uint8Array !== "undefined";
+ })(),
+ // contains true if JSZip can read/generate Blob, false otherwise.
+ blob : (function(){
+ // the spec started with BlobBuilder then replaced it with a construtor for Blob.
+ // Result : we have browsers that :
+ // * know the BlobBuilder (but with prefix)
+ // * know the Blob constructor
+ // * know about Blob but not about how to build them
+ // About the "=== 0" test : if given the wrong type, it may be converted to a string.
+ // Instead of an empty content, we will get "[object Uint8Array]" for example.
+ if (typeof ArrayBuffer === "undefined") {
+ return false;
+ }
+ var buffer = new ArrayBuffer(0);
+ try {
+ return new Blob([buffer], { type: "application/zip" }).size === 0;
+ }
+ catch(e) {}
+
+ try {
+ var builder = new (window.BlobBuilder || window.WebKitBlobBuilder ||
+ window.MozBlobBuilder || window.MSBlobBuilder)();
+ builder.append(buffer);
+ return builder.getBlob('application/zip').size === 0;
+ }
+ catch(e) {}
+
+ return false;
+ })()
+};
JSZip.prototype = (function () {
+ var textEncoder, textDecoder;
+ if (
+ JSZip.support.uint8array &&
+ typeof TextEncoder === "function" &&
+ typeof TextDecoder === "function"
+ ) {
+ textEncoder = new TextEncoder("utf-8");
+ textDecoder = new TextDecoder("utf-8");
+ }
/**
* Returns the raw data of a ZipObject, decompress the content if necessary.
if (!file.options.binary) {
// unicode text !
// unicode string => binary string is a painful process, check if we can avoid it.
- if (JSZip.support.uint8array && typeof TextEncoder === "function") {
- return TextEncoder("utf-8").encode(result);
+ if (textEncoder) {
+ return textEncoder.encode(result);
}
if (JSZip.support.nodebuffer) {
return new Buffer(result, "utf-8");
*/
file : function(name, data, o) {
if (arguments.length === 1) {
- if (name instanceof RegExp) {
+ if (JSZip.utils.isRegExp(name)) {
var regexp = name;
return this.filter(function(relativePath, file) {
return !file.options.dir && regexp.test(relativePath);
return this;
}
- if (arg instanceof RegExp) {
+ if (JSZip.utils.isRegExp(arg)) {
return this.filter(function(relativePath, file) {
return file.options.dir && arg.test(relativePath);
});
if ( !this.files.hasOwnProperty(name) ) { continue; }
var file = this.files[name];
- var compressionName = file.compression || options.compression.toUpperCase();
+ var compressionName = file.options.compression || options.compression.toUpperCase();
var compression = JSZip.compressions[compressionName];
if (!compression) {
throw new Error(compressionName + " is not a valid compression method !");
// TextEncoder + Uint8Array to binary string is faster than checking every bytes on long strings.
// http://jsperf.com/utf8encode-vs-textencoder
// On short strings (file names for example), the TextEncoder API is (currently) slower.
- if (JSZip.support.uint8array && typeof TextEncoder === "function") {
- var u8 = TextEncoder("utf-8").encode(string);
+ if (textEncoder) {
+ var u8 = textEncoder.encode(string);
return JSZip.utils.transformTo("string", u8);
}
if (JSZip.support.nodebuffer) {
// check if we can use the TextDecoder API
// see http://encoding.spec.whatwg.org/#api
- if (JSZip.support.uint8array && typeof TextDecoder === "function") {
- return TextDecoder("utf-8").decode(
+ if (textDecoder) {
+ return textDecoder.decode(
JSZip.utils.transformTo("uint8array", input)
);
}
}
};
-/*
- * List features that require a modern browser, and if the current browser support them.
- */
-JSZip.support = {
- // contains true if JSZip can read/generate ArrayBuffer, false otherwise.
- arraybuffer : (function(){
- return typeof ArrayBuffer !== "undefined" && typeof Uint8Array !== "undefined";
- })(),
- // contains true if JSZip can read/generate nodejs Buffer, false otherwise.
- nodebuffer : (function(){
- return typeof Buffer !== "undefined";
- })(),
- // contains true if JSZip can read/generate Uint8Array, false otherwise.
- uint8array : (function(){
- return typeof Uint8Array !== "undefined";
- })(),
- // contains true if JSZip can read/generate Blob, false otherwise.
- blob : (function(){
- // the spec started with BlobBuilder then replaced it with a construtor for Blob.
- // Result : we have browsers that :
- // * know the BlobBuilder (but with prefix)
- // * know the Blob constructor
- // * know about Blob but not about how to build them
- // About the "=== 0" test : if given the wrong type, it may be converted to a string.
- // Instead of an empty content, we will get "[object Uint8Array]" for example.
- if (typeof ArrayBuffer === "undefined") {
- return false;
- }
- var buffer = new ArrayBuffer(0);
- try {
- return new Blob([buffer], { type: "application/zip" }).size === 0;
- }
- catch(e) {}
-
- try {
- var builder = new (window.BlobBuilder || window.WebKitBlobBuilder ||
- window.MozBlobBuilder || window.MSBlobBuilder)();
- builder.append(buffer);
- return builder.getBlob('application/zip').size === 0;
- }
- catch(e) {}
-
- return false;
- })()
-};
-
(function () {
JSZip.utils = {
/**
var chunk = 65536;
var result = [], len = array.length, type = JSZip.utils.getTypeOf(array), k = 0;
+ var canUseApply = true;
+ try {
+ switch(type) {
+ case "uint8array":
+ String.fromCharCode.apply(null, new Uint8Array(0));
+ break;
+ case "nodebuffer":
+ String.fromCharCode.apply(null, new Buffer(0));
+ break;
+ }
+ } catch(e) {
+ canUseApply = false;
+ }
+
+ // no apply : slow and painful algorithm
+ // default browser on android 4.*
+ if (!canUseApply) {
+ var resultStr = "";
+ for(var i = 0; i < array.length;i++) {
+ resultStr += String.fromCharCode(array[i]);
+ }
+ return resultStr;
+ }
+
while (k < len && chunk > 1) {
try {
if (type === "array" || type === "nodebuffer") {
- result.push(String.fromCharCode.apply(null, array.slice(k, Math.max(k + chunk, len))));
+ result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len))));
} else {
- result.push(String.fromCharCode.apply(null, array.subarray(k, k + chunk)));
+ result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len))));
}
k += chunk;
} catch (e) {
if (typeof input === "string") {
return "string";
}
- if (input instanceof Array) {
+ if (Object.prototype.toString.call(input) === "[object Array]") {
return "array";
}
if (JSZip.support.nodebuffer && Buffer.isBuffer(input)) {
}
};
+ /**
+ * Cross-window, cross-Node-context regular expression detection
+ * @param {Object} object Anything
+ * @return {Boolean} true if the object is a regular expression,
+ * false otherwise
+ */
+ JSZip.utils.isRegExp = function (object) {
+ return Object.prototype.toString.call(object) === "[object RegExp]";
+ };
+
/**
* Throw an exception if the type is not supported.
* @param {String} type the type to check.
}());
// enforcing Stuk's coding style
-// vim: set shiftwidth=3 softtabstop=3:
\ No newline at end of file
+// vim: set shiftwidth=3 softtabstop=3: