如何关闭 Zip 文件

发布于 2024-08-11 05:47:51 字数 359 浏览 2 评论 0原文

我将动态 zip 文件位置从数据库传递给 def。我想将文件解压到临时位置,提取其中的 xml 报告文件,应用 xslt 样式表,将其作为 rhtml 复制到视图目录进行渲染,然后删除临时提取的 xml 文件。该功能工作正常(rhtml 文件每次都会被覆盖并呈现),除了每次执行都是从同一个父 zip 中提取,并且提取的 xml 无法删除,这让我相信第一次执行没有关闭父文件zip(松开手柄)。因此,后续执行将从第一个执行的 zip 中提取 xml。我尝试过“Zip::ZipFile.close”、“zipFile = Zip::ZipFile.open(fileLocation); zipFile.close”、“File.close(fileLocation)”和其他排列。 任何帮助将不胜感激。

I'm passing a dynamic zip file location to a def from a database. I want to unzip the file to a temp location, extract the xml report file inside, apply an xslt stylesheet, copy it as an rhtml to a view directory for rendering, and delete the temp extracted xml file. The functionality is working fine (the rhtml file is overwritten each time and renders) except it is extracting from the same parent zip for each execution and the extracted xml can not be deleted which leads me to believe that the first execution is not closing the parent zip (releasing its handle). Therefore, subsequent executions are extracting the xml from the first zip executed. I've tried "Zip::ZipFile.close", "zipFile = Zip::ZipFile.open(fileLocation); zipFile.close","File.close(fileLocation)", and other permutations.
Any help would be appreciated.

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

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

发布评论

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

评论(1

地狱即天堂 2024-08-18 05:47:51

您可以将一个块传递给 Zip::ZipFile.open 吗?这将在块退出时关闭它:

Zip::ZipFile.open(file_name) do |zip_file|
  zip_file.extract('report.xml', '/tmp')
end

# zip file is closed at this point
# apply_xslt
# copy rhtml to app/views/...
# etc

== 编辑 ==

根据您的评论,这里有一个工作示例:

require 'rubygems'
require 'zip/zip'
require 'fileutils'

zip_file_name = 'test.zip'
out_dir = 'tmp_for_zip'
FileUtils.mkdir_p out_dir

Zip::ZipFile.open(zip_file_name) do |zip_file|
  report_name = File.basename(zip_file.name).gsub('zip', 'xml')
  out = File.join(out_dir, report_name)
  zip_file.extract(report_name, out) unless File.exists?(out)
  puts "extracted #{report_name} to #{out}"
end

另外,我不知道您是否正在运行 unix,但您可以使用 lsof (列出打开的文件)来查找输出文件是否确实打开:

lsof | grep your_file_name

Can you pass a block to Zip::ZipFile.open? This will close it when the block exits:

Zip::ZipFile.open(file_name) do |zip_file|
  zip_file.extract('report.xml', '/tmp')
end

# zip file is closed at this point
# apply_xslt
# copy rhtml to app/views/...
# etc

== EDIT ==

Based on your comments, here's a working example:

require 'rubygems'
require 'zip/zip'
require 'fileutils'

zip_file_name = 'test.zip'
out_dir = 'tmp_for_zip'
FileUtils.mkdir_p out_dir

Zip::ZipFile.open(zip_file_name) do |zip_file|
  report_name = File.basename(zip_file.name).gsub('zip', 'xml')
  out = File.join(out_dir, report_name)
  zip_file.extract(report_name, out) unless File.exists?(out)
  puts "extracted #{report_name} to #{out}"
end

Also, I don't know if you are running a unix, but you can use lsof (list open files) to find out if the file is actually open:

lsof | grep your_file_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文