// some zips can't be opened via ZipFile in JDK6, as the central directory
// contains either non-latin entries or the compression type can't be handled
// the workaround is to iterate over the stream and not the directory
- FileInputStream fis;
+ FileInputStream fis = null;
+ ThresholdInputStream zis = null;
try {
fis = new FileInputStream(file);
- ThresholdInputStream zis = ZipHelper.openZipStream(fis);
+ zis = ZipHelper.openZipStream(fis);
ze = new ZipInputStreamZipEntrySource(zis);
} catch (IOException e2) {
+ if (zis != null) {
+ try {
+ zis.close();
+ } catch (IOException e3) {
+ throw new InvalidOperationException("Can't open the specified file: '" + file + "'"+
+ " and couldn't close the file input stream", e);
+ }
+ } else if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e3) {
+ throw new InvalidOperationException("Can't open the specified file: '" + file + "'"+
+ " and couldn't close the file input stream", e);
+ }
+ }
throw new InvalidOperationException("Can't open the specified file: '" + file + "'", e);
}
}
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
SlideShow<?,?> ppt = SlideShowFactory.create(f, null, true);
ppt.close();
}
+
+ @Test
+ public void testClosingStreamOnException() throws IOException {
+ InputStream is = OpenXML4JTestDataSamples.openSampleStream("dcterms_bug_56479.zip");
+ File tmp = File.createTempFile("poi-test-truncated-zip", "");
+ OutputStream os = new FileOutputStream(tmp);
+ for (int i = 0; i < 100; i++) {
+ os.write(is.read());
+ }
+ os.flush();
+ os.close();
+ is.close();
+
+ try {
+ OPCPackage.open(tmp, PackageAccess.READ);
+ } catch (Exception e) {
+ }
+ assertTrue("Can't delete tmp file", tmp.delete());
+
+ }
}