- * Note this is specific to how WildFly is archived. The first directory is assumed to be the base home directory - * and will returned. - *
- * - * @param archiveFile the archive to uncompress, can be a {@code .zip} or {@code .tar.gz} - * @param targetDir the directory to extract the zip file to - * - * @return the path to the extracted directory - * - * @throws java.io.IOException if an I/O error occurs - */ - public static Path uncompress(final Path archiveFile, final Path targetDir) throws IOException { - return uncompress(archiveFile, targetDir, false); - } - - /** - * Unzips the zip file to the target directory. - *- * Note this is specific to how WildFly is archived. The first directory is assumed to be the base home directory - * and will returned. - *
- * - * @param archiveFile the archive to uncompress, can be a {@code .zip} or {@code .tar.gz} - * @param targetDir the directory to extract the zip file to - * @param replaceIfExists if {@code true} replace the existing files if they exist - * - * @return the path to the extracted directory - * - * @throws java.io.IOException if an I/O error occurs - */ - @SuppressWarnings("WeakerAccess") - public static Path uncompress(final Path archiveFile, final Path targetDir, final boolean replaceIfExists) throws IOException { - final Path archive = getArchive(archiveFile); - - Path firstDir = null; - - try (ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(new BufferedInputStream(Files.newInputStream(archive)))) { - ArchiveEntry entry; - while ((entry = in.getNextEntry()) != null) { - final Path extractTarget = targetDir.resolve(entry.getName()); - if (!replaceIfExists && Files.exists(extractTarget)) { - if (entry.isDirectory() && firstDir == null) { - firstDir = extractTarget; - } - continue; - } - if (entry.isDirectory()) { - final Path dir = Files.createDirectories(extractTarget); - if (firstDir == null) { - firstDir = dir; - } - } else { - Files.createDirectories(extractTarget.getParent()); - Files.copy(in, extractTarget); - } - } - return firstDir == null ? targetDir : firstDir; - } catch (ArchiveException e) { - throw new IOException(e); - } - } - - private static Path getArchive(final Path path) throws IOException { - final Path result; - // Get the extension - final String fileName = path.getFileName().toString(); - final String loweredFileName = fileName.toLowerCase(Locale.ENGLISH); - if (loweredFileName.endsWith(".gz")) { - String tempFileName = fileName.substring(0, loweredFileName.indexOf(".gz")); - final int index = tempFileName.lastIndexOf('.'); - if (index > 0) { - result = Files.createTempFile(tempFileName.substring(0, index), tempFileName.substring(index)); - } else { - result = Files.createTempFile(tempFileName.substring(0, index), ""); - } - try (CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream(new BufferedInputStream(Files.newInputStream(path)))) { - Files.copy(in, result, StandardCopyOption.REPLACE_EXISTING); - } catch (CompressorException e) { - throw new IOException(e); - } - } else { - result = path; - } - return result; - } -} diff --git a/pom.xml b/pom.xml index 3805350f..91a589c0 100644 --- a/pom.xml +++ b/pom.xml @@ -100,19 +100,18 @@