@gitcobra/anzip-es6
Version:
create an uncompressed zip file
3 lines (2 loc) • 7.19 kB
JavaScript
function e(e,t,i,r){return new(i||(i=Promise))((function(n,s){function o(e){try{a(r.next(e))}catch(e){s(e)}}function l(e){try{a(r.throw(e))}catch(e){s(e)}}function a(e){var t;e.done?n(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(o,l)}a((r=r.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const t=new Blob([Uint8Array.from([80,75,3,4,10,0,0,8,0,0])]),i=new Blob([Uint8Array.from([80,75,1,2,10,0,10,0,0,8,0,0])]),r=new Blob([Uint8Array.from([0,0,0,0,0,0,0,0,0,0])]),n=new Blob([Uint8Array.from([0,0,0,0,0,0,16,0,0,0])]),s=new TextEncoder;class o{static getCRC32(e){}constructor(){this.clear()}clear(){this._dirRecords=new Map,this._dirRecList=[],this._dirRecFileList=[],this._localFileChunks=[],this._curLocalFileHeaderOffset=0,this._centralDirHeaderChunks=[],this._centralDirHeaderLen=0,this._pathCount=0,this._fileCount=0,this._dataSize=0,this._zippedBlob=null,this._resolvingCRCPromise=null}add(e,o,l,h){if(this._zippedBlob)throw new Error("the AnZip object was already zipped. create a new instance or execute clear() before adding a file.");if(!e)throw new Error("path is empty");if(e=String(e).replace(/\\/g,"/").replace(/^\//,""),/\/{2,}|\\|^\/|^[a-z]+:/i.test(e))throw new Error('invalid path. containing a drive letter, a leading slash, or empty directory name: "'+e+'"');let u,f=0,d=null;if(void 0!==o){if(!/[^/]+$/.test(e))throw new Error('needs a file name: "'+e+'"');if(this.has(e))throw new Error('the path already exists: "'+e+'"');if(void 0!==h){if("number"!=typeof h)throw new Error("precalculatedCRC must be a number");u=h}if(o instanceof Blob)d=o,f=o.size,void 0===u&&(u=0);else{if(o instanceof Uint8Array||"function"==typeof Buffer&&o instanceof Buffer)d=o;else if("string"==typeof o)d=s.encode(o);else{if(!(o instanceof ArrayBuffer||o instanceof Array))throw new Error("data must be one of the following types Array, TypedArray, ArrayBuffer, Buffer, string, or Blob.");d=new Uint8Array(o)}void 0===u&&(u=a(d)),f=d.byteLength}}let p=null;l instanceof Date?p=l:void 0===l||-1===l?p=new Date:l>=0&&(p=new Date(l));const _=p?c(p.getFullYear()-1980<<25|p.getMonth()+1<<21|p.getDate()<<16|p.getHours()<<11|p.getMinutes()<<5|p.getSeconds()/2):[0,0,0,0];let C,b,w,y=e.replace(/\/+$/,"").split("/"),g="";for(;y.length;){g+=y.shift();const e=Boolean(o&&0===y.length);if(g+=e?"":"/",this._dirRecords.has(g))continue;const l=s.encode(g),a=l.length,h=e&&f?c(f):[0,0,0,0],d={offset:this._curLocalFileHeaderOffset,size:f,path:g,pathLength:a,isFile:e};this._dirRecords.set(g,d),this._dirRecList.push(d),e&&this._dirRecFileList.push(d),this._pathCount++,C=Uint8Array.from([].concat(_,e?c(u):[0,0,0,0],h,h,[255&a,a>>8&255,0,0])),b=[t,C,l],this._localFileChunks.push(b);const p=e?r:n,w=Uint8Array.from(c(this._curLocalFileHeaderOffset));this._centralDirHeaderChunks.push([i,C,p,w,l]),this._centralDirHeaderLen+=46+a,this._curLocalFileHeaderOffset+=30+a+(e?f:0)}d&&(void 0===h&&d instanceof Blob&&(w=this._resolveBlobCRC(d,C,e)),b.push(d),this._dataSize+=f,this._fileCount++);let R=this._pathCount;return(w||Promise.resolve()).then((()=>R))}_resolveBlobCRC(e,t,i){let r,n;const s=this._resolvingCRCPromise||Promise.resolve(),o=new Promise(((e,t)=>{r=e,n=t}));this._resolvingCRCPromise=o;let l=!1;return s.finally((()=>{(function(e){return h(e).then((e=>a(new Uint8Array(e))))})(e).then((e=>{new DataView(t.buffer).setUint32(4,e,!0),l=!0})).catch((e=>{throw this.remove(i),new Error(`${e.message}\ncould not resolve the blob CRC.`)})).finally((()=>{this._resolvingCRCPromise===o&&(this._resolvingCRCPromise=null),l?r():n()}))})),o}_checkPending(){if(this._resolvingCRCPromise)throw new Error("the AnZip object is still pending. execute wait() and await the fulfillment of the Promise beforehand.")}_getFileRecord(e){let t;return"string"==typeof e?t=this._dirRecords.get(e):"number"==typeof e&&(t=this._dirRecFileList[e]),t||null}has(e){return!!this._dirRecords.has(e.replace(/\/+$/,""))}size(){return this._dataSize}count(e){return e?this._pathCount:this._fileCount}get(e,t){const i=this._getFileRecord(e);if(i&&i.isFile){if(this._zippedBlob){const{offset:e,size:r,pathLength:n}=i,s=e+n+30;return this._zippedBlob.slice(s,s+r,t)}{const e=this._dirRecList.indexOf(i),r=this._localFileChunks[e][3];return"string"!=typeof t||r instanceof Blob&&(!t||r.type===t)?r:new Blob([r],{type:t})}}return null}getPathByIndex(e){var t;return(null===(t=this._dirRecFileList[e])||void 0===t?void 0:t.path)||""}remove(e){if(this._zippedBlob)throw new Error(`could not remove the file. the AnZip object is already zipped. ${e}`);const t=this._getFileRecord(e);if(!t||!t.isFile)return!1;const i=this._dirRecList.indexOf(t);this._localFileChunks.splice(i,1)[0],this._centralDirHeaderChunks.splice(i,1)[0];const r=30+t.pathLength+t.size;this._curLocalFileHeaderOffset-=r;const n=46+t.pathLength;this._centralDirHeaderLen-=n;for(let e=i;e<this._centralDirHeaderChunks.length;e++){const t=this._centralDirHeaderChunks[e][3],i=new DataView(t.buffer).getUint32(0,!0);this._centralDirHeaderChunks[e][3]=Uint8Array.from(c(i-r))}return this._pathCount--,this._fileCount--,this._dataSize-=t.size,this._dirRecords.delete(t.path),this._dirRecFileList.splice(this._dirRecFileList.indexOf(t),1),this._dirRecList.splice(this._dirRecList.indexOf(t),1),!0}list(e){const t=[];for(const[i,r]of this._dirRecords)(e||r.isFile)&&t.push({path:r.path,size:r.size,isFile:r.isFile});return t}buffer(){return this.zip().then((e=>h(e)))}blob(){return this.zip()}url(){return this.zip().then((e=>URL.createObjectURL(e)))}urlSync(){return URL.createObjectURL(this.zipSync())}wait(t){return e(this,void 0,void 0,(function*(){for(;this._resolvingCRCPromise&&(yield this._resolvingCRCPromise,t););}))}_buildZipBlob(e){return new Blob(e||this._createCurrentBlobParts(),{type:"application/zip"})}_createCurrentBlobParts(){const e=new Uint8Array([].concat([80,75,5,6,0,0,0,0,255&this._pathCount,this._pathCount>>8,255&this._pathCount,this._pathCount>>8],c(this._centralDirHeaderLen),c(this._curLocalFileHeaderOffset),[0,0]));let t=[];return t=t.concat(...this._localFileChunks,...this._centralDirHeaderChunks,e),t}zip(t){return e(this,void 0,void 0,(function*(){if(this._zippedBlob)return this._zippedBlob;const e=this._createCurrentBlobParts();yield this._resolvingCRCPromise;const i=this._buildZipBlob(e);return t&&this._close(i),i}))}zipSync(e){if(this._zippedBlob)return this._zippedBlob;this._checkPending();const t=this._buildZipBlob(this._createCurrentBlobParts());return e&&this._close(t),t}_close(e){this._zippedBlob=e,this._localFileChunks=[],this._centralDirHeaderChunks=[]}}const l=new Uint32Array(256);for(let e=0;e<256;e++){let t=e;for(let e=0;e<8;e++)t=1&t?3988292384^t>>>1:t>>>1;l[e]=t}function a(e){let t=4294967295;for(let i=0,r=e.length;i<r;i++)t=l[255&(t^e[i])]^t>>>8;return(4294967295^t)>>>0}function h(e){var t;return(null===(t=e.arrayBuffer)||void 0===t?void 0:t.call(e))||new Promise((t=>{let i=new FileReader;i.onload=e=>{t(e.target.result)},i.onerror=()=>{throw new Error("could not read it as ArrayBuffer")},i.readAsArrayBuffer(e),i=null}))}function c(e){return[255&e,e>>8&255,e>>16&255,e>>24&255]}export{o as default};
//# sourceMappingURL=anzip-es6.min.js.map