下面这段代码的作用是压缩,会用到ZipOutputStream
ZipOutputStream zipOutputStream = null;
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath));
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("zipPath error " + "", e);
} finally {
if (null != zipOutputStream) {
try {
zipOutputStream.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
错误放大版1:
ZipOutputStream zipOutputStream = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipPath);
throw new RuntimeException();
zipOutputStream = new ZipOutputStream(fileOutputStream);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("zipPath error " + "", e);
} finally {
if (null != zipOutputStream) {
try {
zipOutputStream.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
错误放大版2:
ZipOutputStream zipOutputStream = null;
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath),null);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("zipPath error " + "", e);
} finally {
if (null != zipOutputStream) {
try {
zipOutputStream.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
这里我有一个疑问,在new FileOutputStream(zipPath)的过程中如果已经打开了这个文件,但是在new ZipOutputStream过程中出错了,这个时候zipOutputStream并没有创建成功,所以在finally中不会调用close方法,造成的结果就是这个文件不会被关闭。
所以这种关闭方式是不是会有漏洞,还是我的理由有问题?