#!/usr/bin/env node
import{a as z}from"./chunk-JBABAK44.js";import{a as $,b as w}from"./chunk-GTNBSMZR.js";import"./chunk-KGOUQS5A.js";import{constants as C,gunzipSync as x,gzipSync as b}from"node:zlib";var I={name:"gzip",summary:"compress or expand files",usage:"gzip [OPTION]... [FILE]...",description:`Compress FILEs (by default, in-place).

When no FILE is given, or when FILE is -, read from standard input.

With -d, decompress instead.`,options:["-c, --stdout      write to standard output, keep original files","-d, --decompress  decompress","-f, --force       force overwrite of output file","-k, --keep        keep (don't delete) input files","-l, --list        list compressed file contents","-n, --no-name     do not save or restore the original name and timestamp","-N, --name        save or restore the original file name and timestamp","-q, --quiet       suppress all warnings","-r, --recursive   operate recursively on directories","-S, --suffix=SUF  use suffix SUF on compressed files (default: .gz)","-t, --test        test compressed file integrity","-v, --verbose     verbose mode","-1, --fast        compress faster","-9, --best        compress better","    --help        display this help and exit"]},q={name:"gunzip",summary:"decompress files",usage:"gunzip [OPTION]... [FILE]...",description:`Decompress FILEs (by default, in-place).

When no FILE is given, or when FILE is -, read from standard input.`,options:["-c, --stdout      write to standard output, keep original files","-f, --force       force overwrite of output file","-k, --keep        keep (don't delete) input files","-l, --list        list compressed file contents","-n, --no-name     do not restore the original name and timestamp","-N, --name        restore the original file name and timestamp","-q, --quiet       suppress all warnings","-r, --recursive   operate recursively on directories","-S, --suffix=SUF  use suffix SUF on compressed files (default: .gz)","-t, --test        test compressed file integrity","-v, --verbose     verbose mode","    --help        display this help and exit"]},P={name:"zcat",summary:"decompress files to stdout",usage:"zcat [OPTION]... [FILE]...",description:`Decompress FILEs to standard output.

When no FILE is given, or when FILE is -, read from standard input.`,options:["-f, --force       force; read compressed data even from a terminal","-l, --list        list compressed file contents","-q, --quiet       suppress all warnings","-S, --suffix=SUF  use suffix SUF on compressed files (default: .gz)","-t, --test        test compressed file integrity","-v, --verbose     verbose mode","    --help        display this help and exit"]},L={stdout:{short:"c",long:"stdout",type:"boolean"},toStdout:{long:"to-stdout",type:"boolean"},decompress:{short:"d",long:"decompress",type:"boolean"},uncompress:{long:"uncompress",type:"boolean"},force:{short:"f",long:"force",type:"boolean"},keep:{short:"k",long:"keep",type:"boolean"},list:{short:"l",long:"list",type:"boolean"},noName:{short:"n",long:"no-name",type:"boolean"},name:{short:"N",long:"name",type:"boolean"},quiet:{short:"q",long:"quiet",type:"boolean"},recursive:{short:"r",long:"recursive",type:"boolean"},suffix:{short:"S",long:"suffix",type:"string",default:".gz"},test:{short:"t",long:"test",type:"boolean"},verbose:{short:"v",long:"verbose",type:"boolean"},fast:{short:"1",long:"fast",type:"boolean"},level2:{short:"2",type:"boolean"},level3:{short:"3",type:"boolean"},level4:{short:"4",type:"boolean"},level5:{short:"5",type:"boolean"},level6:{short:"6",type:"boolean"},level7:{short:"7",type:"boolean"},level8:{short:"8",type:"boolean"},best:{short:"9",long:"best",type:"boolean"}};function F(e){return e.best?C.Z_BEST_COMPRESSION:e.level8?8:e.level7?7:e.level6?6:e.level5?5:e.level4?4:e.level3?3:e.level2?2:e.fast?C.Z_BEST_SPEED:C.Z_DEFAULT_COMPRESSION}function S(e){if(e.length<10)return{originalName:null,mtime:null,headerSize:0};if(e[0]!==31||e[1]!==139)return{originalName:null,mtime:null,headerSize:0};let t=e[3],o=e[4]|e[5]<<8|e[6]<<16|e[7]<<24,r=10;if(t&4){if(r+2>e.length)return{originalName:null,mtime:null,headerSize:0};let s=e[r]|e[r+1]<<8;r+=2+s}let n=null;if(t&8){let s=r;for(;r<e.length&&e[r]!==0;)r++;r<e.length&&(n=new TextDecoder().decode(e.slice(s,r)),r++)}if(t&16){for(;r<e.length&&e[r]!==0;)r++;r++}return t&2&&(r+=2),{originalName:n,mtime:o>0?new Date(o*1e3):null,headerSize:r}}function D(e){if(e.length<4)return 0;let t=e.length;return e[t-4]|e[t-3]<<8|e[t-2]<<16|e[t-1]<<24}function y(e){return e.length>=2&&e[0]===31&&e[1]===139}async function E(e,t,o,r,n,s){let u=o.suffix,f,p,l;if(t==="-"||t==="")if(l=Uint8Array.from(e.stdin,d=>d.charCodeAt(0)),n){if(!y(l))return o.quiet?{stdout:"",stderr:"",exitCode:1}:{stdout:"",stderr:`${r}: stdin: not in gzip format
`,exitCode:1};try{let d=x(l);return{stdout:String.fromCharCode(...d),stderr:"",exitCode:0}}catch(d){let i=d instanceof Error?d.message:"unknown error";return{stdout:"",stderr:`${r}: stdin: ${i}
`,exitCode:1}}}else{let d=F(o),i=b(l,{level:d});return{stdout:String.fromCharCode(...i),stderr:"",exitCode:0}}f=e.fs.resolvePath(e.cwd,t);try{if((await e.fs.stat(f)).isDirectory)return o.recursive?await k(e,f,o,r,n,s):o.quiet?{stdout:"",stderr:"",exitCode:1}:{stdout:"",stderr:`${r}: ${t}: is a directory -- ignored
`,exitCode:1}}catch{return{stdout:"",stderr:`${r}: ${t}: No such file or directory
`,exitCode:1}}try{l=await e.fs.readFileBuffer(f)}catch{return{stdout:"",stderr:`${r}: ${t}: No such file or directory
`,exitCode:1}}if(n){if(!t.endsWith(u))return o.quiet?{stdout:"",stderr:"",exitCode:1}:{stdout:"",stderr:`${r}: ${t}: unknown suffix -- ignored
`,exitCode:1};if(!y(l))return o.quiet?{stdout:"",stderr:"",exitCode:1}:{stdout:"",stderr:`${r}: ${t}: not in gzip format
`,exitCode:1};let d;try{d=x(l)}catch(i){let c=i instanceof Error?i.message:"unknown error";return{stdout:"",stderr:`${r}: ${t}: ${c}
`,exitCode:1}}if(s)return{stdout:String.fromCharCode(...d),stderr:"",exitCode:0};if(o.name){let i=S(l);i.originalName?p=e.fs.resolvePath(e.cwd,i.originalName):p=f.slice(0,-u.length)}else p=f.slice(0,-u.length);if(!o.force)try{return await e.fs.stat(p),{stdout:"",stderr:`${r}: ${p} already exists; not overwritten
`,exitCode:1}}catch{}if(await e.fs.writeFile(p,d),!o.keep&&!s&&await e.fs.rm(f),o.verbose){let i=l.length>0?((1-l.length/d.length)*100).toFixed(1):"0.0";return{stdout:"",stderr:`${t}:	${i}% -- replaced with ${p.split("/").pop()}
`,exitCode:0}}return{stdout:"",stderr:"",exitCode:0}}else{if(t.endsWith(u))return o.quiet?{stdout:"",stderr:"",exitCode:1}:{stdout:"",stderr:`${r}: ${t} already has ${u} suffix -- unchanged
`,exitCode:1};let d=F(o),i;try{i=b(l,{level:d})}catch(c){let a=c instanceof Error?c.message:"unknown error";return{stdout:"",stderr:`${r}: ${t}: ${a}
`,exitCode:1}}if(s)return{stdout:String.fromCharCode(...i),stderr:"",exitCode:0};if(p=f+u,!o.force)try{return await e.fs.stat(p),{stdout:"",stderr:`${r}: ${p} already exists; not overwritten
`,exitCode:1}}catch{}if(await e.fs.writeFile(p,i),!o.keep&&!s&&await e.fs.rm(f),o.verbose){let c=l.length>0?((1-i.length/l.length)*100).toFixed(1):"0.0";return{stdout:"",stderr:`${t}:	${c}% -- replaced with ${p.split("/").pop()}
`,exitCode:0}}return{stdout:"",stderr:"",exitCode:0}}}async function k(e,t,o,r,n,s){let u=await e.fs.readdir(t),f="",p="",l=0;for(let d of u){let i=e.fs.resolvePath(t,d),c=await e.fs.stat(i);if(c.isDirectory){let a=await k(e,i,o,r,n,s);f+=a.stdout,p+=a.stderr,a.exitCode!==0&&(l=a.exitCode)}else if(c.isFile){let a=o.suffix;if(n&&!d.endsWith(a)||!n&&d.endsWith(a))continue;let m=i.startsWith(`${e.cwd}/`)?i.slice(e.cwd.length+1):i,g=await E(e,m,o,r,n,s);f+=g.stdout,p+=g.stderr,g.exitCode!==0&&(l=g.exitCode)}}return{stdout:f,stderr:p,exitCode:l}}async function O(e,t,o,r){let n;if(t==="-"||t==="")n=Uint8Array.from(e.stdin,i=>i.charCodeAt(0));else{let i=e.fs.resolvePath(e.cwd,t);try{n=await e.fs.readFileBuffer(i)}catch{return{stdout:"",stderr:`${r}: ${t}: No such file or directory
`,exitCode:1}}}if(!y(n))return o.quiet?{stdout:"",stderr:"",exitCode:1}:{stdout:"",stderr:`${r}: ${t}: not in gzip format
`,exitCode:1};let s=n.length,u=D(n),f=u>0?((1-s/u)*100).toFixed(1):"0.0",l=S(n).originalName||(t==="-"?"":t.replace(/\.gz$/,""));return{stdout:`${s.toString().padStart(10)} ${u.toString().padStart(10)} ${f.padStart(5)}% ${l}
`,stderr:"",exitCode:0}}async function U(e,t,o,r){let n;if(t==="-"||t==="")n=Uint8Array.from(e.stdin,s=>s.charCodeAt(0));else{let s=e.fs.resolvePath(e.cwd,t);try{n=await e.fs.readFileBuffer(s)}catch{return{stdout:"",stderr:`${r}: ${t}: No such file or directory
`,exitCode:1}}}if(!y(n))return o.quiet?{stdout:"",stderr:"",exitCode:1}:{stdout:"",stderr:`${r}: ${t}: not in gzip format
`,exitCode:1};try{return x(n),o.verbose?{stdout:"",stderr:`${t}:	OK
`,exitCode:0}:{stdout:"",stderr:"",exitCode:0}}catch(s){let u=s instanceof Error?s.message:"invalid";return{stdout:"",stderr:`${r}: ${t}: ${u}
`,exitCode:1}}}async function v(e,t,o){let r=o==="zcat"?P:o==="gunzip"?q:I;if(w(e))return $(r);let n=z(o,e,L);if(!n.ok)return n.error.stderr.includes("unrecognized option"),n.error;let s=n.result.flags,u=n.result.positional,f=o==="gunzip"||o==="zcat"||s.decompress||s.uncompress,p=o==="zcat"||s.stdout||s.toStdout;if(s.list){u.length===0&&(u=["-"]);let c=`  compressed uncompressed  ratio uncompressed_name
`,a="",m=0;for(let g of u){let h=await O(t,g,s,o);c+=h.stdout,a+=h.stderr,h.exitCode!==0&&(m=h.exitCode)}return{stdout:c,stderr:a,exitCode:m}}if(s.test){u.length===0&&(u=["-"]);let c="",a="",m=0;for(let g of u){let h=await U(t,g,s,o);c+=h.stdout,a+=h.stderr,h.exitCode!==0&&(m=h.exitCode)}return{stdout:c,stderr:a,exitCode:m}}u.length===0&&(u=["-"]);let l="",d="",i=0;for(let c of u){let a=await E(t,c,s,o,f,p);l+=a.stdout,d+=a.stderr,a.exitCode!==0&&(i=a.exitCode)}return{stdout:l,stderr:d,exitCode:i}}var _={name:"gzip",async execute(e,t){return v(e,t,"gzip")}},H={name:"gunzip",async execute(e,t){return v(e,t,"gunzip")}},B={name:"zcat",async execute(e,t){return v(e,t,"zcat")}};export{H as gunzipCommand,_ as gzipCommand,B as zcatCommand};
