Browse Source

Throw IOException if `mkdirs()` fails while unpacking ZIP file (#359) (#362)

pull/366/head
Sebastian Lövdahl 5 years ago committed by Decebal Suiu
parent
commit
e524ac2ca5
  1. 39
      pf4j/src/main/java/org/pf4j/util/Unzip.java
  2. 23
      pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java

39
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);
}
}
}

23
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));

Loading…
Cancel
Save