UNPKG

@gitcobra/anzip

Version:

super easy way to create an uncompressed zip file.

2 lines (1 loc) 2.77 kB
var AnZip=function(){"use strict";function t(){this.clear()}t.prototype.clear=function(){this._d={},this._lfh=[],this._curLFHind=0,this._cdh=[],this._cdhLen=0,this._c=0},t.prototype.add=function(t,r){if(!t)throw new Error("path is empty");if(t=String(t).replace(/\\/g,"/"),/\/{2,}|\\|^\/|^[a-z]+:/i.test(t))throw new Error('invalid path. containing a drive letter, a leading slash, or empty directory name: "'+t+'"');var i=0,o=0;if(void 0!==r){if(!/[^/]+$/.test(t))throw new Error('needs a file name: "'+t+'"');if("string"==typeof r&&(r=a(r)),!(r instanceof e))try{if(!(r.buffer||r instanceof Array||r instanceof ArrayBuffer||r instanceof Buffer))throw new Error;r=new Uint8Array(r.buffer||r)}catch(t){throw new Error("data must be one of the following types Array, TypedArray, ArrayBuffer, Buffer, or string.")}if(this.has(t))throw new Error("the path already exists: "+t);i=r.length,o=function(t){for(var r=4294967295,e=0,i=t.length;e<i;e++)r=n[255&(r^t[e])]^r>>>8;return(4294967295^r)>>>0}(r)}for(var h=new Date,f=c(h.getFullYear()-1980<<25|h.getMonth()+1<<21|h.getDate()<<16|h.getHours()<<11|h.getMinutes()<<5|h.getSeconds()/2),s=t.replace(/\/+$/,"").split("/"),u="";s.length;){u+=s.shift();var p=r&&0===s.length;if(u+=p?"":"/",!this._d[u]){this._d[u]=!0,this._c++;var l=a(u),y=l.length,d=p?i:0,_=c(d);this._lfh.push([80,75,3,4]);var g=[10,0,0,8,0,0].concat(f,c(p?o:0),_,_,[255&y,y>>8&255,0,0]);this._lfh.push(g,l),this._cdh.push([80,75,1,2,10,0].concat(g,[0,0,0,0,0,0,p?0:16,0,0,0],c(this._curLFHind)),l),this._cdhLen+=46+y,this._curLFHind+=30+y+d}}r&&this._lfh.push(r)},t.prototype.has=function(t){return!!this._d[t.replace(/\/+$/,"")]},t.prototype.zip=function(){var t,r=[80,75,5,6,0,0,0,0,255&this._c,this._c>>8,255&this._c,this._c>>8].concat(c(this._cdhLen),c(this._curLFHind),[0,0]),n=this._lfh.concat(this._cdh,[r]);if(e===Array)t=Array.prototype.concat.apply([],n);else{var i=0;t=new e(this._curLFHind+this._cdhLen+r.length);for(var o=0;o<n.length;o++){var h=n[o];t.set(h,i),i+=h.length}}return t},t.prototype.buffer=function(){return r?this.zip().buffer:null},t.prototype.blob=function(){return"function"!=typeof Blob?null:new Blob([this.zip()],{type:"application/zip"})},t.prototype.url=function(){return this.blob()&&"function"==typeof URL&&"function"==typeof URL.createObjectURL?URL.createObjectURL(this.blob()):""};for(var r="undefined"!=typeof Uint8Array,e=r?Uint8Array:Array,n=new(r?Uint32Array:Array)(256),i=0;i<256;i++){for(var o=i,h=0;h<8;h++)o=1&o?3988292384^o>>>1:o>>>1;n[i]=o}function a(t){var r;return"function"==typeof TextEncoder?r=new TextEncoder("utf-8").encode(t):(r=[],encodeURI(t).replace(/%(..)|(.)/g,(function(t,e,n){r.push(e?parseInt(e,16):n.charCodeAt(0))}))),r}function c(t){return[255&t,t>>8&255,t>>16&255,t>>24&255]}return t}();