Class: Zconv::App

Inherits:
Object
  • Object
show all
Defined in:
lib/zconv/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



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
# File 'lib/zconv/app.rb', line 10

def initialize
  @options = OpenStruct.new(from_code: Encoding.default_external,
                           to_code: Encoding.default_external)

  @parser = OptionParser.new do |parser|
    parser.on('-f ENCODING', '--from-code=ENCODING', :REQUIRED,
              'the encoding of the input') do |enc|
      options.from_code = Encoding.find(enc) if enc
    end

    parser.on('-tENCODING', '--to-code=ENCODING', :REQUIRED,
              'the encoding of the output') do |enc|
      options.to_code = Encoding.find(enc) if enc
    end

    parser.on('-iINPUT', '--input-file=INPUT', :REQUIRED,
              'the input zip file') do |file|
      options.input = file
    end

    parser.on('-oOUTPUT', '--output-file=OUTPUT', :REQUIRED,
              'the output zip file') do |file|
      options.output = file
    end

    parser.on('-h', '--help', 'print this help message') do
      puts parser
      exit
    end
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/zconv/app.rb', line 8

def options
  @options
end

#parserObject

Returns the value of attribute parser.



8
9
10
# File 'lib/zconv/app.rb', line 8

def parser
  @parser
end

Instance Method Details

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zconv/app.rb', line 42

def run
  ARGV << '--help' if ARGV.empty?
  @parser.parse!

  Zip::File.open(options.output, Zip::File::CREATE) do |out_zip|
    Zip::File.open(options.input) do |in_zip|
      in_zip.each do |in_entry|
        out_name = in_entry.name.encode(options.to_code, options.from_code)
        if in_entry.directory?
          out_zip.mkdir(out_name)
        else
          out_zip.get_output_stream(out_name) do |f|
            f.write(in_entry.get_input_stream.read)
          end
        end
      end
    end
  end
end