如何避免在阅读损坏或无效的zip文件时将ZipArchieveInputStream循环到无穷大?

问题描述:

ZipArchieveInputStream从流中读取数据时无限循环 - 无效或损坏的zip文件通过流时传递。如何避免在阅读损坏或无效的zip文件时将ZipArchieveInputStream循环到无穷大?

ZipArchiveInputStream stream = new ZipArchiveInputStream(file, charset, true); 

     while((entry = stream.getNextZipEntry())!=null) { 
       while((read = stream.read(buf)) != -1) { 
         //This loops to infinity for invalid entry 
       } 
     } 

我不知道,你正在使用Apache的百科全书类ZipArchiveInputStream的行为,但也许你应该尝试切换到相应的Java ZipInputStream类。我已经使用它,它的getNextEntry方法返回null,或者在条目损坏时引发异常。

+0

谢谢卡洛斯!我使用它来提取zip文件中存在的实体。这是用'ZipInputStream'实现的吗? – Natasha

+0

是的,看起来它具有相同的功能。 –