Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update coding style for brackets

See pull request 28 :
Change
if (test)
{
   doStuff();
}
to
if (test) {
   doStuff();
}

Also remove an extra (unused) `this` and fix a typo.
  • Loading branch information...
commit 98bb58fb9fcb6cb5970fc487a2485c2443b0981b 1 parent e9cfea9
@dduponchel dduponchel authored
View
39 index.html
@@ -6,8 +6,7 @@
<meta name="description" content="Create .zip files using Javascript. Provides a simple API to place any content generated by Javascript into a .zip file for your users." />
<link rel="stylesheet" href="960.css" type="text/css" media="all" />
<style type="text/css">
-body
-{
+body {
font-family: Helvetica, Deja Vu Sans, sans-serif;
font-size: 90%;
}
@@ -16,8 +15,7 @@
a:hover {text-decoration: none;}
-ul, ol
-{
+ul, ol {
padding-left: 20px;
}
@@ -30,30 +28,25 @@
h3 { margin-bottom: 0.2em; }
p, ul { margin: 0.5em 0; }
-h4
-{
+h4 {
border-bottom: 1px solid #CCCCCC;
}
-#header
-{
+#header {
text-align: center;
}
-.border
-{
+.border {
border-right: 1px solid #CCCCCC;
margin-right: 0;
padding-right: 9px;
}
-table
-{
+table {
margin-right: 2em;
}
-th, td
-{
+th, td {
padding: 2px 5px;
text-align: center;
border-right: 1px solid #CCC;
@@ -69,26 +62,22 @@
.cr {border-color: #39B642;}
.op {border-color: #C42122;}
-hr
-{
+hr {
clear: both;
border: none;
border-top: 1px solid #CCCCCC;
}
-dt
-{
+dt {
/*font-weight:bold;*/
text-decoration:underline;
}
-#where p
-{
+#where p {
text-align: center;
}
-#where .download
-{
+#where .download {
background: #28AA31;
color: #FFFFFF;
height: 2em;
@@ -98,8 +87,7 @@
border-radius: 0.25em;
}
-pre.example
-{
+pre.example {
background: #E8E8E8;
margin-left: -10px;
padding: 10px;
@@ -475,8 +463,7 @@ <h4 id="doc_generate_options">generate(options)</h4>
<pre class="example">
content = zip.generate({type:"string"});
-for (var c = 0; c &lt; content.length; c++)
-{
+for (var c = 0; c &lt; content.length; c++) {
console.log(content.charCodeAt(c));
// do other things
}</pre>
View
6 jszip-deflate.js
@@ -8,8 +8,7 @@
* Original code can be found here: http://www.onicos.com/staff/iz/amuse/javascript/expert/deflate.txt
*/
-if(!JSZip)
-{
+if(!JSZip) {
throw "JSZip not defined";
}
@@ -1693,3 +1692,6 @@ if(!JSZip.compressions["DEFLATE"]) {
}
})();
+
+// enforcing Stuk's coding style
+// vim: set shiftwidth=3 softtabstop=3:
View
6 jszip-inflate.js
@@ -8,8 +8,7 @@
* Original code can be found here: http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt
*/
-if(!JSZip)
-{
+if(!JSZip) {
throw "JSZip not defined";
}
@@ -776,3 +775,6 @@ if(!JSZip.compressions["DEFLATE"]) {
}
})();
+
+// enforcing Stuk's coding style
+// vim: set shiftwidth=3 softtabstop=3:
View
183 jszip-load.js
@@ -17,8 +17,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
*/
var pretty = function (str) {
var res = '', code, i;
- for (i = 0; i < (str||"").length; i++)
- {
+ for (i = 0; i < (str||"").length; i++) {
code = str.charCodeAt(i);
res += '\\x' + (code < 16 ? "0" : "") + code.toString(16).toUpperCase();
}
@@ -31,11 +30,9 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* @return {Object|null} the JSZip compression object, null if none found.
*/
var findCompression = function (compressionMethod) {
- for (var method in JSZip.compressions)
- {
+ for (var method in JSZip.compressions) {
if( !JSZip.compressions.hasOwnProperty(method) ) { continue; }
- if (JSZip.compressions[method].magic === compressionMethod)
- {
+ if (JSZip.compressions[method].magic === compressionMethod) {
return JSZip.compressions[method];
}
}
@@ -50,17 +47,12 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
*/
function StreamReader(stream) {
this.stream = "";
- if (JSZip.support.uint8array && stream instanceof Uint8Array)
- {
+ if (JSZip.support.uint8array && stream instanceof Uint8Array) {
this.stream = JSZip.utils.uint8Array2String(stream);
- }
- else if (JSZip.support.arraybuffer && stream instanceof ArrayBuffer)
- {
+ } else if (JSZip.support.arraybuffer && stream instanceof ArrayBuffer) {
var bufferView = new Uint8Array(stream);
this.stream = JSZip.utils.uint8Array2String(bufferView);
- }
- else
- {
+ } else {
this.stream = JSZip.utils.string2binary(stream);
}
this.index = 0;
@@ -71,8 +63,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* @param {string} offset the additional offset to check.
* @throws {Error} an Error if the offset is out of bounds.
*/
- checkOffset : function (offset)
- {
+ checkOffset : function (offset) {
this.checkIndex(this.index + offset);
},
/**
@@ -80,10 +71,8 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* @param {string} newIndex the index to check.
* @throws {Error} an Error if the index is out of bounds.
*/
- checkIndex : function (newIndex)
- {
- if (this.stream.length < newIndex || newIndex < 0)
- {
+ checkIndex : function (newIndex) {
+ if (this.stream.length < newIndex || newIndex < 0) {
throw new Error("End of stream reached (stream length = " +
this.stream.length + ", asked index = " +
(newIndex) + "). Corrupted zip ?");
@@ -94,8 +83,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* @param {number} newIndex The new index.
* @throws {Error} if the new index is out of the stream.
*/
- setIndex : function (newIndex)
- {
+ setIndex : function (newIndex) {
this.checkIndex(newIndex);
this.index = newIndex;
},
@@ -103,8 +91,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* Check if the end of the file has been reached.
* @return {boolean} true if it is the case, false otherwise.
*/
- eof : function ()
- {
+ eof : function () {
return this.index >= this.stream.length;
},
/**
@@ -112,8 +99,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* @param {number} i the index to use.
* @return {number} a byte.
*/
- byteAt : function(i)
- {
+ byteAt : function(i) {
return this.stream.charCodeAt(i);
},
/**
@@ -121,12 +107,10 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* @param {number} size the number of bytes to read.
* @return {number} the corresponding number.
*/
- readInt : function (size)
- {
+ readInt : function (size) {
var result = 0, i;
this.checkOffset(size);
- for(i = this.index + size - 1; i >= this.index; i--)
- {
+ for(i = this.index + size - 1; i >= this.index; i--) {
result = (result << 8) + this.byteAt(i);
}
this.index += size;
@@ -137,8 +121,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* @param {number} size the number of bytes to read.
* @return {string} the corresponding string.
*/
- readString : function (size)
- {
+ readString : function (size) {
this.checkOffset(size);
// this will work because the constructor applied the "& 0xff" mask.
var result = this.stream.slice(this.index, this.index + size);
@@ -149,8 +132,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* Get the next date.
* @return {Date} the date.
*/
- readDate : function ()
- {
+ readDate : function () {
var dostime = this.readInt(4);
return new Date(
((dostime >> 25) & 0x7f) + 1980, // year
@@ -179,8 +161,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* say if the file is encrypted.
* @return {boolean} true if the file is encrypted, false otherwise.
*/
- isEncrypted : function ()
- {
+ isEncrypted : function () {
// bit 1 is set
return (this.bitFlag & 0x0001) === 0x0001;
},
@@ -188,8 +169,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* say if the file has a data decriptor.
* @return {boolean} true if the file has a data descriptor, false otherwise.
*/
- hasDataDescriptor : function ()
- {
+ hasDataDescriptor : function () {
// bit 3 is set
return (this.bitFlag & 0x0008) === 0x0008;
},
@@ -197,8 +177,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* say if the file has utf-8 filename/comment.
* @return {boolean} true if the filename/comment is in utf-8, false otherwise.
*/
- useUTF8 : function ()
- {
+ useUTF8 : function () {
// bit 11 is set
return (this.bitFlag & 0x0800) === 0x0800;
},
@@ -206,16 +185,14 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* say if the file is a zip64 file.
* @return {boolean} true if the file is zip64, false otherwise.
*/
- isZIP64 : function ()
- {
+ isZIP64 : function () {
return this.options.zip64;
},
/**
* Read the local part header of a zip file and add the info in this object.
* @param {StreamReader} reader the reader to use.
*/
- readLocalPartHeader : function(reader)
- {
+ readLocalPartHeader : function(reader) {
// the signature has already been consumed
this.versionNeeded = reader.readInt(2);
this.bitFlag = reader.readInt(2);
@@ -227,8 +204,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
this.fileNameLength = reader.readInt(2);
this.extraFieldsLength = reader.readInt(2);
- if (this.isEncrypted())
- {
+ if (this.isEncrypted()) {
throw new Error("Encrypted zip are not supported");
}
},
@@ -236,8 +212,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* Read the local part of a zip file and add the info in this object.
* @param {StreamReader} reader the reader to use.
*/
- readLocalPart : function(reader)
- {
+ readLocalPart : function(reader) {
var compression;
this.readLocalPartHeader(reader);
@@ -245,41 +220,34 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
this.fileName = reader.readString(this.fileNameLength);
this.readExtraFields(reader);
- if (!this.hasDataDescriptor())
- {
+ if (!this.hasDataDescriptor()) {
// easy : we know the file length
this.compressedFileData = reader.readString(this.compressedSize);
- }
- else
- {
+ } else {
// hard way : find the data descriptor manually
this.compressedFileData = this.findDataUntilDataDescriptor(reader);
this.crc32 = reader.readInt(4);
this.compressedSize = reader.readInt(this.isZIP64() ? 8 : 4);
this.uncompressedSize = reader.readInt(this.isZIP64() ? 8 : 4);
- if (this.compressedFileData.length !== this.compressedSize)
- {
+ if (this.compressedFileData.length !== this.compressedSize) {
throw new Error("Bug : data descriptor incorrectly read (size mismatch)");
}
}
this.uncompressedFileData = null;
compression = findCompression(this.compressionMethod);
- if (compression === null) // no compression found
- {
+ if (compression === null) { // no compression found
throw new Error("Corrupted zip : compression " + pretty(this.compressionMethod) +
" unknown (inner file : " + this.fileName + ")");
}
this.uncompressedFileData = compression.uncompress(this.compressedFileData);
- if (this.loadOptions.checkCRC32 && JSZip.prototype.crc32(this.uncompressedFileData) !== this.crc32)
- {
+ if (this.loadOptions.checkCRC32 && JSZip.prototype.crc32(this.uncompressedFileData) !== this.crc32) {
throw new Error("Corrupted zip : CRC32 mismatch");
}
- if (this.useUTF8())
- {
+ if (this.useUTF8()) {
this.fileName = JSZip.prototype.utf8decode(this.fileName);
}
},
@@ -288,14 +256,12 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* Read data until a data descriptor signature is found.
* @param {StreamReader} reader the reader to use.
*/
- findDataUntilDataDescriptor : function(reader)
- {
+ findDataUntilDataDescriptor : function(reader) {
var data = "",
buffer = reader.readString(4),
aByte;
- while(buffer !== JSZip.signature.DATA_DESCRIPTOR)
- {
+ while(buffer !== JSZip.signature.DATA_DESCRIPTOR) {
aByte = reader.readString(1);
data += buffer.slice(0, 1);
buffer = (buffer + aByte).slice(-4);
@@ -306,8 +272,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* Read the central part of a zip file and add the info in this object.
* @param {StreamReader} reader the reader to use.
*/
- readCentralPart : function(reader)
- {
+ readCentralPart : function(reader) {
this.versionMadeBy = reader.readString(2);
this.readLocalPartHeader(reader);
@@ -321,8 +286,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
this.fileName = reader.readString(this.fileNameLength);
this.readExtraFields(reader);
this.fileComment = reader.readString(this.fileCommentLength);
- if (this.useUTF8())
- {
+ if (this.useUTF8()) {
this.fileName = JSZip.prototype.utf8decode(this.fileName);
this.fileComment = JSZip.prototype.utf8decode(this.fileComment);
}
@@ -334,24 +298,19 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* Parse the ZIP64 extra field and merge the info in the current ZipEntry.
* @param {StreamReader} reader the reader to use.
*/
- parseZIP64ExtraField : function(reader)
- {
+ parseZIP64ExtraField : function(reader) {
// should be something, preparing the extra reader
var extraReader = new StreamReader(this.extraFields[0x0001].value);
- if(this.uncompressedSize === -1)
- {
+ if(this.uncompressedSize === -1) {
this.uncompressedSize = extraReader.readInt(8);
}
- if(this.compressedSize === -1)
- {
+ if(this.compressedSize === -1) {
this.compressedSize = extraReader.readInt(8);
}
- if(this.localHeaderOffset === -1)
- {
+ if(this.localHeaderOffset === -1) {
this.localHeaderOffset = extraReader.readInt(8);
}
- if(this.diskNumberStart === -1)
- {
+ if(this.diskNumberStart === -1) {
this.diskNumberStart = extraReader.readInt(4);
}
},
@@ -359,8 +318,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* Read the central part of a zip file and add the info in this object.
* @param {StreamReader} reader the reader to use.
*/
- readExtraFields : function(reader)
- {
+ readExtraFields : function(reader) {
var start = reader.index,
extraFieldId,
extraFieldLength,
@@ -368,8 +326,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
this.extraFields = this.extraFields || {};
- while (reader.index < start + this.extraFieldsLength)
- {
+ while (reader.index < start + this.extraFieldsLength) {
extraFieldId = reader.readInt(2);
extraFieldLength = reader.readInt(2);
extraFieldValue = reader.readString(extraFieldLength);
@@ -381,8 +338,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
};
}
- if(this.isZIP64() && this.extraFields[0x0001])
- {
+ if(this.isZIP64() && this.extraFields[0x0001]) {
this.parseZIP64ExtraField(reader);
}
}
@@ -409,11 +365,9 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* @param {string} expectedSignature the expected signature.
* @throws {Error} if it is an other signature.
*/
- checkSignature : function(expectedSignature)
- {
+ checkSignature : function(expectedSignature) {
var signature = this.reader.readString(4);
- if (signature !== expectedSignature)
- {
+ if (signature !== expectedSignature) {
throw new Error("Corrupted zip or bug : unexpected signature " +
"(" + pretty(signature) + ", expected " + pretty(expectedSignature) + ")");
}
@@ -421,8 +375,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
/**
* Read the end of the central directory.
*/
- readBlockEndOfCentral : function ()
- {
+ readBlockEndOfCentral : function () {
this.diskNumber = this.reader.readInt(2);
this.diskWithCentralDirStart = this.reader.readInt(2);
this.centralDirRecordsOnThisDisk = this.reader.readInt(2);
@@ -439,8 +392,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* The end of central can coexist with its Zip64 brother,
* I don't want to read the wrong number of bytes !
*/
- readBlockZip64EndOfCentral : function ()
- {
+ readBlockZip64EndOfCentral : function () {
this.zip64EndOfCentralSize = this.reader.readInt(8);
this.versionMadeBy = this.reader.readString(2);
this.versionNeeded = this.reader.readInt(2);
@@ -457,8 +409,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
extraFieldId,
extraFieldLength,
extraFieldValue;
- while(index < extraDataSize)
- {
+ while(index < extraDataSize) {
extraFieldId = this.reader.readInt(2);
extraFieldLength = this.reader.readInt(4);
extraFieldValue = this.reader.readString(extraFieldLength);
@@ -472,24 +423,20 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
/**
* Read the end of the Zip 64 central directory locator.
*/
- readBlockZip64EndOfCentralLocator : function ()
- {
+ readBlockZip64EndOfCentralLocator : function () {
this.diskWithZip64CentralDirStart = this.reader.readInt(4);
this.relativeOffsetEndOfZip64CentralDir = this.reader.readInt(8);
this.disksCount = this.reader.readInt(4);
- if (this.disksCount > 1)
- {
+ if (this.disksCount > 1) {
throw new Error("Multi-volumes zip are not supported");
}
},
/**
* Read the local files, based on the offset read in the central part.
*/
- readLocalFiles : function()
- {
+ readLocalFiles : function() {
var i, file;
- for(i = 0; i < this.files.length; i++)
- {
+ for(i = 0; i < this.files.length; i++) {
file = this.files[i];
this.reader.setIndex(file.localHeaderOffset);
this.checkSignature(JSZip.signature.LOCAL_FILE_HEADER);
@@ -499,13 +446,11 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
/**
* Read the central directory.
*/
- readCentralDir : function()
- {
+ readCentralDir : function() {
var file;
this.reader.setIndex(this.centralDirOffset);
- while(this.reader.readString(4) === JSZip.signature.CENTRAL_FILE_HEADER)
- {
+ while(this.reader.readString(4) === JSZip.signature.CENTRAL_FILE_HEADER) {
file = new ZipEntry({
zip64: this.zip64
}, this.loadOptions);
@@ -516,25 +461,20 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
/**
* Read the end of central directory.
*/
- readEndOfCentral : function()
- {
+ readEndOfCentral : function() {
// zip 64 ?
var offset = this.reader.stream.lastIndexOf(JSZip.signature.ZIP64_CENTRAL_DIRECTORY_LOCATOR);
- if (offset === -1) // nope
- {
+ if (offset === -1) { // nope
this.zip64 = false;
offset = this.reader.stream.lastIndexOf(JSZip.signature.CENTRAL_DIRECTORY_END);
- if (offset === -1)
- {
+ if (offset === -1) {
throw new Error("Corrupted zip : can't find end of central directory");
}
this.reader.setIndex(offset);
this.checkSignature(JSZip.signature.CENTRAL_DIRECTORY_END);
this.readBlockEndOfCentral();
- }
- else // zip 64 !
- {
+ } else { // zip 64 !
this.zip64 = true;
this.reader.setIndex(offset);
this.checkSignature(JSZip.signature.ZIP64_CENTRAL_DIRECTORY_LOCATOR);
@@ -549,8 +489,7 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
* Read a zip file and create ZipEntries.
* @param {String|ArrayBuffer|Uint8Array} data the binary string representing a zip file.
*/
- load : function(data)
- {
+ load : function(data) {
this.reader = new StreamReader(data);
this.readEndOfCentral();
@@ -570,15 +509,13 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
JSZip.prototype.load = function(data, options) {
var files, zipEntries, i, input;
options = options || {};
- if(options.base64)
- {
+ if(options.base64) {
data = JSZipBase64.decode(data);
}
zipEntries = new ZipEntries(data, options);
files = zipEntries.files;
- for (i = 0; i < files.length; i++)
- {
+ for (i = 0; i < files.length; i++) {
input = files[i];
this.file(input.fileName, input.uncompressedFileData, {
binary:true,
@@ -591,6 +528,6 @@ Dual licenced under the MIT license or GPLv3. See LICENSE.markdown.
return this;
};
-}(this));
+}());
// enforcing Stuk's coding style
// vim: set shiftwidth=3 softtabstop=3 foldmethod=marker:
View
210 jszip.js
@@ -34,7 +34,7 @@ var JSZip = function(data, options) {
// Where we are in the hierarchy
this.root = "";
- if(data) {
+ if (data) {
this.load(data, options);
}
};
@@ -57,8 +57,7 @@ JSZip.defaults = {
};
-JSZip.prototype = (function ()
-{
+JSZip.prototype = (function () {
/**
* A simple object representing a file in the zip file.
* @constructor
@@ -77,15 +76,12 @@ JSZip.prototype = (function ()
* Return the content as UTF8 string.
* @return {string} the UTF8 string.
*/
- asText : function ()
- {
+ asText : function () {
var result = this.data;
- if (this.options.base64)
- {
+ if (this.options.base64) {
result = JSZipBase64.decode(result);
}
- if (this.options.binary)
- {
+ if (this.options.binary) {
result = JSZip.prototype.utf8decode(result);
}
return result;
@@ -94,15 +90,12 @@ JSZip.prototype = (function ()
* Returns the binary content.
* @return {string} the content as binary.
*/
- asBinary : function ()
- {
+ asBinary : function () {
var result = this.data;
- if (this.options.base64)
- {
+ if (this.options.base64) {
result = JSZipBase64.decode(result);
}
- if (!this.options.binary)
- {
+ if (!this.options.binary) {
result = JSZip.prototype.utf8encode(result);
}
return result;
@@ -111,16 +104,14 @@ JSZip.prototype = (function ()
* Returns the content as an Uint8Array.
* @return {Uint8Array} the content as an Uint8Array.
*/
- asUint8Array : function ()
- {
+ asUint8Array : function () {
return JSZip.utils.string2Uint8Array(this.asBinary());
},
/**
* Returns the content as an ArrayBuffer.
* @return {ArrayBuffer} the content as an ArrayBufer.
*/
- asArrayBuffer : function ()
- {
+ asArrayBuffer : function () {
return JSZip.utils.string2Uint8Array(this.asBinary()).buffer;
}
};
@@ -134,8 +125,7 @@ JSZip.prototype = (function ()
*/
var decToHex = function(dec, bytes) {
var hex = "", i;
- for(i = 0; i < bytes; i++)
- {
+ for(i = 0; i < bytes; i++) {
hex += String.fromCharCode(dec&0xff);
dec=dec>>>8;
}
@@ -150,12 +140,9 @@ JSZip.prototype = (function ()
*/
var extend = function () {
var result = {}, i, attr;
- for (i = 0; i < arguments.length; i++) // arguments is not enumerable in some browsers
- {
- for (attr in arguments[i])
- {
- if(arguments[i].hasOwnProperty(attr) && typeof result[attr] === "undefined")
- {
+ for (i = 0; i < arguments.length; i++) { // arguments is not enumerable in some browsers
+ for (attr in arguments[i]) {
+ if (arguments[i].hasOwnProperty(attr) && typeof result[attr] === "undefined") {
result[attr] = arguments[i][attr];
}
}
@@ -198,25 +185,19 @@ JSZip.prototype = (function ()
o = prepareFileAttrs(o);
- if (JSZip.support.uint8array && data instanceof Uint8Array)
- {
+ if (JSZip.support.uint8array && data instanceof Uint8Array) {
o.base64 = false;
o.binary = true;
data = JSZip.utils.uint8Array2String(data);
- }
- else if (JSZip.support.arraybuffer && data instanceof ArrayBuffer)
- {
+ } else if (JSZip.support.arraybuffer && data instanceof ArrayBuffer) {
o.base64 = false;
o.binary = true;
var bufferView = new Uint8Array(data);
data = JSZip.utils.uint8Array2String(bufferView);
- }
- else if (o.binary && !o.base64)
- {
+ } else if (o.binary && !o.base64) {
// optimizedBinaryString == true means that the file has already been filtered with a 0xFF mask
- if (o.optimizedBinaryString !== true)
- {
- // this a string, not in a base64 format.
+ if (o.optimizedBinaryString !== true) {
+ // this is a string, not in a base64 format.
// Be sure that this is a correct "binary string"
data = JSZip.utils.string2binary(data);
}
@@ -235,8 +216,7 @@ JSZip.prototype = (function ()
* @return {string} the parent folder, or ""
*/
var parentFolder = function (path) {
- if (path.slice(-1) == '/')
- {
+ if (path.slice(-1) == '/') {
path = path.substring(0, path.length - 1);
}
var lastSlash = path.lastIndexOf('/');
@@ -256,8 +236,7 @@ JSZip.prototype = (function ()
}
// Does this folder already exist?
- if (!this.files[name])
- {
+ if (!this.files[name]) {
// be sure sub folders exist
var parent = parentFolder(name);
if (parent) {
@@ -346,8 +325,7 @@ JSZip.prototype = (function ()
* options.base64 : is the stream in base64 ? default : false
* @return {JSZip} the current JSZip object
*/
- load : function (stream, options)
- {
+ load : function (stream, options) {
throw new Error("Load method is not defined. Is the file jszip-load.js included ?");
},
@@ -358,19 +336,16 @@ JSZip.prototype = (function ()
* It takes 2 arguments : the relative path and the file.
* @return {Array} An array of matching elements.
*/
- filter : function (search)
- {
+ filter : function (search) {
var result = [], filename, relativePath, file, fileClone;
- for (filename in this.files)
- {
- if( !this.files.hasOwnProperty(filename) ) { continue; }
+ for (filename in this.files) {
+ if ( !this.files.hasOwnProperty(filename) ) { continue; }
file = this.files[filename];
// return a new object, don't let the user mess with our internal objects :)
fileClone = new ZipObject(file.name, file.data, extend(file.options));
relativePath = filename.slice(this.root.length, filename.length);
if (filename.slice(0, this.root.length) === this.root && // the file is in the current root
- search(relativePath, fileClone)) // and the file matches the function
- {
+ search(relativePath, fileClone)) { // and the file matches the function
result.push(fileClone);
}
}
@@ -386,26 +361,19 @@ JSZip.prototype = (function ()
* @return {JSZip|Object|Array} this JSZip object (when adding a file),
* a file (when searching by string) or an array of files (when searching by regex).
*/
- file : function(name, data, o)
- {
- if (arguments.length === 1)
- {
- if (name instanceof RegExp)
- {
+ file : function(name, data, o) {
+ if (arguments.length === 1) {
+ if (name instanceof RegExp) {
var regexp = name;
return this.filter(function(relativePath, file) {
return !file.options.dir && regexp.test(relativePath);
});
- }
- else // text
- {
+ } else { // text
return this.filter(function (relativePath, file) {
return !file.options.dir && relativePath === name;
})[0]||null;
}
- }
- else // more than one argument : we have data !
- {
+ } else { // more than one argument : we have data !
name = this.root+name;
fileAdd.call(this, name, data, o);
}
@@ -417,15 +385,12 @@ JSZip.prototype = (function ()
* @param {String|RegExp} arg The name of the directory to add, or a regex to search folders.
* @return {JSZip} an object with the new directory as the root, or an array containing matching folders.
*/
- folder : function(arg)
- {
- if (!arg)
- {
+ folder : function(arg) {
+ if (!arg) {
throw new Error("folder : wrong argument");
}
- if (arg instanceof RegExp)
- {
+ if (arg instanceof RegExp) {
return this.filter(function(relativePath, file) {
return file.options.dir && arg.test(relativePath);
});
@@ -446,12 +411,10 @@ JSZip.prototype = (function ()
* @param {string} name the name of the file to delete
* @return {JSZip} this JSZip object
*/
- remove : function(name)
- {
+ remove : function(name) {
name = this.root + name;
var file = this.files[name];
- if (!file)
- {
+ if (!file) {
// Look for any folders
if (name.slice(-1) != "/") {
name += "/";
@@ -459,21 +422,16 @@ JSZip.prototype = (function ()
file = this.files[name];
}
- if (file)
- {
- if (!file.options.dir)
- {
+ if (file) {
+ if (!file.options.dir) {
// file
delete this.files[name];
- }
- else
- {
+ } else {
// folder
var kids = this.filter(function (relativePath, file) {
return file.name.slice(0, name.length) === name;
});
- for (var i = 0; i < kids.length; i++)
- {
+ for (var i = 0; i < kids.length; i++) {
delete this.files[kids[i].name];
}
}
@@ -490,8 +448,7 @@ JSZip.prototype = (function ()
* - type, "base64" by default. Values are : string, base64, uint8array, arraybuffer, blob.
* @return {String|Uint8Array|ArrayBuffer|Blob} the zip file
*/
- generate : function(options)
- {
+ generate : function(options) {
options = extend(options || {}, {
base64 : true,
compression : "STORE",
@@ -506,9 +463,8 @@ JSZip.prototype = (function ()
throw compression + " is not a valid compression method !";
}
- for (var name in this.files)
- {
- if( !this.files.hasOwnProperty(name) ) { continue; }
+ for (var name in this.files) {
+ if ( !this.files.hasOwnProperty(name) ) { continue; }
var file = this.files[name];
@@ -568,8 +524,7 @@ JSZip.prototype = (function ()
var zip = fileData + dirData + dirEnd;
- switch(options.type.toLowerCase())
- {
+ switch(options.type.toLowerCase()) {
case "uint8array" :
return JSZip.utils.string2Uint8Array(zip);
case "arraybuffer" :
@@ -589,8 +544,7 @@ JSZip.prototype = (function ()
* http://www.webtoolkit.info/
*
*/
- crc32 : function(str, crc)
- {
+ crc32 : function(str, crc) {
if (str === "" || typeof str === "undefined") {
return 0;
@@ -678,13 +632,10 @@ JSZip.prototype = (function ()
},
// Inspired by http://my.opera.com/GreyWyvern/blog/show.dml/1725165
- clone : function()
- {
+ clone : function() {
var newObj = new JSZip();
- for (var i in this)
- {
- if (typeof this[i] !== "function")
- {
+ for (var i in this) {
+ if (typeof this[i] !== "function") {
newObj[i] = this[i];
}
}
@@ -705,12 +656,10 @@ JSZip.prototype = (function ()
if (c < 128) {
utftext += String.fromCharCode(c);
- }
- else if((c > 127) && (c < 2048)) {
+ } else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
- }
- else {
+ } else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
@@ -736,13 +685,11 @@ JSZip.prototype = (function ()
if (c < 128) {
string += String.fromCharCode(c);
i++;
- }
- else if((c > 191) && (c < 224)) {
+ } else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
- }
- else {
+ } else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
@@ -771,12 +718,10 @@ JSZip.prototype = (function ()
JSZip.compressions = {
"STORE" : {
magic : "\x00\x00",
- compress : function (content)
- {
+ compress : function (content) {
return content; // no compression
},
- uncompress : function (content)
- {
+ uncompress : function (content) {
return content; // no compression
}
}
@@ -803,19 +748,16 @@ JSZip.support = {
// * 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")
- {
+ if (typeof ArrayBuffer === "undefined") {
return false;
}
var buffer = new ArrayBuffer(0);
- try
- {
+ try {
return new Blob([buffer], { type: "application/zip" }).size === 0;
}
catch(e) {}
- try
- {
+ try {
var builder = new (window.BlobBuilder || window.WebKitBlobBuilder ||
window.MozBlobBuilder || window.MSBlobBuilder)();
builder.append(buffer);
@@ -833,11 +775,9 @@ JSZip.utils = {
* @param {string} str the string to transform.
* @return {String} the binary string.
*/
- string2binary : function (str)
- {
+ string2binary : function (str) {
var result = "";
- for (var i = 0; i < str.length; i++)
- {
+ for (var i = 0; i < str.length; i++) {
result += String.fromCharCode(str.charCodeAt(i) & 0xff);
}
return result;
@@ -848,16 +788,13 @@ JSZip.utils = {
* @return {Uint8Array} the typed array.
* @throws {Error} an Error if the browser doesn't support the requested feature.
*/
- string2Uint8Array : function (str)
- {
- if (!JSZip.support.uint8array)
- {
+ string2Uint8Array : function (str) {
+ if (!JSZip.support.uint8array) {
throw new Error("Uint8Array is not supported by this browser");
}
var buffer = new ArrayBuffer(str.length);
var bufferView = new Uint8Array(buffer);
- for(var i = 0; i < str.length; i++)
- {
+ for(var i = 0; i < str.length; i++) {
bufferView[i] = str.charCodeAt(i);
}
@@ -870,15 +807,12 @@ JSZip.utils = {
* @return {string} the string.
* @throws {Error} an Error if the browser doesn't support the requested feature.
*/
- uint8Array2String : function (array)
- {
- if (!JSZip.support.uint8array)
- {
+ uint8Array2String : function (array) {
+ if (!JSZip.support.uint8array) {
throw new Error("Uint8Array is not supported by this browser");
}
var result = "";
- for(var i = 0; i < array.length; i++)
- {
+ for(var i = 0; i < array.length; i++) {
result += String.fromCharCode(array[i]);
}
@@ -890,23 +824,19 @@ JSZip.utils = {
* @return {Blob} the string.
* @throws {Error} an Error if the browser doesn't support the requested feature.
*/
- string2Blob : function (str)
- {
- if (!JSZip.support.blob)
- {
+ string2Blob : function (str) {
+ if (!JSZip.support.blob) {
throw new Error("Blob is not supported by this browser");
}
var buffer = JSZip.utils.string2Uint8Array(str).buffer;
- try
- {
+ try {
// Blob constructor
return new Blob([buffer], { type: "application/zip" });
}
catch(e) {}
- try
- {
+ try {
// deprecated, browser only, old way
var builder = new (window.BlobBuilder || window.WebKitBlobBuilder ||
window.MozBlobBuilder || window.MSBlobBuilder)();
@@ -916,7 +846,7 @@ JSZip.utils = {
catch(e) {}
// well, fuck ?!
- throw new Error("Bug : can't construct the Blob.")
+ throw new Error("Bug : can't construct the Blob.");
}
};
View
51 test/index.html
@@ -89,13 +89,11 @@
// Be sure to have a well formatted string
expected = JSZip.utils.string2binary(expected);
- if (actual.length !== expected.length)
- {
+ if (actual.length !== expected.length) {
mistakes -= Math.abs((actual.length||0) - (expected.length||0));
}
- for (var i = 0; i < Math.min(actual.length, expected.length); i++)
- {
+ for (var i = 0; i < Math.min(actual.length, expected.length); i++) {
if (actual.charAt(i) !== expected.charAt(i)) {
mistakes--;
}
@@ -118,13 +116,10 @@
var refZips = [];
function testZipFile(testName, zipName, testFunction) {
- test(testName, function ()
- {
+ test(testName, function () {
if (refZips[zipName]) {
testFunction.call(this, refZips[zipName]);
- }
- else
- {
+ } else {
stop();
/*
* Here is the tricky part : getting the data.
@@ -150,8 +145,7 @@
})
.done(function(data, textStatus, jqXHR){
start();
- try
- {
+ try {
// this.xhr comes from the context
var file = getBinaryFromXHR(jqXHR.responseText, this.xhr);
refZips[zipName] = file;
@@ -742,13 +736,10 @@
// zip -0 -X -e encrypted.zip Hello.txt
testZipFile("basic encryption", "ref/encrypted.zip", function(file) {
- try
- {
+ try {
var zip = new JSZip(file);
ok(false, "Encryption is not supported, but no exception were thrown");
- }
- catch(e)
- {
+ } catch(e) {
equal(e.message, "Encrypted zip are not supported", "the error message is useful");
}
});
@@ -757,49 +748,37 @@
module("Load file, corrupted zip"); // {{{
testZipFile("bad compression method", "ref/invalid/compression.zip", function(file) {
- try
- {
+ try {
var zip = new JSZip(file);
ok(false, "no exception were thrown");
- }
- catch(e)
- {
+ } catch(e) {
ok(e.message.match("Corrupted zip"), "the error message is useful");
}
});
testZipFile("invalid crc32 but no check", "ref/invalid/crc32.zip", function(file) {
- try
- {
+ try {
var zip = new JSZip(file, {checkCRC32:false});
ok(true, "no exception were thrown");
- }
- catch(e)
- {
+ } catch(e) {
ok(false, "An exception were thrown but the check should have been disabled.");
}
});
testZipFile("invalid crc32", "ref/invalid/crc32.zip", function(file) {
- try
- {
+ try {
var zip = new JSZip(file, {checkCRC32:true});
ok(false, "no exception were thrown");
- }
- catch(e)
- {
+ } catch(e) {
ok(e.message.match("Corrupted zip"), "the error message is useful");
}
});
testZipFile("bad offset", "ref/invalid/bad_offset.zip", function(file) {
- try
- {
+ try {
var zip = new JSZip(file);
ok(false, "no exception were thrown");
- }
- catch(e)
- {
+ } catch(e) {
ok(e.message.match("Corrupted zip"), "the error message is useful");
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.