Class: Omnizip::Converter::ZipToSevenZipStrategy

Inherits:
ConversionStrategy show all
Defined in:
lib/omnizip/converter/zip_to_seven_zip_strategy.rb

Overview

Convert ZIP archives to 7-Zip format

Instance Attribute Summary

Attributes inherited from ConversionStrategy

#options, #source_path, #target_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ConversionStrategy

#initialize

Constructor Details

This class inherits a constructor from Omnizip::Converter::ConversionStrategy

Class Method Details

.can_convert?(source, target) ⇒ Boolean

Check if can convert

Parameters:

  • source (String)

    Source file

  • target (String)

    Target file

Returns:

  • (Boolean)

    True if can convert



50
51
52
# File 'lib/omnizip/converter/zip_to_seven_zip_strategy.rb', line 50

def self.can_convert?(source, target)
  source.end_with?(".zip") && target.end_with?(".7z")
end

Instance Method Details

#convertConversionResult

Perform ZIP to 7z conversion

Returns:

  • (ConversionResult)

    Conversion result



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/omnizip/converter/zip_to_seven_zip_strategy.rb', line 11

def convert
  start_time = Time.now
  entry_count = 0

  Dir.mktmpdir("omnizip_convert_zip") do |tmp|
    # Extract the ZIP through the native reader; directories are
    # implied by extracted file paths
    Omnizip::Formats::Zip.extract(source_path, tmp)

    writer = Omnizip::Formats::SevenZip::Writer.new(target_path,
                                                    writer_options)
    Dir.glob(File.join(tmp, "**", "*")).each do |path|
      next if File.directory?(path)

      entry_count += 1
      writer.add_file(path, path.delete_prefix("#{tmp}/"))
    end
    writer.write
  end

  create_result(start_time, entry_count)
end

#source_formatSymbol

Get source format

Returns:

  • (Symbol)

    Source format (:zip)



36
37
38
# File 'lib/omnizip/converter/zip_to_seven_zip_strategy.rb', line 36

def source_format
  :zip
end

#target_formatSymbol

Get target format

Returns:

  • (Symbol)

    Target format (:seven_zip)



42
43
44
# File 'lib/omnizip/converter/zip_to_seven_zip_strategy.rb', line 42

def target_format
  :seven_zip
end