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

Nodejs support and isue #54 #57

Merged
merged 2 commits into from
Jul 7, 2013
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
7 changes: 5 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ <h4 id="doc_file_name">file(name)</h4>
<li><code>asBinary()</code>, string, the content as binary (utf8 decoded if necessary).</li>
<li><code>asArrayBuffer()</code>, ArrayBuffer, need a <a href="#jszip_support">compatible browser</a>.</li>
<li><code>asUint8Array()</code>, Uint8Array, need a <a href="#jszip_support">compatible browser</a>.</li>
<li><code>asNodeBuffer()</code>, nodejs Buffer, need <a href="#jszip_support">nodejs</a>.</li>
</ul>
</dd>
</dl>
Expand Down Expand Up @@ -345,7 +346,7 @@ <h4 id="doc_file_name_data_options">file(name, data [,options])</h4>
<dd>Add a file to the zip file.</dd>
<dt>Parameters : </dt>
<dd><code>name</code> (String) the name of the file.</dd>
<dd><code>data</code> (String/ArrayBuffer/Uint8Array) the content of the file.</dd>
<dd><code>data</code> (String/ArrayBuffer/Uint8Array/Buffer) the content of the file.</dd>
<dd><code>options</code> (Object) the options :
<ul>
<li><code>base64</code> (boolean) set to <code>true</code> if the data
Expand Down Expand Up @@ -452,6 +453,7 @@ <h4 id="doc_generate_options">generate(options)</h4>
<li><code>uint8array</code> : the result will be a Uint8Array containing the zip. This requires a compatible browser.</li>
<li><code>arraybuffer</code> : the result will be a ArrayBuffer containing the zip. This requires a compatible browser.</li>
<li><code>blob</code> : the result will be a Blob containing the zip. This requires a compatible browser.</li>
<li><code>nodebuffer</code> : the result will be a nodejs Buffer containing the zip. This requires nodejs.</li>
</ul>
</li>
</ul>
Expand Down Expand Up @@ -484,7 +486,7 @@ <h4 id="doc_load_data_options">load(data, options)</h4>
<dt>Description : </dt>
<dd>Read an existing zip and merge the data in the current JSZip object. The implementation is in jszip-load.js, don't forget to include it. This technique has some limitations, see <a href="#zip_load_limits">below</a>.</dd>
<dt>Parameters : </dt>
<dd><code>data</code> (String/ArrayBuffer/Uint8Array) the zip file</dd>
<dd><code>data</code> (String/ArrayBuffer/Uint8Array/Buffer) the zip file</dd>
<dd><code>options</code> (Object) the options to load the zip file :
<ul>
<li><code>base64</code> (boolean) <code>true</code> if the data is base64 encoded, <code>false</code> for binary. Default : <code>false</code>.</li>
Expand Down Expand Up @@ -546,6 +548,7 @@ <h4 id="jszip_support">JSZip.support</h4>
<li><code>arraybuffer</code> : true if JSZip can read and generate ArrayBuffer, false otherwise.</li>
<li><code>uint8array</code> : true if JSZip can read and generate Uint8Array, false otherwise.</li>
<li><code>blob</code> : true if JSZip can read and generate Blob, false otherwise.</li>
<li><code>nodebuffer</code> : true if JSZip can read and generate nodejs Buffer, false otherwise.</li>
</ul>
</p>

Expand Down
40 changes: 33 additions & 7 deletions jszip-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* Developer tip : when debugging, a watch on pretty(this.reader.data.slice(this.reader.index))
* is very useful :)
* @constructor
* @param {String|ArrayBuffer|Uint8Array} data the data to read.
* @param {String|ArrayBuffer|Uint8Array|Buffer} data the data to read.
*/
function DataReader(data) {
this.data = null; // type : see implementation
Expand Down Expand Up @@ -203,9 +203,11 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* @param {Uint8Array} data the data to read.
*/
function Uint8ArrayReader(data) {
this.data = data;
this.length = this.data.length;
this.index = 0;
if (data) {
this.data = data;
this.length = this.data.length;
this.index = 0;
}
}
Uint8ArrayReader.prototype = new DataReader();
/**
Expand Down Expand Up @@ -239,6 +241,28 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
this.index += size;
return result;
};

/**
* Read bytes from a Buffer.
* @constructor
* @param {Buffer} data the data to read.
*/
function NodeBufferReader(data) {
this.data = data;
this.length = this.data.length;
this.index = 0;
}
NodeBufferReader.prototype = new Uint8ArrayReader();

/**
* @see DataReader.readData
*/
NodeBufferReader.prototype.readData = function (size) {
this.checkOffset(size);
var result = this.data.slice(this.index, this.index + size);
this.index += size;
return result;
};
// }}} end of DataReader

// class ZipEntry {{{
Expand Down Expand Up @@ -465,7 +489,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
/**
* All the entries in the zip file.
* @constructor
* @param {String|ArrayBuffer|Uint8Array} data the binary data to load.
* @param {String|ArrayBuffer|Uint8Array|Buffer} data the binary data to load.
* @param {Object} loadOptions Options for loading the data.
*/
function ZipEntries(data, loadOptions) {
Expand Down Expand Up @@ -635,13 +659,15 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
var type = JSZip.utils.getTypeOf(data);
if (type === "string" && !JSZip.support.uint8array) {
this.reader = new StringReader(data, this.loadOptions.optimizedBinaryString);
} else if (type === "nodebuffer") {
this.reader = new NodeBufferReader(data);
} else {
this.reader = new Uint8ArrayReader(JSZip.utils.transformTo("uint8array", data));
}
},
/**
* Read a zip file and create ZipEntries.
* @param {String|ArrayBuffer|Uint8Array} data the binary string representing a zip file.
* @param {String|ArrayBuffer|Uint8Array|Buffer} data the binary string representing a zip file.
*/
load : function(data) {
this.prepareReader(data);
Expand All @@ -655,7 +681,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
/**
* Implementation of the load method of JSZip.
* It uses the above classes to decode a zip file, and load every files.
* @param {String|ArrayBuffer|Uint8Array} data the data to load.
* @param {String|ArrayBuffer|Uint8Array|Buffer} data the data to load.
* @param {Object} options Options for loading the data.
* options.base64 : is the data in base64 ? default : false
*/
Expand Down
Loading