首先,我要承認我已經在這里和互聯網上閱讀了幾個線程,但我的問題仍然存在,而且似乎有所不同。我有一個 zip 文件,其中包含多個 .txt 文件、目錄、該目錄的子目錄等。里面還有大量的 zip 存檔,里面有 zip、目錄和文件。最深層次的歸檔是 5 個步驟 -> 5 個 zip,一個包含另一個,其中包含不同的文件。我有這個代碼:ZipFile zipFile = new ZipFile(Objects.requireNonNull(this.classLoader.getResource("inputs.zip")).getFile()); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); InputStream stream = zipFile.getInputStream(entry); System.out.println(entry.getName()); processZipFile(stream); }這是 processZipFile:private void processZipFile(InputStream stream) throws IOException { ZipInputStream zipInputStream = new ZipInputStream(stream); ZipEntry zipEntry = zipInputStream.getNextEntry(); while (zipEntry != null) { System.out.print(" /" + zipEntry.getName()); if (zipEntry.getName().endsWith(".zip")) { processZipFile(stream); } zipEntry = zipInputStream.getNextEntry(); }在歸檔級別 3 之前,一切似乎都工作正常,列出了所有目錄、zip、gzip 和子目錄,但當涉及到處理 input.zip/1.zip/2.zip 之類的內容時,它會拋出異常Exception in thread "main" java.util.zip.ZipException: invalid distance too far back正如我在 Java 8 文檔中讀到的ZipInputStream.getNextEntry(): Reads the next ZIP file entry and positions the stream at the beginning of the entry data.因為在獲取入口程序后就會拋出異常。在這種情況下,“2.zip”內的文件相當大 - 800 MB,與最大大小為 3 MB 的其他情況相比 - 我想知道它是否會影響程序。我試圖在不解壓這些拉鏈的情況下完成所有這些事情,這在這里非常重要。我知道這種錯誤通常與損壞的 zip 文件有關,但這些錯誤是完全合法的。所以我的問題是 - 我如何瀏覽所有這些嵌套的 zip 文件?編輯/解決方案:根據 Talex 提出的更改,我已經修復了我的代碼,ZipInputStreams使其能夠在標準InputStreams. 它不再拋出錯誤,但不知怎的,它仍然跳過比 3 級歸檔更深的嵌套 zip(仍然不確定這是否是正確的命名方法,哈哈)。解決方案也很簡單 - 當我將它循環傳遞給我的函數時,我包裝ZipInputStream到另一個。
添加回答
舉報
0/150
提交
取消