ZipFile 内的 ZipFile
我有一个 zip 文件,其中可以包含任意数量的 zip 文件(也递归)。我需要迭代所有这些。
现在,我有一个以 zipInputStream 和 zipFile 作为参数的函数。问题是;如果我在另一个 zip 中得到一个 zip,我将再次递归调用此函数。所以我不知道如何为另一个 zip 文件中的 zip 文件创建 zipFile 对象。有人可以建议一种方法吗?你以前遇到过这个吗?
该片段将如下所示。
private void checkZIP(ZipInputStream zInpStream, ZipFile zf) {
try {
ZipEntry zipEntry = zInpStream.getNextEntry();
while (zipEntry != null) {
String entryName = zipEntry.getName();
if(entryName.endsWith(".zip"))
checkZIP(new ZipInputStream(zf.getInputStream(zipEntry)),<NEW ZIPFILE OBJECT FOR THIS ENTRY>);
//other files parsing apart from zip.
zInpStream.closeEntry();
zipEntry = zInpStream.getNextEntry();
}
zInpStream.close();
} catch(Exception ioe) {
//catching specific exceptions here. But did not want to put al
}
}
编辑:我需要该 zip 文件对象,因为。如果我遇到 XML 文件,我需要创建一个 Document 对象。因此,如果我在 DocumentBuilder
中传递正常的 zipInputStream for parse()
方法,它会关闭流,我无法再次使用它。所以我在 DocumentBuilder parse() 中使用了
(ZipFile object).getInputStream(currentEntryForXML)
。
I have a zip file which can contain any number of zipfiles inside it (recursively also). I need to iterate over all of them.
Right now, i have a function which takes zipInputStream and zipFile as parameters. The problem is; if i get a zip inside another zip, i am calling this function recursively again. So i dont know how to create a zipFile object for zipfiles inside another zipfile. Can someone suggest a way to do it? Have you come across this before.
The snippet will look like this.
private void checkZIP(ZipInputStream zInpStream, ZipFile zf) {
try {
ZipEntry zipEntry = zInpStream.getNextEntry();
while (zipEntry != null) {
String entryName = zipEntry.getName();
if(entryName.endsWith(".zip"))
checkZIP(new ZipInputStream(zf.getInputStream(zipEntry)),<NEW ZIPFILE OBJECT FOR THIS ENTRY>);
//other files parsing apart from zip.
zInpStream.closeEntry();
zipEntry = zInpStream.getNextEntry();
}
zInpStream.close();
} catch(Exception ioe) {
//catching specific exceptions here. But did not want to put al
}
}
EDIT: I need that zip file object because. If i come across an XML file, i need to create a Document object. So if i pass the normal zipInputStream for parse()
method in DocumentBuilder
, it closes the stream and i am not able to use it again. So i used (ZipFile object).getInputStream(currentEntryForXML)
inside DocumentBuilder parse().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法为存档内的 zip 文件创建 ZipFile。但为什么你的方法中需要这个参数呢?如果您只需要一个流,只需使用您的参数 zInpStream 作为构造函数参数,例如
在调用 zInpStream.getNextEntry() 之后,zInpStream 已经位于该条目的开头,并且只会读取该条目,直到调用 getNextEntry() 为止。
编辑。看到你的编辑。看来,如果你阻止流关闭,这对你来说就足够了。只需使用稍作修改的 FilterInputStream,它将 close() 调用委托给 zipInputStream 中的 closeEntry(),而不是关闭。
并在 checkZip 中创建这个守卫:
You can't create a ZipFile for the zip file inside archive. But why you need this argument in your method? If your only need a stream, just use your argument zInpStream as contstructor parameter, like
After call to zInpStream.getNextEntry() zInpStream is already positioned at the beginning of that entry and will read only that entry until getNextEntry() will be called.
Edit. Saw your edit. Seems, if you prevent stream closing, it will be sufficient for you. Just use slightly modified FilterInputStream, which delegates close() call to closeEntry() in zipInputStream, not a close.
And create this guard in checkZip: