如何关闭 Zip 文件
我将动态 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将一个块传递给 Zip::ZipFile.open 吗?这将在块退出时关闭它:
== 编辑 ==
根据您的评论,这里有一个工作示例:
另外,我不知道您是否正在运行 unix,但您可以使用 lsof (列出打开的文件)来查找输出文件是否确实打开:
Can you pass a block to Zip::ZipFile.open? This will close it when the block exits:
== EDIT ==
Based on your comments, here's a working example:
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: