ResourceContent content = resource.adapt(ResourceContent.class);
IllegalStateAssertion.assertNotNull(content, "Cannot obtain resource content for: " + artefact);
try {
ArchiveInputStream ais;
if ("tar.gz".equals(artefact.getType())) {
InputStream inputStream = content.getContent();
ais = new TarArchiveInputStream(new GZIPInputStream(inputStream));
} else {
InputStream inputStream = content.getContent();
ais = new ArchiveStreamFactory().createArchiveInputStream(artefact.getType(), inputStream);
}
ArchiveEntry entry = null;
boolean needContainerHome = homeDir == null;
while ((entry = ais.getNextEntry()) != null) {
File targetFile;
if (needContainerHome) {
targetFile = new File(targetDir, entry.getName());
} else {
targetFile = new File(homeDir, entry.getName());
}
if (!entry.isDirectory()) {
File parentDir = targetFile.getParentFile();
IllegalStateAssertion.assertTrue(parentDir.exists() || parentDir.mkdirs(), "Cannot create target directory: " + parentDir);
FileOutputStream fos = new FileOutputStream(targetFile);
IOUtils.copy(ais, fos);
fos.close();
if (needContainerHome && homeDir == null) {
File currentDir = parentDir;
while (!currentDir.getParentFile().equals(targetDir)) {
currentDir = currentDir.getParentFile();
}
homeDir = currentDir;
}
}
}
ais.close();
} catch (RuntimeException rte) {
throw rte;
} catch (Exception ex) {
throw new IllegalStateException("Cannot extract artefact: " + artefact, ex);
}