我已经对我的代码进行了 klocwork 代码分析。我收到以下错误我最终关闭了输入流即使这样我也收到错误'fileInputStream'在退出时没有关闭。
以下是一段代码
LOGGER.log(Level.INFO, "Inside unzipDownloadedFile method");
File fileDirectory = new File(destDir);
FileInputStream fileInputStream = null;
// buffer for read and write data to file
byte[] buffer = new byte[1024];
ZipInputStream zipInputStream = null;
File zipPath = new File(zipFilePath);
FileOutputStream fileOutputStream = null;
// create output directory if it doesn't exist
if (!fileDirectory.exists()) {
fileDirectory.mkdirs();
}
if (zipPath != null) {
if (zipPath.exists()) {
try {
fileInputStream = new FileInputStream(zipFilePath);
zipInputStream = new ZipInputStream(fileInputStream);
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
File newFile = new File(destDir + File.separator
+ fileName);
// create directories for sub directories in zip
new File(newFile.getParent()).mkdirs();
fileOutputStream = new FileOutputStream(newFile);
int zipStream = 0;
while ((zipStream = zipInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, zipStream);
}
fileOutputStream.close();
// close this ZipEntry
zipInputStream.closeEntry();
zipEntry = zipInputStream.getNextEntry();
}
fileInputStream.close();
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
try {
// close last ZipEntry
if (zipInputStream != null) {
zipInputStream.closeEntry();
zipInputStream.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}