1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00

Cleared out libs folder, use npm install or bower install instead. Cleaned examples. Fixed small issues in core.

This commit is contained in:
Fred Chasen 2015-09-16 13:05:08 -04:00
parent 1a782d0bd4
commit 2b6b7594c4
153 changed files with 2497 additions and 47756 deletions

View file

@ -115,7 +115,7 @@ module.exports = CompressedObject;
'use strict';
exports.STORE = {
magic: "\x00\x00",
compress: function(content) {
compress: function(content, compressionOptions) {
return content; // no compression
},
uncompress: function(content) {
@ -347,7 +347,10 @@ exports.dir = false;
exports.createFolders = false;
exports.date = null;
exports.compression = null;
exports.compressionOptions = null;
exports.comment = null;
exports.unixPermissions = null;
exports.dosPermissions = null;
},{}],7:[function(_dereq_,module,exports){
'use strict';
@ -465,8 +468,10 @@ exports.uncompressInputType = USE_TYPEDARRAY ? "uint8array" : "array";
exports.compressInputType = USE_TYPEDARRAY ? "uint8array" : "array";
exports.magic = "\x08\x00";
exports.compress = function(input) {
return pako.deflateRaw(input);
exports.compress = function(input, compressionOptions) {
return pako.deflateRaw(input, {
level : compressionOptions.level || -1 // default compression
});
};
exports.uncompress = function(input) {
return pako.inflateRaw(input);
@ -574,6 +579,8 @@ module.exports = function(data, options) {
date: input.date,
dir: input.dir,
comment : input.fileComment.length ? input.fileComment : null,
unixPermissions : input.unixPermissions,
dosPermissions : input.dosPermissions,
createFolders: options.createFolders
});
}
@ -593,6 +600,7 @@ module.exports = function(data, encoding){
module.exports.test = function(b){
return Buffer.isBuffer(b);
};
}).call(this,(typeof Buffer !== "undefined" ? Buffer : undefined))
},{}],12:[function(_dereq_,module,exports){
'use strict';
@ -718,6 +726,8 @@ var ZipObject = function(name, data, options) {
this.dir = options.dir;
this.date = options.date;
this.comment = options.comment;
this.unixPermissions = options.unixPermissions;
this.dosPermissions = options.dosPermissions;
this._data = data;
this.options = options;
@ -842,6 +852,23 @@ var fileAdd = function(name, data, o) {
o = prepareFileAttrs(o);
if (typeof o.unixPermissions === "string") {
o.unixPermissions = parseInt(o.unixPermissions, 8);
}
// UNX_IFDIR 0040000 see zipinfo.c
if (o.unixPermissions && (o.unixPermissions & 0x4000)) {
o.dir = true;
}
// Bit 4 Directory
if (o.dosPermissions && (o.dosPermissions & 0x0010)) {
o.dir = true;
}
if (o.dir) {
name = forceTrailingSlash(name);
}
if (o.createFolders && (parent = parentFolder(name))) {
folderAdd.call(this, parent, true);
}
@ -850,6 +877,7 @@ var fileAdd = function(name, data, o) {
o.base64 = false;
o.binary = false;
data = null;
dataType = null;
}
else if (dataType === "string") {
if (o.binary && !o.base64) {
@ -894,6 +922,20 @@ var parentFolder = function (path) {
return (lastSlash > 0) ? path.substring(0, lastSlash) : "";
};
/**
* Returns the path with a slash at the end.
* @private
* @param {String} path the path to check.
* @return {String} the path with a trailing slash.
*/
var forceTrailingSlash = function(path) {
// Check the name ends with a /
if (path.slice(-1) != "/") {
path += "/"; // IE doesn't like substr(-1)
}
return path;
};
/**
* Add a (sub) folder in the current folder.
* @private
@ -903,13 +945,10 @@ var parentFolder = function (path) {
* @return {Object} the new folder.
*/
var folderAdd = function(name, createFolders) {
// Check the name ends with a /
if (name.slice(-1) != "/") {
name += "/"; // IE doesn't like substr(-1)
}
createFolders = (typeof createFolders !== 'undefined') ? createFolders : false;
name = forceTrailingSlash(name);
// Does this folder already exist?
if (!this.files[name]) {
fileAdd.call(this, name, null, {
@ -924,9 +963,10 @@ var folderAdd = function(name, createFolders) {
* Generate a JSZip.CompressedObject for a given zipOject.
* @param {ZipObject} file the object to read.
* @param {JSZip.compression} compression the compression to use.
* @param {Object} compressionOptions the options to use when compressing.
* @return {JSZip.CompressedObject} the compressed result.
*/
var generateCompressedObjectFrom = function(file, compression) {
var generateCompressedObjectFrom = function(file, compression, compressionOptions) {
var result = new CompressedObject(),
content;
@ -946,7 +986,7 @@ var generateCompressedObjectFrom = function(file, compression) {
else {
content = file._data.getContent();
// need to decompress / recompress
result.compressedContent = compression.compress(utils.transformTo(compression.compressInputType, content));
result.compressedContent = compression.compress(utils.transformTo(compression.compressInputType, content), compressionOptions);
}
}
else {
@ -958,7 +998,7 @@ var generateCompressedObjectFrom = function(file, compression) {
}
result.uncompressedSize = content.length;
result.crc32 = crc32(content);
result.compressedContent = compression.compress(utils.transformTo(compression.compressInputType, content));
result.compressedContent = compression.compress(utils.transformTo(compression.compressInputType, content), compressionOptions);
}
result.compressedSize = result.compressedContent.length;
@ -967,15 +1007,67 @@ var generateCompressedObjectFrom = function(file, compression) {
return result;
};
/**
* Generate the UNIX part of the external file attributes.
* @param {Object} unixPermissions the unix permissions or null.
* @param {Boolean} isDir true if the entry is a directory, false otherwise.
* @return {Number} a 32 bit integer.
*
* adapted from http://unix.stackexchange.com/questions/14705/the-zip-formats-external-file-attribute :
*
* TTTTsstrwxrwxrwx0000000000ADVSHR
* ^^^^____________________________ file type, see zipinfo.c (UNX_*)
* ^^^_________________________ setuid, setgid, sticky
* ^^^^^^^^^________________ permissions
* ^^^^^^^^^^______ not used ?
* ^^^^^^ DOS attribute bits : Archive, Directory, Volume label, System file, Hidden, Read only
*/
var generateUnixExternalFileAttr = function (unixPermissions, isDir) {
var result = unixPermissions;
if (!unixPermissions) {
// I can't use octal values in strict mode, hence the hexa.
// 040775 => 0x41fd
// 0100664 => 0x81b4
result = isDir ? 0x41fd : 0x81b4;
}
return (result & 0xFFFF) << 16;
};
/**
* Generate the DOS part of the external file attributes.
* @param {Object} dosPermissions the dos permissions or null.
* @param {Boolean} isDir true if the entry is a directory, false otherwise.
* @return {Number} a 32 bit integer.
*
* Bit 0 Read-Only
* Bit 1 Hidden
* Bit 2 System
* Bit 3 Volume Label
* Bit 4 Directory
* Bit 5 Archive
*/
var generateDosExternalFileAttr = function (dosPermissions, isDir) {
// the dir flag is already set for compatibility
return (dosPermissions || 0) & 0x3F;
};
/**
* Generate the various parts used in the construction of the final zip file.
* @param {string} name the file name.
* @param {ZipObject} file the file content.
* @param {JSZip.CompressedObject} compressedObject the compressed object.
* @param {number} offset the current offset from the start of the zip file.
* @param {String} platform let's pretend we are this platform (change platform dependents fields)
* @return {object} the zip parts.
*/
var generateZipParts = function(name, file, compressedObject, offset) {
var generateZipParts = function(name, file, compressedObject, offset, platform) {
var data = compressedObject.compressedContent,
utfEncodedFileName = utils.transformTo("string", utf8.utf8encode(file.name)),
comment = file.comment || "",
@ -1005,6 +1097,20 @@ var generateZipParts = function(name, file, compressedObject, offset) {
date = o.date;
}
var extFileAttr = 0;
var versionMadeBy = 0;
if (dir) {
// dos or unix, we set the dos dir flag
extFileAttr |= 0x00010;
}
if(platform === "UNIX") {
versionMadeBy = 0x031E; // UNIX, version 3.0
extFileAttr |= generateUnixExternalFileAttr(file.unixPermissions, dir);
} else { // DOS or other, fallback to DOS
versionMadeBy = 0x0014; // DOS, version 2.0
extFileAttr |= generateDosExternalFileAttr(file.dosPermissions, dir);
}
// date
// @see http://www.delorie.com/djgpp/doc/rbinter/it/52/13.html
// @see http://www.delorie.com/djgpp/doc/rbinter/it/65/16.html
@ -1097,7 +1203,7 @@ var generateZipParts = function(name, file, compressedObject, offset) {
var dirRecord = signature.CENTRAL_FILE_HEADER +
// version made by (00: DOS)
"\x14\x00" +
decToHex(versionMadeBy, 2) +
// file header (common to file and central directory)
header +
// file comment length
@ -1107,7 +1213,7 @@ var generateZipParts = function(name, file, compressedObject, offset) {
// internal file attributes TODO
"\x00\x00" +
// external file attributes
(dir === true ? "\x10\x00\x00\x00" : "\x00\x00\x00\x00") +
decToHex(extFileAttr, 4) +
// relative offset of local header
decToHex(offset, 4) +
// file name
@ -1265,12 +1371,28 @@ var out = {
options = extend(options || {}, {
base64: true,
compression: "STORE",
compressionOptions : null,
type: "base64",
comment: null
platform: "DOS",
comment: null,
mimeType: 'application/zip'
});
utils.checkSupport(options.type);
// accept nodejs `process.platform`
if(
options.platform === 'darwin' ||
options.platform === 'freebsd' ||
options.platform === 'linux' ||
options.platform === 'sunos'
) {
options.platform = "UNIX";
}
if (options.platform === 'win32') {
options.platform = "DOS";
}
var zipData = [],
localDirLength = 0,
centralDirLength = 0,
@ -1289,10 +1411,11 @@ var out = {
if (!compression) {
throw new Error(compressionName + " is not a valid compression method !");
}
var compressionOptions = file.options.compressionOptions || options.compressionOptions || {};
var compressedObject = generateCompressedObjectFrom.call(this, file, compression);
var compressedObject = generateCompressedObjectFrom.call(this, file, compression, compressionOptions);
var zipPart = generateZipParts.call(this, name, file, compressedObject, localDirLength);
var zipPart = generateZipParts.call(this, name, file, compressedObject, localDirLength, options.platform);
localDirLength += zipPart.fileRecord.length + compressedObject.compressedSize;
centralDirLength += zipPart.dirRecord.length;
zipData.push(zipPart);
@ -1350,7 +1473,7 @@ var out = {
case "nodebuffer" :
return utils.transformTo(options.type.toLowerCase(), zip);
case "blob" :
return utils.arrayBuffer2Blob(utils.transformTo("arraybuffer", zip));
return utils.arrayBuffer2Blob(utils.transformTo("arraybuffer", zip), options.mimeType);
// case "zip is a string"
case "base64" :
return (options.base64) ? base64.encode(zip) : zip;
@ -1816,13 +1939,14 @@ exports.string2binary = function(str) {
}
return result;
};
exports.arrayBuffer2Blob = function(buffer) {
exports.arrayBuffer2Blob = function(buffer, mimeType) {
exports.checkSupport("blob");
mimeType = mimeType || 'application/zip';
try {
// Blob constructor
return new Blob([buffer], {
type: "application/zip"
type: mimeType
});
}
catch (e) {
@ -1832,7 +1956,7 @@ exports.arrayBuffer2Blob = function(buffer) {
var Builder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
var builder = new Builder();
builder.append(buffer);
return builder.getBlob('application/zip');
return builder.getBlob(mimeType);
}
catch (e) {
@ -2238,6 +2362,7 @@ ZipEntries.prototype = {
this.checkSignature(sig.LOCAL_FILE_HEADER);
file.readLocalPart(this.reader);
file.handleUTF8();
file.processAttributes();
}
},
/**
@ -2261,7 +2386,24 @@ ZipEntries.prototype = {
readEndOfCentral: function() {
var offset = this.reader.lastIndexOfSignature(sig.CENTRAL_DIRECTORY_END);
if (offset === -1) {
throw new Error("Corrupted zip : can't find end of central directory");
// Check if the content is a truncated zip or complete garbage.
// A "LOCAL_FILE_HEADER" is not required at the beginning (auto
// extractible zip for example) but it can give a good hint.
// If an ajax request was used without responseType, we will also
// get unreadable data.
var isGarbage = true;
try {
this.reader.setIndex(0);
this.checkSignature(sig.LOCAL_FILE_HEADER);
isGarbage = false;
} catch (e) {}
if (isGarbage) {
throw new Error("Can't find end of central directory : is this a zip file ? " +
"If it is, see http://stuk.github.io/jszip/documentation/howto/read_zip.html");
} else {
throw new Error("Corrupted zip : can't find end of central directory");
}
}
this.reader.setIndex(offset);
this.checkSignature(sig.CENTRAL_DIRECTORY_END);
@ -2337,6 +2479,10 @@ var StringReader = _dereq_('./stringReader');
var utils = _dereq_('./utils');
var CompressedObject = _dereq_('./compressedObject');
var jszipProto = _dereq_('./object');
var MADE_BY_DOS = 0x00;
var MADE_BY_UNIX = 0x03;
// class ZipEntry {{{
/**
* An entry in the zip file.
@ -2463,7 +2609,7 @@ ZipEntry.prototype = {
* @param {DataReader} reader the reader to use.
*/
readCentralPart: function(reader) {
this.versionMadeBy = reader.readString(2);
this.versionMadeBy = reader.readInt(2);
this.versionNeeded = reader.readInt(2);
this.bitFlag = reader.readInt(2);
this.compressionMethod = reader.readString(2);
@ -2487,10 +2633,37 @@ ZipEntry.prototype = {
this.readExtraFields(reader);
this.parseZIP64ExtraField(reader);
this.fileComment = reader.readString(this.fileCommentLength);
// warning, this is true only for zip with madeBy == DOS (plateform dependent feature)
this.dir = this.externalFileAttributes & 0x00000010 ? true : false;
},
/**
* Parse the external file attributes and get the unix/dos permissions.
*/
processAttributes: function () {
this.unixPermissions = null;
this.dosPermissions = null;
var madeBy = this.versionMadeBy >> 8;
// Check if we have the DOS directory flag set.
// We look for it in the DOS and UNIX permissions
// but some unknown platform could set it as a compatibility flag.
this.dir = this.externalFileAttributes & 0x0010 ? true : false;
if(madeBy === MADE_BY_DOS) {
// first 6 bits (0 to 5)
this.dosPermissions = this.externalFileAttributes & 0x3F;
}
if(madeBy === MADE_BY_UNIX) {
this.unixPermissions = (this.externalFileAttributes >> 16) & 0xFFFF;
// the octal permissions are in (this.unixPermissions & 0x01FF).toString(8);
}
// fail safe : if the name ends with a / it probably means a folder
if (!this.dir && this.fileName.slice(-1) === '/') {
this.dir = true;
}
},
/**
* Parse the ZIP64 extra field and merge the info in the current ZipEntry.
* @param {DataReader} reader the reader to use.
@ -5554,7 +5727,7 @@ function GZheader() {
// but leave for few code modifications
//
// Setup limits is not necessary because in js we should not preallocate memory
// Setup limits is not necessary because in js we should not preallocate memory
// for inflate use constant limit in 65536 bytes
//
@ -8979,4 +9152,4 @@ function ZStream() {
module.exports = ZStream;
},{}]},{},[9])
(9)
});
});