简体   繁体   English

如何使用rubyzip gem在压缩目录中找到特定条目?

[英]How do I find a specific entry inside a zipped directory using the rubyzip gem?

I have a zip file named test.zip which contains a directory named invoice. 我有一个名为test.zip的zip文件,其中包含一个名为invoice的目录。 Inside the invoice directory there are documents each with different names. 发票目录中有每个文件都有不同的名称。 I would like to find a specific document named summary.txt which is inside the invoice directory. 我想在发票目录中找到一个名为summary.txt的特定文档。

I am able to get a handle to test.zip using the following: 我可以使用以下方法获取test.zip的句柄:

zip = Zip::ZipFile.open("/path/to/test.zip")

but when I use 但是当我使用

zip.find_entry("summary.txt")

I get nil. 我没钱

On the other hand, if summary.txt is not inside the the invoices directory, but rather at the root of the zip file itself, the find_entry method as described above seems to work. 另一方面,如果summary.txt不在invoices目录中,而是在zip文件本身的根目录中,则如上所述的find_entry方法似乎有效。

It seems that somehow I must navigate down into the invoices directory before searching for summary.txt. 似乎我必须以某种方式向下导航到发票目录,然后再搜索summary.txt。

Is this correct? 这个对吗? If so, how do I do it? 如果是这样,我该怎么办? If not, what I am I doing wrong. 如果不是,我在做什么错。

您需要指定文件的完整路径:

zip.find_entry 'invoices/summary.txt'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM