简体   繁体   English

如何在不创建任何新文件的情况下访问zip文件目录中的文件?

[英]How does one access a file within a directory in a zip file without creating any new files?

I'm working on a java project that requires me to access a file within multiple embedded zip files and directories. 我正在研究一个Java项目,该项目需要我访问多个嵌入式zip文件和目录中的文件。

For example, archive1.zip/archive1/archive2.zip/archive2/directory1/file_that_I_need.txt. 例如,archive1.zip / archive1 / archive2.zip / archive2 / directory1 / file_that_I_need.txt。

It would be a lot easier if when each zip file was extracted, it would immediately list its contents but instead there's a folder inside that contains all the contents. 如果提取每个zip文件时会立即列出其内容,但是里面有一个包含所有内容的文件夹,则会容易得多。

The examples I found online deal with zip files that, when extracted, contain the files they need to access but I can't find any that deal with accessing files within a directory in a zip file. 我在网上找到的示例处理的是zip文件,这些文件在提取后包含它们需要访问的文件,但找不到与处理zip文件中的目录中的文件有关的文件。 Any advice on this would be great. 任何建议,这将是巨大的。

Thanks! 谢谢!

Given the prohibition against creating new files, you're pretty much stuck with ZipInputStream . 鉴于禁止创建新文件的限制,您几乎陷入了ZipInputStream的困境。 When you find the ZipEntry that corresponds to the embedded archive, you then read its stream to find the actual file. 当找到与嵌入式存档相对应的ZipEntry ,您将读取其流以查找实际文件。 You can proceed recursively through as many levels of archives as you want. 您可以根据需要递归浏览所有级别的存档。

This works OK if you're looking to process a single file. 如果您要处理单个文件,则此方法正常。 However, re-reading the archives for multiple files can be expensive. 但是,重新读取多个文件的存档可能会很昂贵。 A better solution is to at least open the outer archive as a ZipFile , which memory-maps the actual file. 更好的解决方案是至少以ZipFile的形式打开外部档案,该档案对实际文件进行内存映射。

If you can then extract the contained archives into a temporary directory and open them as ZipFile s as well, you'll probably see a big speed increase (as long as you're pulling multiple files from each embedded archive). 然后,如果您可以将包含的存档提取到一个临时目录中,并以ZipFile的形式打开它们,则可能会看到速度大大提高(只要从每个嵌入式存档中提取多个文件)。

You might also look at http://truezip.java.net/ I've used an older version of it, and its quite a bit more powerful than the support that's built into Java. 您可能还会看到http://truezip.java.net/,我使用了较旧的版本,它比Java内置的支持功能强大得多。 I think there is also an Apache Commons library for reading files from within nested archive structures. 我认为还有一个Apache Commons库,用于从嵌套存档结构中读取文件。

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

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