내 코드에서 klocwork 코드 분석을 수행했습니다. 다음 오류가 발생했습니다 결국 입력 스트림을 닫았습니다. 오류가 발생하더라도 종료시 'fileInputStream'이 닫히지 않습니다.'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();
}
}
}
}
'zipPath.exists()'테스트는 시간과 공간의 낭비입니다. 'FileInputStream'은 이미이를 테스트하고 이미 catch하고있는 예외를 throw합니다.일을 두 번하는 것은 그것을 추천 할만한 것이 없습니다. – EJP