oxiarc-cli 0.3.2

Command-line interface for OxiArc archive operations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! OxiArc CLI - The Oxidized Archiver
//!
//! A Pure Rust archive utility supporting ZIP, GZIP, TAR, LZH, XZ, 7z, CAB, LZ4, Zstd, Bzip2, Brotli, and Snappy formats.

mod commands;
mod style;
mod utils;
mod windows;

use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
use clap_complete::{Shell, generate};
use commands::{
    CompressionLevel, OutputFormat, SortBy, cmd_add, cmd_convert, cmd_create, cmd_detect,
    cmd_extract, cmd_info, cmd_list, cmd_man, cmd_test,
};
use std::io;
use std::path::PathBuf;
use style::{ColorChoice, Styler};

#[derive(Parser)]
#[command(name = "oxiarc")]
#[command(
    author,
    version,
    about = "The Oxidized Archiver - Pure Rust archive utility"
)]
#[command(long_about = "
OxiArc is a Pure Rust implementation of common archive formats.
Supported formats: ZIP, GZIP, TAR, LZH, XZ, 7z, LZ4, Zstd, Bzip2, Brotli, Snappy

Examples:
  oxiarc list archive.zip
  oxiarc list archive.7z
  oxiarc extract archive.zip
  oxiarc extract archive.7z
  oxiarc extract data.xz
  oxiarc extract data.lz4
  oxiarc extract data.zst
  oxiarc extract data.bz2
  oxiarc extract data.br
  oxiarc extract data.sz
  oxiarc create archive.zip file1.txt file2.txt
  oxiarc create data.xz file.txt
  oxiarc create data.lz4 file.txt
  oxiarc create data.bz2 file.txt
  oxiarc create data.br file.txt
  oxiarc create data.sz file.txt
  oxiarc convert archive.lzh output.zip
  oxiarc convert archive.7z output.zip
  oxiarc test archive.lzh
  oxiarc info archive.7z
")]
pub struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Control color output
    #[arg(long, value_enum, default_value_t = ColorChoice::Auto, global = true)]
    color: ColorChoice,
}

#[derive(Subcommand)]
enum Commands {
    /// List contents of an archive
    #[command(alias = "l")]
    List {
        /// Archive file to list
        archive: PathBuf,

        /// Show verbose output
        #[arg(short, long)]
        verbose: bool,

        /// Output as JSON (machine-readable)
        #[arg(short, long)]
        json: bool,

        /// Display as directory tree
        #[arg(short = 'T', long)]
        tree: bool,

        /// Sort entries by: name, size, date, or ratio
        #[arg(short, long, value_enum, default_value = "name")]
        sort: SortBy,

        /// Reverse sort order
        #[arg(short = 'r', long)]
        reverse: bool,

        /// Include only files matching pattern (glob syntax: *.txt, src/**/*)
        #[arg(short = 'I', long)]
        include: Vec<String>,

        /// Exclude files matching pattern (glob syntax)
        #[arg(short = 'X', long)]
        exclude: Vec<String>,

        /// Continue on corruption when reading the archive (emit warnings to stderr)
        #[arg(long)]
        lenient: bool,

        /// Refuse to extract entries exceeding this memory limit (e.g. 100M, 512K, 1G)
        #[arg(long, value_parser = crate::utils::parse_byte_size)]
        memory_limit: Option<u64>,
    },

    /// Extract files from an archive
    #[command(alias = "x")]
    #[command(group = clap::ArgGroup::new("overwrite_mode").multiple(false))]
    Extract {
        /// Archive file to extract (use "-" for stdin)
        archive: String,

        /// Output directory (use "-" for stdout when extracting single-file formats)
        #[arg(short, long, default_value = ".")]
        output: String,

        /// Files to extract (all if empty)
        files: Vec<String>,

        /// Include only files matching pattern (glob syntax: *.txt, src/**/*)
        #[arg(short = 'I', long)]
        include: Vec<String>,

        /// Exclude files matching pattern (glob syntax)
        #[arg(short = 'X', long)]
        exclude: Vec<String>,

        /// Show verbose output
        #[arg(short, long)]
        verbose: bool,

        /// Show progress bar
        #[arg(short = 'P', long, default_value = "true")]
        progress: bool,

        /// Format hint for stdin (gzip, xz, bz2, lz4, zst, br, snappy)
        #[arg(short, long, value_enum)]
        format: Option<OutputFormatArg>,

        /// Always overwrite existing files (default behavior)
        #[arg(long, group = "overwrite_mode")]
        overwrite: bool,

        /// Skip extraction if file already exists
        #[arg(long, group = "overwrite_mode")]
        skip_existing: bool,

        /// Prompt user before overwriting each file
        #[arg(long, group = "overwrite_mode")]
        prompt: bool,

        /// Preserve file timestamps (modification time)
        #[arg(short = 't', long)]
        preserve_timestamps: bool,

        /// Preserve file permissions (Unix mode)
        #[arg(long)]
        preserve_permissions: bool,

        /// Preserve all metadata (timestamps and permissions)
        #[arg(short = 'p', long)]
        preserve: bool,

        /// Dry run: show what would be extracted without writing files
        #[arg(short = 'n', long)]
        dry_run: bool,

        /// Password for encrypted entries (prompts interactively if omitted)
        #[arg(long)]
        password: Option<String>,

        /// Refuse to extract entries whose basename is a Windows reserved name
        /// (CON, NUL, COM1.., LPT1..). Default: append '_' to the stem.
        #[arg(long)]
        strict_names: bool,

        /// Continue on corruption (CRC mismatch, bad TAR checksum, etc.) with warnings instead of errors
        #[arg(long)]
        lenient: bool,

        /// Refuse to extract entries exceeding this memory limit (e.g. 100M, 512K, 1G)
        #[arg(long, value_parser = crate::utils::parse_byte_size)]
        memory_limit: Option<u64>,
    },

    /// Test archive integrity
    #[command(alias = "t")]
    Test {
        /// Archive file to test
        archive: PathBuf,

        /// Show verbose output
        #[arg(short, long)]
        verbose: bool,
    },

    /// Create a new archive
    #[command(alias = "c")]
    Create {
        /// Output archive file (use "-" for stdout)
        archive: String,

        /// Files to add to the archive
        files: Vec<PathBuf>,

        /// Archive format (required for stdout: gzip, xz, bz2, lz4, zst, br, snappy)
        #[arg(short, long, value_enum)]
        format: Option<OutputFormatArg>,

        /// Compression level
        #[arg(short = 'l', long, value_enum, default_value = "normal")]
        compression: CompressionLevelArg,

        /// Files smaller than this (bytes) are stored, not compressed (ZIP only; 0 disables)
        #[arg(long, default_value_t = 0)]
        compress_threshold: u64,

        /// Verbose output
        #[arg(short, long)]
        verbose: bool,

        /// Dry run: show what would be done without creating the archive
        #[arg(short = 'n', long)]
        dry_run: bool,
    },

    /// Add files to an existing archive (ZIP, TAR, LZH)
    Add {
        /// Existing archive file to append to
        archive: PathBuf,

        /// Files to add to the archive
        files: Vec<PathBuf>,

        /// Compression level (used when the archive format supports it)
        #[arg(short = 'l', long, value_enum, default_value = "normal")]
        compression: CompressionLevelArg,

        /// Verbose output
        #[arg(short, long)]
        verbose: bool,

        /// Dry run: show planned changes without modifying the archive
        #[arg(short = 'n', long)]
        dry_run: bool,
    },

    /// Show information about an archive
    #[command(alias = "i")]
    Info {
        /// Archive file to inspect
        archive: PathBuf,
    },

    /// Detect archive format
    Detect {
        /// File to detect
        file: PathBuf,
    },

    /// Convert archive to another format
    Convert {
        /// Input archive file
        input: PathBuf,

        /// Output archive file
        output: PathBuf,

        /// Output format (zip, tar, gzip, lzh, xz, lz4, br, snappy) - auto-detected from extension if not specified
        #[arg(short, long, value_enum)]
        format: Option<OutputFormatArg>,

        /// Compression level for output
        #[arg(short = 'l', long, value_enum, default_value = "normal")]
        compression: CompressionLevelArg,

        /// Verbose output
        #[arg(short, long)]
        verbose: bool,
    },

    /// Generate shell completion scripts
    #[command(hide = true)]
    Completion {
        /// Shell to generate completions for
        #[arg(value_enum)]
        shell: Shell,
    },

    /// Generate man pages for all subcommands
    Man {
        /// Directory to write man pages into (default: ./man)
        out_dir: Option<PathBuf>,
    },
}

/// Output archive format (for clap ValueEnum).
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
enum OutputFormatArg {
    /// ZIP archive
    Zip,
    /// TAR archive
    Tar,
    /// GZIP compressed file
    Gzip,
    /// LZH archive
    Lzh,
    /// XZ compressed file
    Xz,
    /// LZ4 compressed file
    Lz4,
    /// Bzip2 compressed file
    Bz2,
    /// Zstandard compressed file
    Zst,
    /// Brotli compressed file
    Br,
    /// Snappy compressed file
    Snappy,
}

impl From<OutputFormatArg> for OutputFormat {
    fn from(arg: OutputFormatArg) -> Self {
        match arg {
            OutputFormatArg::Zip => OutputFormat::Zip,
            OutputFormatArg::Tar => OutputFormat::Tar,
            OutputFormatArg::Gzip => OutputFormat::Gzip,
            OutputFormatArg::Lzh => OutputFormat::Lzh,
            OutputFormatArg::Xz => OutputFormat::Xz,
            OutputFormatArg::Lz4 => OutputFormat::Lz4,
            OutputFormatArg::Bz2 => OutputFormat::Bz2,
            OutputFormatArg::Zst => OutputFormat::Zst,
            OutputFormatArg::Br => OutputFormat::Br,
            OutputFormatArg::Snappy => OutputFormat::Snappy,
        }
    }
}

/// Compression level (for clap ValueEnum).
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Default)]
enum CompressionLevelArg {
    /// Store without compression
    Store,
    /// Fast compression
    Fast,
    /// Normal compression (default)
    #[default]
    Normal,
    /// Best compression
    Best,
}

impl From<CompressionLevelArg> for CompressionLevel {
    fn from(arg: CompressionLevelArg) -> Self {
        match arg {
            CompressionLevelArg::Store => CompressionLevel::Store,
            CompressionLevelArg::Fast => CompressionLevel::Fast,
            CompressionLevelArg::Normal => CompressionLevel::Normal,
            CompressionLevelArg::Best => CompressionLevel::Best,
        }
    }
}

fn main() {
    let cli = Cli::parse();
    let styler = Styler::new(cli.color);

    let result = match cli.command {
        Commands::List {
            archive,
            verbose,
            json,
            tree,
            sort,
            reverse,
            include,
            exclude,
            lenient,
            memory_limit,
        } => {
            let options = commands::list::ListOptions {
                verbose,
                json,
                tree,
                sort_by: sort,
                reverse,
                include: &include,
                exclude: &exclude,
                lenient,
                memory_limit,
            };
            cmd_list(&archive, &options, &styler)
        }
        Commands::Extract {
            archive,
            output,
            files,
            include,
            exclude,
            verbose,
            progress,
            format,
            overwrite,
            skip_existing,
            prompt,
            preserve_timestamps,
            preserve_permissions,
            preserve,
            dry_run,
            password,
            strict_names,
            lenient,
            memory_limit,
        } => cmd_extract(
            commands::extract::ExtractArgs {
                archive: &archive,
                output: &output,
                files: &files,
                include: &include,
                exclude: &exclude,
                verbose,
                progress,
                format_hint: format.map(Into::into),
                overwrite,
                skip_existing,
                prompt,
                preserve_timestamps,
                preserve_permissions,
                preserve,
                dry_run,
                password,
                strict_names,
                lenient,
                memory_limit,
            },
            &styler,
        ),
        Commands::Test { archive, verbose } => cmd_test(&archive, verbose),
        Commands::Create {
            archive,
            files,
            format,
            compression,
            compress_threshold,
            verbose,
            dry_run,
        } => cmd_create(
            &archive,
            &files,
            format.map(Into::into),
            compression.into(),
            compress_threshold,
            verbose,
            dry_run,
        ),
        Commands::Add {
            archive,
            files,
            compression,
            verbose,
            dry_run,
        } => cmd_add(&archive, &files, compression.into(), verbose, dry_run),
        Commands::Info { archive } => cmd_info(&archive, &styler),
        Commands::Detect { file } => cmd_detect(&file, &styler),
        Commands::Convert {
            input,
            output,
            format,
            compression,
            verbose,
        } => cmd_convert(
            &input,
            &output,
            format.map(Into::into),
            compression.into(),
            verbose,
        ),
        Commands::Completion { shell } => {
            let mut cmd = Cli::command();
            generate(shell, &mut cmd, "oxiarc", &mut io::stdout());
            return;
        }
        Commands::Man { out_dir } => {
            let cmd = Cli::command();
            if let Err(e) = cmd_man(cmd, out_dir) {
                eprintln!("{}: {e}", styler.error("Error"));
                std::process::exit(1);
            }
            return;
        }
    };

    if let Err(e) = result {
        eprintln!("{}: {}", styler.error("Error"), e);
        std::process::exit(1);
    }
}