Browse Source

Close the input stream after read it and remove redundant util method

pull/159/head
Decebal Suiu 7 years ago
parent
commit
6e15379429
  1. 37
      pf4j/src/main/java/ro/fortsoft/pf4j/util/Unzip.java

37
pf4j/src/main/java/ro/fortsoft/pf4j/util/Unzip.java

@ -67,10 +67,11 @@ public class Unzip {
log.debug("Extract content of '{}' to '{}'", source, destination); log.debug("Extract content of '{}' to '{}'", source, destination);
// delete destination file if exists // delete destination file if exists
removeDirectory(destination); if (destination.exists() && destination.isDirectory()) {
FileUtils.delete(destination.toPath());
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(source)); }
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(source))) {
ZipEntry zipEntry; ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) { while ((zipEntry = zipInputStream.getNextEntry()) != null) {
try { try {
@ -84,42 +85,18 @@ public class Unzip {
file.mkdirs(); file.mkdirs();
} else { } else {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int length = 0; int length;
FileOutputStream fos = new FileOutputStream(file); try (FileOutputStream fos = new FileOutputStream(file)) {
while ((length = zipInputStream.read(buffer)) >= 0) { while ((length = zipInputStream.read(buffer)) >= 0) {
fos.write(buffer, 0, length); fos.write(buffer, 0, length);
} }
}
fos.close();
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
log.error("File '{}' not found", zipEntry.getName()); log.error("File '{}' not found", zipEntry.getName());
} }
} }
zipInputStream.close();
}
private boolean removeDirectory(File directory) {
if (!directory.exists()) {
return true;
} }
if (!directory.isDirectory()) {
return false;
}
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
removeDirectory(file);
} else {
file.delete();
}
}
return directory.delete();
} }
} }

Loading…
Cancel
Save