Following is part of the some code. I need to close out resources in finally clause. Do I need to call closeEntry() first or close()? I am getting some error messages.
Error closing the zipoutjava.io.IOException: Stream closed at
java.util.zip.ZipOutputStream.ensureOpen(ZipOutputStream.java:70) at
java.util.zip.ZipOutputStream.closeEntry(ZipOutputStream.java:189)
The code
ZipOutputStream zos = null;
try{
ZipEntry entry = new ZipEntry("file.csv")
zipout.putNextEntry(entry);
csvBeanWriter = new CsvBeanWriter(writer,
CsvPreference.STANDARD_PREFERENCE);
csvBeanWriter.writeHeader(header);
for (Book book : bookList) {
csvBeanWriter.write(book, header);
csvBeanWriterTest.write(book, header);
}
} catch (Exception e) {
logger.error("Export of package data failed: "
+ e);
} finally {
if (zipout != null) {
try {
zos.closeEntry();
zos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("Error closing the zos"
+ e);
}
}
}
Consider the scheme of using zipout
:
zipout = // open zipout someway
// do something with it
zipout.close();
So, it the block, where we do something with zipout
you should create and close entries:
ZipEntry z = ...
// do something with it
zipout.closeEntry();
The resulting scheme is:
zipout = ...
ZipEntry z1 = ...
zipout.putNextEntry(z1);
// write something to zipout
zipout.closeEntry();
ZipEntry z2 = ...
zipout.putNextEntry(z2);
// write something to zipout
zipout.closeEntry();
//...
ZipEntry zN = ...
zipout.putNextEntry(zN);
// write something to zipout
zipout.closeEntry();
zipout.close();
So, you should firstly close entry, then zipout
.
Example code: https://community.oracle.com/thread/2097141?start=0