Skip to content

Commit

Permalink
#327 Fix bug to close inputstreams when ZipFile is closed multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed Jun 25, 2021
1 parent f290b66 commit 861f03e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
6 changes: 0 additions & 6 deletions src/main/java/net/lingala/zip4j/ZipFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public class ZipFile implements Closeable {
private ExecutorService executorService;
private int bufferSize = InternalZipConstants.BUFF_SIZE;
private List<InputStream> openInputStreams = new ArrayList<>();
private boolean zipFileClosed = false;

/**
* Creates a new ZipFile instance with the zip file at the location specified in zipFile.
Expand Down Expand Up @@ -1079,15 +1078,10 @@ public List<File> getSplitZipFiles() throws ZipException {
*/
@Override
public void close() throws IOException {
if (zipFileClosed) {
return;
}

for (InputStream inputStream : openInputStreams) {
inputStream.close();
}
openInputStreams.clear();
zipFileClosed = true;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/net/lingala/zip4j/MiscZipFileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<InputStream> 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<InputStream> inputStreams) {
for (InputStream inputStream : inputStreams) {
try {
Expand Down

0 comments on commit 861f03e

Please sign in to comment.