Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of Array in JSZip#load. #252

Merged
merged 1 commit into from
Mar 9, 2016
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
2 changes: 1 addition & 1 deletion documentation/api_jszip/load.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ __Arguments__

name | type | description
-------------------|--------|------------
data | String/ArrayBuffer/Uint8Array/Buffer | the zip file
data | String/Array of bytes/ArrayBuffer/Uint8Array/Buffer | the zip file
options | object | the options to load the zip file

Content of `options` :
Expand Down
50 changes: 50 additions & 0 deletions lib/arrayReader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';
var DataReader = require('./dataReader');

function ArrayReader(data) {
if (data) {
this.data = data;
this.length = this.data.length;
this.index = 0;

for(var i = 0; i < this.data.length; i++) {
data[i] = data[i] & 0xFF;
}
}
}
ArrayReader.prototype = new DataReader();
/**
* @see DataReader.byteAt
*/
ArrayReader.prototype.byteAt = function(i) {
return this.data[i];
};
/**
* @see DataReader.lastIndexOfSignature
*/
ArrayReader.prototype.lastIndexOfSignature = function(sig) {
var sig0 = sig.charCodeAt(0),
sig1 = sig.charCodeAt(1),
sig2 = sig.charCodeAt(2),
sig3 = sig.charCodeAt(3);
for (var i = this.length - 4; i >= 0; --i) {
if (this.data[i] === sig0 && this.data[i + 1] === sig1 && this.data[i + 2] === sig2 && this.data[i + 3] === sig3) {
return i;
}
}

return -1;
};
/**
* @see DataReader.readData
*/
ArrayReader.prototype.readData = function(size) {
this.checkOffset(size);
if(size === 0) {
return [];
}
var result = this.data.slice(this.index, this.index + size);
this.index += size;
return result;
};
module.exports = ArrayReader;
26 changes: 2 additions & 24 deletions lib/uint8ArrayReader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var DataReader = require('./dataReader');
var ArrayReader = require('./arrayReader');

function Uint8ArrayReader(data) {
if (data) {
Expand All @@ -8,29 +8,7 @@ function Uint8ArrayReader(data) {
this.index = 0;
}
}
Uint8ArrayReader.prototype = new DataReader();
/**
* @see DataReader.byteAt
*/
Uint8ArrayReader.prototype.byteAt = function(i) {
return this.data[i];
};
/**
* @see DataReader.lastIndexOfSignature
*/
Uint8ArrayReader.prototype.lastIndexOfSignature = function(sig) {
var sig0 = sig.charCodeAt(0),
sig1 = sig.charCodeAt(1),
sig2 = sig.charCodeAt(2),
sig3 = sig.charCodeAt(3);
for (var i = this.length - 4; i >= 0; --i) {
if (this.data[i] === sig0 && this.data[i + 1] === sig1 && this.data[i + 2] === sig2 && this.data[i + 3] === sig3) {
return i;
}
}

return -1;
};
Uint8ArrayReader.prototype = new ArrayReader();
/**
* @see DataReader.readData
*/
Expand Down
8 changes: 7 additions & 1 deletion lib/zipEntries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var StringReader = require('./stringReader');
var NodeBufferReader = require('./nodeBufferReader');
var Uint8ArrayReader = require('./uint8ArrayReader');
var ArrayReader = require('./arrayReader');
var utils = require('./utils');
var sig = require('./signature');
var ZipEntry = require('./zipEntry');
Expand Down Expand Up @@ -196,14 +197,19 @@ ZipEntries.prototype = {
},
prepareReader: function(data) {
var type = utils.getTypeOf(data);
utils.checkSupport(type);
if (type === "string" && !support.uint8array) {
this.reader = new StringReader(data, this.loadOptions.optimizedBinaryString);
}
else if (type === "nodebuffer") {
this.reader = new NodeBufferReader(data);
}
else {
else if (support.uint8array) {
this.reader = new Uint8ArrayReader(utils.transformTo("uint8array", data));
} else if (support.array) {
this.reader = new ArrayReader(utils.transformTo("array", data));
} else {
throw new Error("Unexpected error: unsupported type '" + type + "'");
}
},
/**
Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,26 @@ testZipFile("load(string) handles bytes > 255", "ref/all.zip", function(file) {
equal(zip.file("Hello.txt").asText(), "Hello World\n", "the zip was correctly read.");
});

testZipFile("load(Array) works", "ref/deflate.zip", function(file) {
var updatedFile = new Array(file.length);
for( var i = 0; i < file.length; ++i ) {
updatedFile[i] = file.charCodeAt(i) + 0x4200;
}
var zip = new JSZip(updatedFile);

equal(zip.file("Hello.txt").asText(), "This a looong file : we need to see the difference between the different compression methods.\n", "the zip was correctly read.");
});

testZipFile("load(array) handles bytes > 255", "ref/deflate.zip", function(file) {
var updatedFile = new Array(file.length);
for( var i = 0; i < file.length; ++i ) {
updatedFile[i] = file.charCodeAt(i) + 0x4200;
}
var zip = new JSZip(updatedFile);

equal(zip.file("Hello.txt").asText(), "This a looong file : we need to see the difference between the different compression methods.\n", "the zip was correctly read.");
});

if (JSZip.support.arraybuffer) {
testZipFile("load(ArrayBuffer) works", "ref/all.zip", function(fileAsString) {
var file = new ArrayBuffer(fileAsString.length);
Expand Down