Class: Zconv::App
- Inherits:
-
Object
- Object
- Zconv::App
- Defined in:
- lib/zconv/app.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#parser ⇒ Object
Returns the value of attribute parser.
Instance Method Summary collapse
-
#initialize ⇒ App
constructor
A new instance of App.
- #run ⇒ Object
Constructor Details
#initialize ⇒ App
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 = 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| .from_code = Encoding.find(enc) if enc end parser.on('-tENCODING', '--to-code=ENCODING', :REQUIRED, 'the encoding of the output') do |enc| .to_code = Encoding.find(enc) if enc end parser.on('-iINPUT', '--input-file=INPUT', :REQUIRED, 'the input zip file') do |file| .input = file end parser.on('-oOUTPUT', '--output-file=OUTPUT', :REQUIRED, 'the output zip file') do |file| .output = file end parser.on('-h', '--help', 'print this help message') do puts parser exit end end end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/zconv/app.rb', line 8 def end |
#parser ⇒ Object
Returns the value of attribute parser.
8 9 10 |
# File 'lib/zconv/app.rb', line 8 def parser @parser end |
Instance Method Details
#run ⇒ Object
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(.output, Zip::File::CREATE) do |out_zip| Zip::File.open(.input) do |in_zip| in_zip.each do |in_entry| out_name = in_entry.name.encode(.to_code, .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 |