Module: Omnizip::Converter

Defined in:
lib/omnizip/converter.rb,
lib/omnizip/converter/conversion_registry.rb,
lib/omnizip/converter/conversion_strategy.rb,
lib/omnizip/converter/seven_zip_to_zip_strategy.rb,
lib/omnizip/converter/zip_to_seven_zip_strategy.rb

Overview

Archive format conversion module Provides conversion between different archive formats

Defined Under Namespace

Classes: ConversionRegistry, ConversionStrategy, SevenZipToZipStrategy, ZipToSevenZipStrategy

Class Method Summary collapse

Class Method Details

.batch_convert(sources, target_format: :seven_zip, **options) {|result| ... } ⇒ Array<ConversionResult>

Batch convert multiple archives

Parameters:

  • sources (Array<String>)

    Source file paths

  • target_format (Symbol) (defaults to: :seven_zip)

    Target format (:zip or :seven_zip)

  • options (Hash)

    Conversion options

Yields:

  • (result)

    Optional block called for each conversion

Returns:

  • (Array<ConversionResult>)

    Conversion results



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/omnizip/converter.rb', line 78

def batch_convert(sources, target_format: :seven_zip, **options, &block)
  results = []

  sources.each do |source|
    target = generate_target_path(source, target_format)
    result = convert(source, target, target_format: target_format,
                                     **options)
    results << result
    yield(result) if block
  end

  results
end

.convert(source_path, target_path, **options) ⇒ ConversionResult

Convert archive from one format to another

Parameters:

  • source_path (String)

    Source archive path

  • target_path (String)

    Target archive path

  • options (Hash, ConversionOptions)

    Conversion options

Returns:

  • (ConversionResult)

    Conversion result



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
# File 'lib/omnizip/converter.rb', line 21

def convert(source_path, target_path, **options)
  # Find appropriate strategy first
  strategy_class = ConversionRegistry.find_strategy(source_path,
                                                    target_path)
  unless strategy_class
    raise ArgumentError, "No conversion strategy available for " \
                         "#{source_path} -> #{target_path}"
  end

  # Then validate input files
  unless File.exist?(source_path)
    raise Errno::ENOENT, "Source file not found: #{source_path}"
  end

  # Create options object
  opts = options.is_a?(Models::ConversionOptions) ? options : create_options(**options)
  opts.validate

  # Perform conversion
  strategy = strategy_class.new(source_path, target_path, opts)
  result = strategy.convert

  # Delete source if requested
  File.delete(source_path) if opts.delete_source && File.exist?(source_path)

  result
end

.convert_with_options(source_path, target_path, options) ⇒ ConversionResult

Convert with explicit options object

Parameters:

  • source_path (String)

    Source archive path

  • target_path (String)

    Target archive path

  • options (ConversionOptions)

    Conversion options

Returns:

  • (ConversionResult)

    Conversion result



54
55
56
# File 'lib/omnizip/converter.rb', line 54

def convert_with_options(source_path, target_path, options)
  convert(source_path, target_path, options)
end

.strategiesArray<Class>

Get available conversion strategies

Returns:

  • (Array<Class>)

    List of strategy classes



68
69
70
# File 'lib/omnizip/converter.rb', line 68

def strategies
  ConversionRegistry.strategies
end

.supported?(source_path, target_path) ⇒ Boolean

Check if conversion is supported

Parameters:

  • source_path (String)

    Source archive path

  • target_path (String)

    Target archive path

Returns:

  • (Boolean)

    True if conversion is supported



62
63
64
# File 'lib/omnizip/converter.rb', line 62

def supported?(source_path, target_path)
  ConversionRegistry.supported?(source_path, target_path)
end