Base64 encoded string to file(Ruby on Rails) -


I have a requirement where a zip file is sent to ROR application after encoding it with Base64 encoding. I should decode it, save it as a zipfile and open it and do some operations. They are sending zip file encoded data in the form of a parameter called "POST" by the POST method as a zip Here, what I have done in the code

  'base64' Def get_pdf encoded_data is required 'rubygems' require 'zip / zip' required = Base64.decode64. (Parameter [: code]) file. Open ("# {RAILS_ROOT} /zip_archive/zip_file.zip", "w") {| F | F.write encoded_data} unzip_file ("# {RAILS_ROOT} /zip_archive/zip_file.zip", "# {RAILS_ROOT} / unzip /") ... (using file_path, doing stuff) end DEF unzip_file (file, destination ) Destination = file. Add (destination, file .basinum (file, ".zip")) ZIP :: ZIPFile.Open (file) {| Zip_file | Zip_file.each {| F | F_path = File.join (destination, f.name) FileUtils.mkdir_p (File.dirname (f_path)) zip_file.extract (f, f_path) Until File.exist? (F_path)}} @file_path = Destination end   

However, I can not save the zip file correctly. After saving the file is giving an error on the unziping part.

  Pin :: ZipError (pin end of central directory signature is not found): rubyzip (0.9.4) lib / zip / zip.rb: 1287: `get_e_o_c_d 'in rubyzip (0.9.4 ) Lib / zip / zip.rb: 1235: in `read_e_o_c_d 'rubyzip (0.9.4) lib / zip / rtr: 1260: in' read_from_stream 'rubyzip (0.9.4) lib / zip / zip.rb: 1392 : Rubypip (0.9.4) BB / ZIP / Zip. RB: '139:' Open 'in RubyZip (0.9.4) Lib / Zip / Zip. RB: 1392: In 'Initialized RubyZip' (0.9.4) lib / zip / zip.rb: 1410: `New in 'rubyzip (0.9.4) lib / zip / zip.rb: 1410:' In the open ' : 37: 'app' in app / admin / pdf_controller.rb `unzip_file   

When I tried to open the file outside of the app, the file was not going to be opened

[/ home / prince / desktop / test_project / zip_archive / zip_file.zip] Mid-mid-directory signature not found. Either this file is not a zipfile, or it is a disk of multi-part archive. In the latter case, central directory and zipfile comments will be found on the last disc (disc) of this archive. Zipinfo: /home/prince/Desktop/test_project/zip_archive/zip_file.zip or /home/prince/Desktop/test_project/zip_archive/zip_file.zip.zip The zipfile directory is missing, and can not be found / Home / prince / desktop /test_project/zip_archive/zip_file.zip.zip, duration

I save the file with File.open ("..", "wb") Tried to write content in binary mode, but still there is a single error should i do anything for parameters [: zip] before coding it?

works like a magic open file # to block the file You should write and stop properly, maybe you are getting a wrong path there. If File.exists and gone through decode64, then you should be good

  ruby ​​1.9.2-p0>. Zip = "UEsDBAoAAAAAAKphcT4AAAAAAAAAAAAAAAAFABwAZW1wdHlVVAkAA8 / sgU3P \ n7IFNdXgLAAEE9QEAAAQUAAAAUEsBAh4DCgAAAAAAqmFxPgAAAAAAAAAAAAAA \ nAAUAGAAAAAAAAAAAAKSBAAAAAGVtcHR5VVQFAAPP7IFNdXgLAAEE9QEAAAQU \ nAAAAUEsFBgAAAAABAAEASwAAAD8AAAAAAA == \ n" Ruby 1.9.2-p0 & gt; File.open ('test2.zip', 'wb') {| F | | F.write (Base64.decode64 (zip))} = & gt; 160 ruby-1.9.2-p0 & gt; Zip :: zipfile.open ('test2.zip') {| Z | Z.each {| F | Puts f.name}} below = & gt; [Empty]    

Comments