FileUtils { public static Path expandIfZip(Path filePath) throws IOException { if (!isZipFile(filePath)) { return filePath; } FileTime pluginZipDate = Files.getLastModifiedTime(filePath); String fileName = filePath.getFileName().toString(); String directoryName = fileName.substring(0, fileName.lastIndexOf(".")); Path pluginDirectory = filePath.resolveSibling(directoryName); if (!Files.exists(pluginDirectory) || pluginZipDate.compareTo(Files.getLastModifiedTime(pluginDirectory)) > 0) { Unzip unzip = new Unzip(); unzip.setSource(filePath.toFile()); unzip.setDestination(pluginDirectory.toFile()); unzip.extract(); log.info("Expanded plugin zip '{}' in '{}'", filePath.getFileName(), pluginDirectory.getFileName()); } return pluginDirectory; } private  FileUtils(); static List<String> readLines(Path path, boolean ignoreComments); @Deprecated static void writeLines(Collection<String> lines, File file); static void writeLines(Collection<String> lines, Path path); static void delete(Path path); static List<File> getJars(Path folder); static Path findWithEnding(Path basePath, String... endings); static void optimisticDelete(Path path); static Path expandIfZip(Path filePath); static boolean isZipFile(Path path); static boolean isJarFile(Path path); static Path getPath(Path path, String first, String... more); static Path getPath(URI uri, String first, String... more); static void closePath(Path path); static Path findFile(Path directoryPath, String fileName);  }
@Test public void expandIfZipForZipWithOnlyModuleDescriptor() throws Exception { PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin") .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 { Path extra = pluginsPath.resolve("extra"); assertEquals(extra, FileUtils.expandIfZip(extra)); Path folder = pluginsPath.resolve("folder"); assertEquals(folder, FileUtils.expandIfZip(folder)); }