Base64 编码字符串到文件(Ruby on Rails)

发布于 2024-10-22 07:42:55 字数 2290 浏览 6 评论 0原文

我有一个要求,即使用 Base64 编码对 zip 文件进行编码后将其发送到 ROR 应用程序。我应该解码它,将其保存为 zip 文件并解压缩并执行一些操作。他们通过 POST 方法将 zip 文件编码数据作为名为 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] 做些什么吗?

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

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

But, I am not able to save the zip file correctly. The file after saving is giving error on the unzipping part.

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'

When I tried to open the file outside the app also, the file was not getting opened saying

[/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.

I tried saving the file with File.open("..", "wb") to write the contents in the binary mode, but then also the same error occurs. Should I do anything to the params[:zip] before decoding it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无尽的现实 2024-10-29 07:42:55

就像魅力一样。 File#open 块应该正确写入并关闭文件,您可能只是在某个地方有错误的路径。如果File.exists? 和decode64 都通过了,那么你应该没问题。

ruby-1.9.2-p0 > zip = "UEsDBAoAAAAAAKphcT4AAAAAAAAAAAAAAAAFABwAZW1wdHlVVAkAA8/sgU3P\n7IFNdXgLAAEE9QEAAAQUAAAAUEsBAh4DCgAAAAAAqmFxPgAAAAAAAAAAAAAA\nAAUAGAAAAAAAAAAAAKSBAAAAAGVtcHR5VVQFAAPP7IFNdXgLAAEE9QEAAAQU\nAAAAUEsFBgAAAAABAAEASwAAAD8AAAAAAA==\n"
ruby-1.9.2-p0 > File.open('test2.zip', 'wb') {|f| f.write(Base64.decode64(zip))}
 => 160 
ruby-1.9.2-p0 > Zip::ZipFile.open('test2.zip') {|z| z.each {|f| puts f.name}}
empty
 => [empty] 

Works like a charm. The File#open block should properly write and close the file, you're probably just having the wrong path in there somewhere. If the File.exists? and the decode64 went through, you should be good.

ruby-1.9.2-p0 > zip = "UEsDBAoAAAAAAKphcT4AAAAAAAAAAAAAAAAFABwAZW1wdHlVVAkAA8/sgU3P\n7IFNdXgLAAEE9QEAAAQUAAAAUEsBAh4DCgAAAAAAqmFxPgAAAAAAAAAAAAAA\nAAUAGAAAAAAAAAAAAKSBAAAAAGVtcHR5VVQFAAPP7IFNdXgLAAEE9QEAAAQU\nAAAAUEsFBgAAAAABAAEASwAAAD8AAAAAAA==\n"
ruby-1.9.2-p0 > File.open('test2.zip', 'wb') {|f| f.write(Base64.decode64(zip))}
 => 160 
ruby-1.9.2-p0 > Zip::ZipFile.open('test2.zip') {|z| z.each {|f| puts f.name}}
empty
 => [empty] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文