Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions jszip.js
Original file line number Diff line number Diff line change
Expand Up @@ -1120,12 +1120,36 @@ JSZip.support = {
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;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, what's broken with apply?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some versions of the default android browser (4.x, tested in the emulator) doesn't accept ArrayBuffer or Uint8Array for apply, only arrays : I get a TypeError: Function.prototype.apply: Arguments list has wrong type.


// 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) {
Expand Down
10 changes: 5 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ var _actualTestFileDataGetters = {
if (JSZip.support.arraybuffer) {
var buffer = opts.zip.file("file.txt").asArrayBuffer();
ok(buffer instanceof ArrayBuffer, opts.name + " : the result is a instance of ArrayBuffer");
var actual = String.fromCharCode.apply(null, new Uint8Array(buffer));
var actual = JSZip.utils.transformTo("string", buffer);
equal(actual, opts.rawData, opts.name + " : asArrayBuffer()");
} else {
try {
Expand All @@ -461,7 +461,7 @@ var _actualTestFileDataGetters = {
if (JSZip.support.uint8array) {
var bufferView = opts.zip.file("file.txt").asUint8Array();
ok(bufferView instanceof Uint8Array, opts.name + " : the result is a instance of Uint8Array");
var actual = String.fromCharCode.apply(null, bufferView);
var actual = JSZip.utils.transformTo("string", bufferView);
equal(actual, opts.rawData, opts.name + " : asUint8Array()");
} else {
try {
Expand All @@ -476,7 +476,7 @@ var _actualTestFileDataGetters = {
if (JSZip.support.nodebuffer) {
var buffer = opts.zip.file("file.txt").asNodeBuffer();
ok(buffer instanceof Buffer, opts.name + " : the result is a instance of Buffer");
var actual = String.fromCharCode.apply(null, buffer);
var actual = JSZip.utils.transformTo("string", buffer);
equal(actual, opts.rawData, opts.name + " : .asNodeBuffer()");
} else {
try {
Expand Down Expand Up @@ -665,7 +665,7 @@ if (JSZip.support.uint8array) {
ok(array instanceof Uint8Array, "The result is a instance of Uint8Array");
equal(array.length, expected.length);

var actual = String.fromCharCode.apply(null, array);
var actual = JSZip.utils.transformTo("string", array);

ok(similar(actual, expected, 18) , "Generated ZIP matches reference ZIP");
});
Expand All @@ -689,7 +689,7 @@ if (JSZip.support.arraybuffer) {
var buffer = zip.generate({type:"arraybuffer"});
ok(buffer instanceof ArrayBuffer, "The result is a instance of ArrayBuffer");

var actual = String.fromCharCode.apply(null, new Uint8Array(buffer));
var actual = JSZip.utils.transformTo("string", buffer);

ok(similar(actual, expected, 18) , "Generated ZIP matches reference ZIP");
});
Expand Down