我正在嘗試使用 Kotlin 和 ZipInputStream 將壓縮文件讀入 ByteArrayOutputStream()val f = File("/path/to/zip/myFile.zip")val zis = ZipInputStream(FileInputStream(f))//loop through all entries in the zipped filevar entry = zis.nextEntrywhile(entry != null) { val baos = ByteArrayOutputStream() //read the entry into a ByteArrayOutputStream zis.use{ it.copyTo(baos) } val bytes = baos.toByteArray() System.out.println(bytes[0]) zis.closeEntry() //error thrown here on first iteration entry = zis.nextEntry}我得到的錯誤是:java.io.IOException: Stream closed at java.util.zip.ZipInputStream.ensureOpen(ZipInputStream.java:67) at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:139) <the code above>我想可能zis.use在讀取條目的內容后已經關閉了條目,所以我刪除了zis.closeEntry(),但是在嘗試獲取下一個條目時它產生了相同的錯誤我知道zis.use是安全的并保證輸入流已關閉,但我希望它只關閉條目而不是整個流。打印了整個字節數組后,我知道只有 zip 中的第一個文件正在被讀取 zis.use有沒有一種好方法可以在 kotlin 中讀取 ZipInputStream 中的所有條目?
添加回答
舉報
0/150
提交
取消