如何在Ruby on Rails中解压缩文件?

时间:2023-02-05 23:14:53

I'm uploading a file to the server in Ruby on Rails

我在Ruby on Rails上将文件上传到服务器

Normally, it's a text file and I save it in the model as a 'file' field in a Submission ActiveRecord with other fields such as title of submission.. etc.

通常,它是一个文本文件,我将其作为提交ActiveRecord中的“文件”字段保存在模型中,其他字段包括提交标题等。

However, the user can also submit a zip file. In this case the zipfile should unzipped and for each file in the zip a new Submission should be created with the same text fields, but current file.

但是,用户也可以提交zip文件。在这种情况下,zip文件应该解压缩,并且对于zip中的每个文件,应该使用相同的文本字段创建新的提交,但是当前文件。

How can I accomplish this?

我怎么能做到这一点?

I looked at unzip examples on the net, but most use a directory to unzip the files to. I'm not sure if I need that as in the current create method of SubmissionsController, a file object is received and I presume the path to save the file to is automatically generated when the Submission save method is called. So I was thinking that maybe I should unzip the zipfile in memory to get an array of file objects and then create a new Submission with each file object but same fields and then let ActiveRecord generate the file paths for each one when it saves them to the database. I might be wrong here, because I'm kind of new to Rails and Ruby.

我在网上查看了解压缩示例,但大多数都使用目录来解压缩文件。我不确定我是否需要它,因为在当前的SubmissionsController创建方法中,接收到一个文件对象,我假设保存文件的路径是在调用Submission save方法时自动生成的。所以我想也许我应该将zip文件解压缩到内存中以获取文件对象数组,然后使用每个文件对象创建一个新的Submission但是相同的字段,然后让ActiveRecord在将它们保存到每个文件时生成每个文件路径的文件路径。数据库。我可能在这里错了,因为我对Rails和Ruby有点新意。

3 个解决方案

#1


34  

I'd use the rubyzip gem. Specifically this part: https://github.com/rubyzip/rubyzip/blob/master/lib/zip/filesystem.rb

我会使用rubyzip gem。特别是这部分:https://github.com/rubyzip/rubyzip/blob/master/lib/zip/filesystem.rb

It creates an artificial file system in memory mirroring the contents of the zip file. Here's an example based of the example from the docs:

它在内存中创建一个人工文件系统,镜像zip文件的内容。以下是基于文档示例的示例:

Rubyzip interface changed!!! No need to do require "zip/zip" and Zip prefix in class names removed.

Rubyzip界面改变了!无需执行“zip / zip”和删除类名中的Zip前缀。

require 'zip'

Zip::File.open("my.zip") do |zipfile|
  zipfile.each do |file|
    # do something with file
  end
end

In your case, just put the name of the uploaded tempfile where my.zip is in the example, and you can loop through the contents and do your regular operations on them.

在您的情况下,只需将上传的临时文件的名称放在my.zip所在的示例中,您就可以遍历内容并对它们进行常规操作。

#2


25  

From the RubyZip project page:

从RubyZip项目页面:

Rubyzip interface changed!!! No need to do require "zip/zip" and Zip prefix in class names removed.

Rubyzip界面改变了!无需执行“zip / zip”和删除类名中的Zip前缀。

So, the example code from @ben-lee should be updated to something like this:

因此,@ ben-lee的示例代码应该更新为:

require 'zip'

Zip::File.open("my.zip") do |zipfile|
  zipfile.each do |file|
    # do something with file
  end
end

#3


12  

Extract Zip files in Ruby

Once you've installed the rubyzip gem, you can use this method to extract zip files:

一旦安装了rubyzip gem,就可以使用此方法提取zip文件:

require 'zip'

def extract_zip(file, destination)
  FileUtils.mkdir_p(destination)

  Zip::File.open(file) do |zip_file|
    zip_file.each do |f|
      fpath = File.join(destination, f.name)
      zip_file.extract(f, fpath) unless File.exist?(fpath)
    end
  end
end

You use it like this:

你这样使用它:

extract_zip(zip_path, extract_destination)

#1


34  

I'd use the rubyzip gem. Specifically this part: https://github.com/rubyzip/rubyzip/blob/master/lib/zip/filesystem.rb

我会使用rubyzip gem。特别是这部分:https://github.com/rubyzip/rubyzip/blob/master/lib/zip/filesystem.rb

It creates an artificial file system in memory mirroring the contents of the zip file. Here's an example based of the example from the docs:

它在内存中创建一个人工文件系统,镜像zip文件的内容。以下是基于文档示例的示例:

Rubyzip interface changed!!! No need to do require "zip/zip" and Zip prefix in class names removed.

Rubyzip界面改变了!无需执行“zip / zip”和删除类名中的Zip前缀。

require 'zip'

Zip::File.open("my.zip") do |zipfile|
  zipfile.each do |file|
    # do something with file
  end
end

In your case, just put the name of the uploaded tempfile where my.zip is in the example, and you can loop through the contents and do your regular operations on them.

在您的情况下,只需将上传的临时文件的名称放在my.zip所在的示例中,您就可以遍历内容并对它们进行常规操作。

#2


25  

From the RubyZip project page:

从RubyZip项目页面:

Rubyzip interface changed!!! No need to do require "zip/zip" and Zip prefix in class names removed.

Rubyzip界面改变了!无需执行“zip / zip”和删除类名中的Zip前缀。

So, the example code from @ben-lee should be updated to something like this:

因此,@ ben-lee的示例代码应该更新为:

require 'zip'

Zip::File.open("my.zip") do |zipfile|
  zipfile.each do |file|
    # do something with file
  end
end

#3


12  

Extract Zip files in Ruby

Once you've installed the rubyzip gem, you can use this method to extract zip files:

一旦安装了rubyzip gem,就可以使用此方法提取zip文件:

require 'zip'

def extract_zip(file, destination)
  FileUtils.mkdir_p(destination)

  Zip::File.open(file) do |zip_file|
    zip_file.each do |f|
      fpath = File.join(destination, f.name)
      zip_file.extract(f, fpath) unless File.exist?(fpath)
    end
  end
end

You use it like this:

你这样使用它:

extract_zip(zip_path, extract_destination)