@gitcobra/anzip
Version:
super easy way to create an uncompressed zip file.
2 lines (1 loc) • 2.96 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).AnZip=e()}(this,(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,e){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!==e){if(!/[^/]+$/.test(t))throw new Error('needs a file name: "'+t+'"');if("string"==typeof e&&(e=h(e)),!(e instanceof r))try{if(!(e.buffer||e instanceof Array||e instanceof ArrayBuffer||e instanceof Buffer))throw new Error;e=new Uint8Array(e.buffer||e)}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=e.length,o=function(t){for(var e=4294967295,r=0,i=t.length;r<i;r++)e=n[255&(e^t[r])]^e>>>8;return(4294967295^e)>>>0}(e)}for(var f=new Date,c=a(f.getFullYear()-1980<<25|f.getMonth()+1<<21|f.getDate()<<16|f.getHours()<<11|f.getMinutes()<<5|f.getSeconds()/2),s=t.replace(/\/+$/,"").split("/"),u="";s.length;){u+=s.shift();var p=e&&0===s.length;if(u+=p?"":"/",!this._d[u]){this._d[u]=!0,this._c++;var l=h(u),d=l.length,y=p?i:0,_=a(y);this._lfh.push([80,75,3,4]);var g=[10,0,0,8,0,0].concat(c,a(p?o:0),_,_,[255&d,d>>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],a(this._curLFHind)),l),this._cdhLen+=46+d,this._curLFHind+=30+d+y}}e&&this._lfh.push(e)},t.prototype.has=function(t){return!!this._d[t.replace(/\/+$/,"")]},t.prototype.zip=function(){var t,e=[80,75,5,6,0,0,0,0,255&this._c,this._c>>8,255&this._c,this._c>>8].concat(a(this._cdhLen),a(this._curLFHind),[0,0]),n=this._lfh.concat(this._cdh,[e]);if(r===Array)t=Array.prototype.concat.apply([],n);else{var i=0;t=new r(this._curLFHind+this._cdhLen+e.length);for(var o=0;o<n.length;o++){var f=n[o];t.set(f,i),i+=f.length}}return t},t.prototype.buffer=function(){return e?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 e="undefined"!=typeof Uint8Array,r=e?Uint8Array:Array,n=new(e?Uint32Array:Array)(256),i=0;i<256;i++){for(var o=i,f=0;f<8;f++)o=1&o?3988292384^o>>>1:o>>>1;n[i]=o}function h(t){var e;return"function"==typeof TextEncoder?e=new TextEncoder("utf-8").encode(t):(e=[],encodeURI(t).replace(/%(..)|(.)/g,(function(t,r,n){e.push(r?parseInt(r,16):n.charCodeAt(0))}))),e}function a(t){return[255&t,t>>8&255,t>>16&255,t>>24&255]}return t}));