Base64エンコーディングでエンコードした後、zipファイルをRORアプリケーションに送信する必要があります。それをデコードし、zipファイルとして保存して解凍し、いくつかの操作を行う必要があります。zip
彼らは、 POST メソッドを介して呼び出されるパラメーターとして、zip ファイルでエンコードされたデータを送信しています。これが私がコードで行ったことです。
require 'rubygems'
require 'zip/zip'
require 'base64'
def get_pdf
encoded_data = Base64.decode64(params[:zip])
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}/unzipped/")
...(using @file_path, do stuff)
end
def unzip_file (file, destination)
destination = File.join(destination, File.basename(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) unless File.exist?(f_path)
}
}
@file_path = destination
end
しかし、zipファイルを正しく保存できません。保存後のファイルは、解凍部分でエラーになっています。
Zip::ZipError (Zip end of central directory signature not found):
rubyzip (0.9.4) lib/zip/zip.rb:1287:in `get_e_o_c_d'
rubyzip (0.9.4) lib/zip/zip.rb:1235:in `read_e_o_c_d'
rubyzip (0.9.4) lib/zip/zip.rb:1260:in `read_from_stream'
rubyzip (0.9.4) lib/zip/zip.rb:1392:in `initialize'
rubyzip (0.9.4) lib/zip/zip.rb:1392:in `open'
rubyzip (0.9.4) lib/zip/zip.rb:1392:in `initialize'
rubyzip (0.9.4) lib/zip/zip.rb:1410:in `new'
rubyzip (0.9.4) lib/zip/zip.rb:1410:in `open'
app/controllers/pdf_controller.rb:37:in `unzip_file'
app/controllers/pdf_controller.rb:13:in `get_pdf'
アプリの外でもファイルを開こうとすると、ファイルが開かれませんでした
[/home/prince/Desktop/test_project/zip_archive/zip_file.zip]
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
zipinfo: cannot find zipfile directory in one of /home/prince/Desktop/test_project/zip_archive/zip_file.zip or
/home/prince/Desktop/test_project/zip_archive/zip_file.zip.zip, and cannot find /home/prince/Desktop/test_project/zip_archive/zip_file.zip.ZIP, period.
バイナリモードで内容を書き込むでファイルを保存してみFile.open("..", "wb")
ましたが、同様のエラーが発生します。params[:zip]
デコードする前に何かする必要がありますか?