Module: Cabriolet::Auto

Defined in:
lib/cabriolet/auto.rb

Overview

Auto-detection and extraction module

Defined Under Namespace

Classes: SimpleExtractor

Class Method Summary collapse

Class Method Details

.detect_format(path) ⇒ Symbol?

Detect format only without parsing

Examples:

format = Cabriolet::Auto.detect_format('file.cab')
# => :cab

Parameters:

  • path (String)

    Path to the file

Returns:

  • (Symbol, nil)

    Detected format symbol or nil



69
70
71
# File 'lib/cabriolet/auto.rb', line 69

def detect_format(path)
  FormatDetector.detect(path)
end

.extract(archive_path, output_dir, **options) ⇒ Hash

Detect format and extract all files automatically

Examples:

Cabriolet::Auto.extract('archive.cab', 'output/')
Cabriolet::Auto.extract('file.chm', 'docs/', parallel: true, workers: 8)

Parameters:

  • archive_path (String)

    Path to the archive

  • output_dir (String)

    Directory to extract to

  • options (Hash)

    Extraction options

Options Hash (**options):

  • :preserve_paths (Boolean) — default: true

    Preserve directory structure

  • :overwrite (Boolean) — default: false

    Overwrite existing files

  • :parallel (Boolean) — default: false

    Use parallel extraction

  • :workers (Integer) — default: 4

    Number of parallel workers

Returns:

  • (Hash)

    Extraction statistics



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cabriolet/auto.rb', line 49

def extract(archive_path, output_dir, **options)
  archive = open(archive_path)

  extractor = if options[:parallel]
                ParallelExtractor.new(archive, output_dir, **options)
              else
                SimpleExtractor.new(archive, output_dir, **options)
              end

  extractor.extract_all
end

.info(path) ⇒ Hash

Get information about an archive without full extraction

Examples:

info = Cabriolet::Auto.info('archive.cab')
# => { format: :cab, file_count: 145, total_size: 52428800, ... }

Parameters:

  • path (String)

    Path to the archive

Returns:

  • (Hash)

    Archive information



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cabriolet/auto.rb', line 81

def info(path)
  archive = open(path)
  format = detect_format(path)

  {
    format: format,
    path: path,
    file_count: archive.files.count,
    total_size: archive.files.sum { |f| f.size || 0 },
    compressed_size: File.size(path),
    compression_ratio: calculate_compression_ratio(archive, path),
    files: archive.files.map { |f| file_info(f) },
  }
end

.open(path, **options) ⇒ Object

Open and parse an archive with automatic format detection

Examples:

archive = Cabriolet::Auto.open('unknown.archive')
archive.files.each { |f| puts f.name }

Parameters:

  • path (String)

    Path to the archive file

  • options (Hash)

    Options to pass to the parser

Returns:

  • (Object)

    Parsed archive object

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cabriolet/auto.rb', line 19

def open(path, **options)
  format = FormatDetector.detect(path)
  unless format
    raise UnsupportedFormatError,
          "Unable to detect format for: #{path}"
  end

  parser_class = FormatDetector.format_to_parser(format)
  unless parser_class
    raise UnsupportedFormatError,
          "No parser available for format: #{format}"
  end

  parser_class.new(**options).parse(path)
end