diff --git a/src/main/java/net/lingala/zip4j/ZipFile.java b/src/main/java/net/lingala/zip4j/ZipFile.java index c19b0b1d..eb2b5182 100755 --- a/src/main/java/net/lingala/zip4j/ZipFile.java +++ b/src/main/java/net/lingala/zip4j/ZipFile.java @@ -99,7 +99,6 @@ public class ZipFile implements Closeable { private ExecutorService executorService; private int bufferSize = InternalZipConstants.BUFF_SIZE; private List openInputStreams = new ArrayList<>(); - private boolean zipFileClosed = false; /** * Creates a new ZipFile instance with the zip file at the location specified in zipFile. @@ -1079,15 +1078,10 @@ public List getSplitZipFiles() throws ZipException { */ @Override public void close() throws IOException { - if (zipFileClosed) { - return; - } - for (InputStream inputStream : openInputStreams) { inputStream.close(); } openInputStreams.clear(); - zipFileClosed = true; } /** diff --git a/src/test/java/net/lingala/zip4j/MiscZipFileIT.java b/src/test/java/net/lingala/zip4j/MiscZipFileIT.java index 11bed8ab..c7700ec0 100644 --- a/src/test/java/net/lingala/zip4j/MiscZipFileIT.java +++ b/src/test/java/net/lingala/zip4j/MiscZipFileIT.java @@ -22,6 +22,7 @@ import java.nio.charset.Charset; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Set; import java.util.concurrent.ThreadFactory; @@ -608,6 +609,29 @@ public void testCloseZipFileByTryWithResourceSuccessfullyClosesAllOpenStreams() assertInputStreamsAreClosed(inputStreams); } + @Test + public void testCloseZipFileMultipleTimesClosesAllStreams() throws IOException { + ZipFile zipFile = new ZipFile(generatedZipFile); + zipFile.addFiles(FILES_TO_ADD); + List inputStreams = new ArrayList<>(); + + // First open the inputstreams + for (FileHeader fileHeader : zipFile.getFileHeaders()) { + inputStreams.add(zipFile.getInputStream(fileHeader)); + } + // Close it for the first time + zipFile.close(); + assertInputStreamsAreClosed(inputStreams); + + //Now open an inputstream again + InputStream inputStream = zipFile.getInputStream(zipFile.getFileHeader(FILES_TO_ADD.get(0).getName())); + + // Closing it now again should close the inputstream as well + zipFile.close(); + + assertInputStreamsAreClosed(Collections.singletonList(inputStream)); + } + private void assertInputStreamsAreClosed(List inputStreams) { for (InputStream inputStream : inputStreams) { try {