aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Lövdahl <slovdahl@hibox.fi>2020-01-13 13:39:40 +0200
committerDecebal Suiu <decebal.suiu@gmail.com>2020-01-13 13:39:40 +0200
commite524ac2ca5c18e469c9855da2d2c36ef8a32d1a5 (patch)
treeafb1185b2113882878a1509e62414b79016d07eb
parent77fb6914b47a93b805baae8a6ff819f686367144 (diff)
downloadpf4j-e524ac2ca5c18e469c9855da2d2c36ef8a32d1a5.tar.gz
pf4j-e524ac2ca5c18e469c9855da2d2c36ef8a32d1a5.zip
Throw IOException if `mkdirs()` fails while unpacking ZIP file (#359) (#362)
-rw-r--r--pf4j/src/main/java/org/pf4j/util/Unzip.java39
-rw-r--r--pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java23
2 files changed, 41 insertions, 21 deletions
diff --git a/pf4j/src/main/java/org/pf4j/util/Unzip.java b/pf4j/src/main/java/org/pf4j/util/Unzip.java
index afde37a..198cf7d 100644
--- a/pf4j/src/main/java/org/pf4j/util/Unzip.java
+++ b/pf4j/src/main/java/org/pf4j/util/Unzip.java
@@ -78,29 +78,32 @@ public class Unzip {
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);
- }
+ File file = new File(destination, zipEntry.getName());
+
+ // create intermediary directories - sometimes zip don't add them
+ File dir = new File(file.getParent());
+
+ mkdirsOrThrow(dir);
+
+ if (zipEntry.isDirectory()) {
+ mkdirsOrThrow(file);
+ } 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());
}
}
}
}
+ private static void mkdirsOrThrow(File dir) throws IOException {
+ if (!dir.exists() && !dir.mkdirs()) {
+ throw new IOException("Failed to create directory " + dir);
+ }
+ }
+
}
diff --git a/pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java b/pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java
index 5f9a3a7..7bf06c6 100644
--- a/pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java
+++ b/pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java
@@ -21,6 +21,7 @@ import org.pf4j.plugin.PluginZip;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -31,18 +32,34 @@ public class FileUtilsTest {
Path pluginsPath;
@Test
- public void expandIfZip() throws Exception {
+ public void expandIfZipForZipWithOnlyModuleDescriptor() throws Exception {
PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
- .pluginVersion("1.2.3")
- .build();
+ .pluginVersion("1.2.3")
+ .build();
Path unzipped = FileUtils.expandIfZip(pluginZip.path());
assertEquals(pluginZip.unzippedPath(), unzipped);
assertTrue(Files.exists(unzipped.resolve("plugin.properties")));
+ }
+
+ @Test
+ public void expandIfZipForZipWithResourceFile() throws Exception {
+ PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-second-plugin-1.2.3.zip"), "myPlugin")
+ .pluginVersion("1.2.3")
+ .addFile(Paths.get("classes/META-INF/plugin-file"), "plugin")
+ .build();
+ Path unzipped = FileUtils.expandIfZip(pluginZip.path());
+ assertEquals(pluginZip.unzippedPath(), unzipped);
+ assertTrue(Files.exists(unzipped.resolve("classes/META-INF/plugin-file")));
+ }
+
+ @Test
+ public void expandIfZipNonZipFiles() throws Exception {
// File without .suffix
Path extra = pluginsPath.resolve("extra");
assertEquals(extra, FileUtils.expandIfZip(extra));
+
// Folder
Path folder = pluginsPath.resolve("folder");
assertEquals(folder, FileUtils.expandIfZip(folder));