-1

My zipInputStream is getting closed after writing the first file itself even though I am not closing any of the streams.

 ZipInputStream zipInputStream = new ZipInputStream(inputStream); 
 ZipEntry zipEntry = zipInputStream.getNextEntry();
  while (zipEntry != null) {

        modelFolderName = <somefoldername>
        modelFileName = <somefilename>

        String FILE_STORAGE_LOCATION = env.getProperty("workspacePath");

        File folder = new File(FILE_STORAGE_LOCATION + "/" + modelFolderName );
        if(!folder.exists()) {
            folder.mkdirs();
        }

        try (FileOutputStream fout=new FileOutputStream(FILE_STORAGE_LOCATION + "/" +  modelFolderName + "/" + modelFileName)) {
            try (BufferedInputStream in = new BufferedInputStream(zipInputStream)) {
              byte[] buffer = new byte[8096];
              while (true) {
                int count = in.read(buffer);
                if (count == -1) {
                  break;
                }
                fout.write(buffer, 0, count);
              }
            }
        }
        zipEntry = zipInputStream.getNextEntry();
    }
Sapna Dhalor
  • 86
  • 1
  • 8
  • Are you missing code? Because you're only checking the first stream entry. Loop over the zipEntries, not over only one entry – DGK Jun 22 '18 at 08:22
  • 1
    In your second try-with-resources you are wrapping `zipInputStream` into a `BufferedInputStream` which gets closed after the try and closes the underlying Stream which is also used for `zipInputStream` – Ben Jun 22 '18 at 08:24
  • Also it appears that you're consuming the entire stream inside your try block rather than just the entry – Jeroen Steenbeeke Jun 22 '18 at 08:24
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the [edit] link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Jun 22 '18 at 08:27
  • Dont just put the exception message in the title of the question. Add the relevant parts of the exception stack trace to the question, and clearly identify which line of your code throws. – GhostCat Jun 22 '18 at 08:28

1 Answers1

2

You are using the syntax try-with-resource. Everything inside the parenthesis will be closed automatically as if there is a finally block to close it.

When in is closed in the implicit finally block, zipInputStream will also be closed because BufferedInputStream is a subclass of FilterInputStream, which closes its source when itself get closed.

(In general, most classes implementing Closable release any resources associated when close is called)

To see the implementation of FilterInputStream::close https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/io/FilterInputStream.java

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Fermat's Little Student
  • 5,549
  • 7
  • 49
  • 70