summaryrefslogtreecommitdiffstats
path: root/pf4j
diff options
context:
space:
mode:
authorDecebal Suiu <decebal.suiu@gmail.com>2017-07-28 23:25:01 +0300
committerDecebal Suiu <decebal.suiu@gmail.com>2017-07-28 23:25:01 +0300
commit6e15379429bf23fd1d0ecc674944d09907ed0890 (patch)
treed9f0d36cac5c3e6788e90e2fdaf717054ccf7cda /pf4j
parentc7a95d5ea039b26497ecd4d8acc800f8a3b4d7bb (diff)
downloadpf4j-6e15379429bf23fd1d0ecc674944d09907ed0890.tar.gz
pf4j-6e15379429bf23fd1d0ecc674944d09907ed0890.zip
Close the input stream after read it and remove redundant util method
Diffstat (limited to 'pf4j')
-rw-r--r--pf4j/src/main/java/ro/fortsoft/pf4j/util/Unzip.java75
1 files changed, 26 insertions, 49 deletions
diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/util/Unzip.java b/pf4j/src/main/java/ro/fortsoft/pf4j/util/Unzip.java
index fc9f6d0..bcf6c9d 100644
--- a/pf4j/src/main/java/ro/fortsoft/pf4j/util/Unzip.java
+++ b/pf4j/src/main/java/ro/fortsoft/pf4j/util/Unzip.java
@@ -67,59 +67,36 @@ public class Unzip {
log.debug("Extract content of '{}' to '{}'", source, destination);
// delete destination file if exists
- removeDirectory(destination);
-
- ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(source));
-
- ZipEntry zipEntry;
- while ((zipEntry = zipInputStream.getNextEntry()) != null) {
- try {
- File file = new File(destination, zipEntry.getName());
-
- // create intermediary directories - sometimes zip don't add them
- File dir = new File(file.getParent());
- dir.mkdirs();
-
- if (zipEntry.isDirectory()) {
- file.mkdirs();
- } else {
- byte[] buffer = new byte[1024];
- int length = 0;
- FileOutputStream fos = new FileOutputStream(file);
-
- while ((length = zipInputStream.read(buffer)) >= 0) {
- fos.write(buffer, 0, length);
- }
-
- fos.close();
- }
- } catch (FileNotFoundException e) {
- log.error("File '{}' not found", zipEntry.getName());
- }
- }
-
- zipInputStream.close();
- }
-
- private boolean removeDirectory(File directory) {
- if (!directory.exists()) {
- return true;
+ if (destination.exists() && destination.isDirectory()) {
+ FileUtils.delete(destination.toPath());
}
- if (!directory.isDirectory()) {
- return false;
- }
-
- File[] files = directory.listFiles();
- for (File file : files) {
- if (file.isDirectory()) {
- removeDirectory(file);
- } else {
- file.delete();
+ try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(source))) {
+ ZipEntry zipEntry;
+ while ((zipEntry = zipInputStream.getNextEntry()) != null) {
+ try {
+ File file = new File(destination, zipEntry.getName());
+
+ // create intermediary directories - sometimes zip don't add them
+ File dir = new File(file.getParent());
+ dir.mkdirs();
+
+ if (zipEntry.isDirectory()) {
+ file.mkdirs();
+ } else {
+ byte[] buffer = new byte[1024];
+ int length;
+ try (FileOutputStream fos = new FileOutputStream(file)) {
+ while ((length = zipInputStream.read(buffer)) >= 0) {
+ fos.write(buffer, 0, length);
+ }
+ }
+ }
+ } catch (FileNotFoundException e) {
+ log.error("File '{}' not found", zipEntry.getName());
+ }
}
}
-
- return directory.delete();
}
}