1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-05 15:32:55 +02:00
epub.js/remove/js/libs/zip.js
2013-06-04 22:56:02 -07:00

1 line
No EOL
13 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(function(e){function v(){if(typeof d=="undefined"){var e;e=new p,e.append(y(0).view),d=e.getBlob().size==0}return d}function m(){var e=-1,t=this;t.append=function(n){var r,i=t.table;for(r=0;r<n.length;r++)e=e>>>8^i[(e^n[r])&255]},t.get=function(){return~e}}function g(e,t,n){if(e.slice)return e.slice(t,t+n);if(e.webkitSlice)return e.webkitSlice(t,t+n);if(e.mozSlice)return e.mozSlice(t,t+n);if(e.msSlice)return e.msSlice(t,t+n)}function y(e,t){var n,r;return n=new ArrayBuffer(e),r=new Uint8Array(n),t&&r.set(t,0),{buffer:n,array:r,view:new DataView(n)}}function b(){}function w(e){function r(r,i){var s=new p;s.append(e),n=new S(s.getBlob("text/plain")),n.init(function(){t.size=n.size,r()},i)}function i(e,t,r,i){n.readUint8Array(e,t,r,i)}var t=this,n;t.size=0,t.init=r,t.readUint8Array=i}function E(t){function i(e,i){var s=t.length;while(t.charAt(s-1)=="=")s--;r=t.indexOf(",")+1,n.size=Math.floor((s-r)*.75),e()}function s(n,i,s,o){var u,a=y(i),f=Math.floor(n/3)*4,l=Math.ceil((n+i)/3)*4,c=e.atob(t.substring(f+r,l+r)),h=n-Math.floor(f/4)*3;for(u=h;u<h+i;u++)a.array[u-h]=c.charCodeAt(u);s(a.array)}var n=this,r;n.size=0,n.init=i,n.readUint8Array=s}function S(e){function n(t,n){this.size=e.size,t()}function r(t,n,r,i){var s=new FileReader;s.onload=function(e){r(new Uint8Array(e.target.result))},s.onerror=i,s.readAsArrayBuffer(g(e,t,n))}var t=this;t.size=0,t.init=n,t.readUint8Array=r}function x(e){function n(n,r){var i;t.data?n():(i=new XMLHttpRequest,i.addEventListener("load",function(){t.size||(t.size=Number(i.getResponseHeader("Content-Length"))),t.data=new Uint8Array(i.response),n()},!1),i.addEventListener("error",r,!1),i.open("GET",e),i.responseType="arraybuffer",i.send())}function r(n,r){var i=new XMLHttpRequest;i.addEventListener("load",function(){t.size=Number(i.getResponseHeader("Content-Length")),n()},!1),i.addEventListener("error",r,!1),i.open("HEAD",e),i.send()}function i(e,r,i,s){n(function(){i(new Uint8Array(t.data.subarray(e,e+r)))},s)}var t=this;t.size=0,t.init=r,t.readUint8Array=i}function T(e){function n(n,r){var i=new XMLHttpRequest;i.addEventListener("load",function(){t.size=Number(i.getResponseHeader("Content-Length")),i.getResponseHeader("Accept-Ranges")=="bytes"?n():r(f)},!1),i.addEventListener("error",r,!1),i.open("HEAD",e),i.send()}function r(t,n,r,i){var s=new XMLHttpRequest;s.open("GET",e),s.responseType="arraybuffer",s.setRequestHeader("Range","bytes="+t+"-"+(t+n-1)),s.addEventListener("load",function(){r(s.response)},!1),s.addEventListener("error",i,!1),s.send()}function i(e,t,n,i){r(e,t,function(e){n(new Uint8Array(e))},i)}var t=this;t.size=0,t.init=n,t.readUint8Array=i}function N(){}function C(){function n(e,n){t=new p,e()}function r(e,n,r){t.append(v()?e:e.buffer),n()}function i(e){var n=new FileReader;n.onload=function(t){e(t.target.result)},n.onerror=onerror,n.readAsText(t.getBlob("text/plain"))}var e=this,t;e.init=n,e.writeUint8Array=r,e.getData=i}function k(t){function s(e,n){r+="data:"+(t||"")+";base64,",e()}function o(t,n,s){var o,u=i.length,a=i;i="";for(o=0;o<Math.floor((u+t.length)/3)*3-u;o++)a+=String.fromCharCode(t[o]);for(;o<t.length;o++)i+=String.fromCharCode(t[o]);a.length>2?r+=e.btoa(a):i=a,n()}function u(t){t(r+e.btoa(i))}var n=this,r="",i="";n.init=s,n.writeUint8Array=o,n.getData=u}function L(e,t){function i(t,r){e.createWriter(function(e){n=e,t()},r)}function s(e,r,i){var s=new p;s.append(v()?e:e.buffer),n.onwrite=function(){n.onwrite=null,r()},n.onerror=i,n.write(s.getBlob(t))}function o(t){e.file(t)}var n,r=this;r.init=i,r.writeUint8Array=s,r.getData=o}function A(e){function r(e,n){t=new p,e()}function i(e,n,r){t.append(v()?e:e.buffer),n()}function s(n){n(t.getBlob(e))}var t,n=this;n.init=r,n.writeUint8Array=i,n.getData=s}function O(e,t,n,r,i,s,o,u,a,f){function d(){e.removeEventListener("message",v,!1),u(p)}function v(e){var t=e.data,r=t.data;t.onappend&&(p+=r.length,n.writeUint8Array(r,function(){s(!1,r),m()},f)),t.onflush&&(r?(p+=r.length,n.writeUint8Array(r,function(){s(!1,r),d()},f)):d()),t.progress&&o&&o(h+t.current,i)}function m(){h=c*l,h<i?t.readUint8Array(r+h,Math.min(l,i-h),function(t){e.postMessage({append:!0,data:t}),c++,o&&o(h,i),s(!0,t)},a):e.postMessage({flush:!0})}var c=0,h,p;p=0,e.addEventListener("message",v,!1),m()}function M(e,t,n,r,i,s,o,u,a,f){function d(){var v;h=c*l,h<i?t.readUint8Array(r+h,Math.min(l,i-h),function(t){var u=e.append(t,function(){o&&o(r+h,i)});p+=u.length,s(!0,t),n.writeUint8Array(u,function(){s(!1,u),c++,setTimeout(d,1)},f),o&&o(h,i)},a):(v=e.flush(),v?(p+=v.length,n.writeUint8Array(v,function(){s(!1,v),u(p)},f)):u(p))}var c=0,h,p=0;d()}function _(t,n,r,i,s,o,u,a,f){function p(e,t){s&&!e&&h.append(t)}function d(e){o(e,h.get())}var l,h=new m;return e.zip.useWebWorkers?(l=new Worker(e.zip.workerScriptsPath+c),O(l,t,n,r,i,p,u,d,a,f)):M(new e.zip.Inflater,t,n,r,i,p,u,d,a,f),l}function D(t,n,r,i,s,o,u){function l(e,t){e&&f.append(t)}function c(e){i(e,f.get())}function p(){a.removeEventListener("message",p,!1),O(a,t,n,0,t.size,l,s,c,o,u)}var a,f=new m;return e.zip.useWebWorkers?(a=new Worker(e.zip.workerScriptsPath+h),a.addEventListener("message",p,!1),a.postMessage({init:!0,level:r})):M(new e.zip.Deflater,t,n,0,t.size,l,s,c,o,u),a}function P(e,t,n,r,i,s,o,u,a){function h(){var p=f*l;p<r?e.readUint8Array(n+p,Math.min(l,r-p),function(e){i&&c.append(e),o&&o(p,r,e),t.writeUint8Array(e,function(){f++,h()},a)},u):s(r,c.get())}var f=0,c=new m;h()}function H(e){var t,n="",r,i=["Ç","ü","é","â","ä","à","å","ç","ê","ë","è","ï","î","ì","Ä","Å","É","æ","Æ","ô","ö","ò","û","ù","ÿ","Ö","Ü","ø","£","Ø","×","ƒ","á","í","ó","ú","ñ","Ñ","ª","º","¿","®","¬","½","¼","¡","«","»","_","_","_","¦","¦","Á","Â","À","©","¦","¦","+","+","¢","¥","+","+","-","-","+","-","+","ã","Ã","+","+","-","-","¦","-","+","¤","ð","Ð","Ê","Ë","È","i","Í","Î","Ï","+","+","_","_","¦","Ì","_","Ó","ß","Ô","Ò","õ","Õ","µ","þ","Þ","Ú","Û","Ù","ý","Ý","¯","´","­","±","_","¾","¶","§","÷","¸","°","¨","·","¹","³","²","_"," "];for(t=0;t<e.length;t++)r=e.charCodeAt(t)&255,r>127?n+=i[r-128]:n+=String.fromCharCode(r);return n}function B(e){var t=[],n=0,r=0,i=0,s=0,o=0;e+="";while(n<e.length)i=e.charCodeAt(n),i<128?(t[r++]=String.fromCharCode(i),n++):i>191&&i<224?(s=e.charCodeAt(n+1),t[r++]=String.fromCharCode((i&31)<<6|s&63),n+=2):(s=e.charCodeAt(n+1),o=e.charCodeAt(n+2),t[r++]=String.fromCharCode((i&15)<<12|(s&63)<<6|o&63),n+=3);return t.join("")}function j(e){var t,n="";for(t=0;t<e.length;t++)n+=String.fromCharCode(e[t]);return n}function F(e){var t=(e&4294901760)>>16,n=e&65535;try{return new Date(1980+((t&65024)>>9),((t&480)>>5)-1,t&31,(n&63488)>>11,(n&2016)>>5,(n&31)*2,0)}catch(r){}}function I(e,t,i,s){e.version=t.view.getUint16(i,!0),e.bitFlag=t.view.getUint16(i+2,!0),e.compressionMethod=t.view.getUint16(i+4,!0),e.lastModDateRaw=t.view.getUint32(i+6,!0),e.lastModDate=F(e.lastModDateRaw);if((e.bitFlag&1)===1){onerror(n);return}if(s||(e.bitFlag&8)!=8)e.crc32=t.view.getUint32(i+10,!0),e.compressedSize=t.view.getUint32(i+14,!0),e.uncompressedSize=t.view.getUint32(i+18,!0);if(e.compressedSize===4294967295||e.uncompressedSize===4294967295){onerror(r);return}e.filenameLength=t.view.getUint16(i+22,!0),e.extraFieldLength=t.view.getUint16(i+24,!0)}function q(e,n){function r(){}return r.prototype.getData=function(r,i,s,a){function c(e,t){l&&l.terminate(),l=null,e&&e(t)}function h(e){var t=y(4);return t.view.setUint32(0,e),f.crc32==t.view.getUint32(0)}function p(e,t){a&&!h(t)?d():r.getData(function(e){c(i,e)})}function d(){c(n,u)}function v(){c(n,o)}var f=this,l;e.readUint8Array(f.offset,30,function(i){var o=y(i.length,i),u;if(o.view.getUint32(0)!=1347093252){n(t);return}I(f,o,4),u=f.offset+30+f.filenameLength+f.extraFieldLength,r.init(function(){f.compressionMethod===0?P(e,r,u,f.compressedSize,a,p,s,d,v):l=_(e,r,u,f.compressedSize,a,p,s,d,v)},v)},d)},{getEntries:function(s){if(e.size<22){n(t);return}e.readUint8Array(e.size-22,22,function(o){var u=y(o.length,o).view,a,f;if(u.getUint32(0)!=1347093766){n(t);return}a=u.getUint32(16,!0),f=u.getUint16(8,!0),e.readUint8Array(a,e.size-a,function(e){var i,o=0,u=[],a,l,c,h=y(e.length,e);for(i=0;i<f;i++){a=new r;if(h.view.getUint32(o)!=1347092738){n(t);return}I(a,h,o+6,!0),a.commentLength=h.view.getUint16(o+32,!0),a.directory=(h.view.getUint8(o+38)&16)==16,a.offset=h.view.getUint32(o+42,!0),l=j(h.array.subarray(o+46,o+46+a.filenameLength)),a.filename=(a.bitFlag&2048)===2048?B(l):H(l),!a.directory&&a.filename.charAt(a.filename.length-1)=="/"&&(a.directory=!0),c=j(h.array.subarray(o+46+a.filenameLength+a.extraFieldLength,o+46+a.filenameLength+a.extraFieldLength+a.commentLength)),a.comment=(a.bitFlag&2048)===2048?B(c):H(c),u.push(a),o+=46+a.filenameLength+a.extraFieldLength+a.commentLength}s(u)},function(){n(i)})},function(){n(i)})},close:function(e){e&&e()}}}function R(e){var t,n,r,i=[],s=0,o=0,u=e.length;for(t=0;t<u;t++)n=e.charCodeAt(t),r=null,n<128?o++:n>127&&n<2048?r=String.fromCharCode(n>>6|192)+String.fromCharCode(n&63|128):r=String.fromCharCode(n>>12|224)+String.fromCharCode(n>>6&63|128)+String.fromCharCode(n&63|128),r!=null&&(o>s&&(i+=e.slice(s,o)),i+=r,s=o=t+1);return o>s&&(i+=e.slice(s,u)),i}function U(e){var t,n=[];for(t=0;t<e.length;t++)n.push(e.charCodeAt(t));return n}function z(e,t,n){function l(e,t){r&&r.terminate(),r=null,e&&e(t)}function c(){l(t,s)}function h(){l(t,u)}var r,i=[],o=[],f=0;return{add:function(t,s,u,p,d){function b(r){var s;g=d.lastModDate||new Date,v=y(26),i[t]={headerArray:v.array,directory:d.directory,filename:m,offset:f,comment:U(R(d.comment||""))},v.view.setUint32(0,335546376),d.version&&v.view.setUint8(0,d.version),!n&&d.level!=0&&v.view.setUint16(4,2048),v.view.setUint16(6,(g.getHours()<<6|g.getMinutes())<<5|g.getSeconds()/2,!0),v.view.setUint16(8,(g.getFullYear()-1980<<4|g.getMonth()+1)<<5|g.getDate(),!0),v.view.setUint16(22,m.length,!0),s=y(30+m.length),s.view.setUint32(0,1347093252),s.array.set(v.array,4),s.array.set([],30),s.array.set(m,30),f+=s.array.length,e.writeUint8Array(s.array,r,c)}function w(t,n){var r=y(16);f+=t||0,r.view.setUint32(0,1347094280),typeof n!="undefined"&&(v.view.setUint32(10,n,!0),r.view.setUint32(4,n,!0)),s&&(r.view.setUint32(8,t,!0),v.view.setUint32(14,t,!0),r.view.setUint32(12,s.size,!0),v.view.setUint32(18,s.size,!0)),e.writeUint8Array(r.array,function(){f+=16,l(u)},c)}function E(){d=d||{},t=t.trim(),d.directory&&t.charAt(t.length-1)!="/"&&(t+="/");if(i[t])throw a;m=U(R(t)),o.push(t),b(function(){s?n||d.level==0?P(s,e,0,s.size,!0,w,p,h,c):r=D(s,e,d.level,w,p,h,c):w()},c)}var v,m,g;s?s.init(E,h):E()},close:function(t){var n,r=0,s=0;o.forEach(function(e){var t=i[e];r+=46+t.filename.length+t.comment.length}),n=y(r+22),o.forEach(function(e){var t=i[e];n.view.setUint32(s,1347092738),n.view.setUint16(s+4,5120),n.array.set(t.headerArray,s+6),n.view.setUint16(s+32,t.comment.length,!0),t.directory&&n.view.setUint8(s+38,16),n.view.setUint32(s+42,t.offset,!0),n.array.set(t.filename,s+46),n.array.set(t.comment,s+46+t.filename.length),s+=46+t.filename.length+t.comment.length}),n.view.setUint32(s,1347093766),n.view.setUint16(s+8,o.length,!0),n.view.setUint16(s+10,o.length,!0),n.view.setUint32(s+12,r,!0),n.view.setUint32(s+16,f,!0),e.writeUint8Array(n.array,function(){l(function(){e.getData(t)})},c)}}}var t="File format is not recognized.",n="File contains encrypted entry.",r="File is using Zip64 (4gb+ file size).",i="Error while reading zip file.",s="Error while writing zip file.",o="Error while writing file data.",u="Error while reading file data.",a="File already exists.",f="HTTP Range not supported.",l=524288,c="inflate.js",h="deflate.js",p=e.WebKitBlobBuilder||e.MozBlobBuilder||e.MSBlobBuilder||e.BlobBuilder,d;m.prototype.table=function(){var e,t,n,r=[];for(e=0;e<256;e++){n=e;for(t=0;t<8;t++)n&1?n=n>>>1^3988292384:n>>>=1;r[e]=n}return r}(),w.prototype=new b,w.prototype.constructor=w,E.prototype=new b,E.prototype.constructor=E,S.prototype=new b,S.prototype.constructor=S,x.prototype=new b,x.prototype.constructor=x,T.prototype=new b,T.prototype.constructor=T,N.prototype.getData=function(e){e(this.data)},C.prototype=new N,C.prototype.constructor=C,k.prototype=new N,k.prototype.constructor=k,L.prototype=new N,L.prototype.constructor=L,A.prototype=new N,A.prototype.constructor=A,typeof p=="undefined"&&(p=function(){function n(){t||(t=[new Blob])}var e=this,t;e.append=function(e){n(),t.push(e)},e.getBlob=function(e){n();if(t.length>1||t[0].type!=e)t=[e?new Blob(t,{type:e}):new Blob(t)];return t[0]}}),e.zip={Reader:b,Writer:N,BlobReader:S,HttpReader:x,HttpRangeReader:T,Data64URIReader:E,TextReader:w,BlobWriter:A,FileWriter:L,Data64URIWriter:k,TextWriter:C,createReader:function(e,t,n){e.init(function(){t(q(e,n))},n)},createWriter:function(e,t,n,r){e.init(function(){t(z(e,n,r))},n)},workerScriptsPath:"",useWebWorkers:!0}})(this);