in src/main/java/co/elastic/support/util/ArchiveUtils.java [71:122]
public static void extractArchive(String filename, String targetDir) throws IOException {
ArchiveInputStream ais = null;
try {
InputStream inputStream = new FileInputStream(new File(filename));
if (filename.endsWith(".zip")) {
ais = new ZipArchiveInputStream(inputStream);
} else {
logger.error(Constants.CONSOLE, "Unsupported archive type");
return;
}
ArchiveEntry entry = ais.getNextEntry();
String archiveDir = entry.getName();
while ((entry = ais.getNextEntry()) != null) {
String newPath = entry.getName().replace(archiveDir, "");
if (newPath.endsWith(".zip") || newPath.endsWith(".tar") | newPath.endsWith("tar.gz")) {
String nestedArch = entryToDisk(ais, targetDir, newPath);
String nestedTargetDir = nestedArch.substring(nestedArch.lastIndexOf(SystemProperties.fileSeparator));
extractArchive(nestedArch, nestedTargetDir);
new File(nestedArch).delete();
} else if (entry.isDirectory()) {
File f = new File(targetDir + SystemProperties.fileSeparator + newPath);
boolean created = f.mkdir();
if (!created) {
System.out.printf("Unable to create directory '%s', during extraction of archive contents.\n",
f.getAbsolutePath());
}
} else {
entryToDisk(ais, targetDir, newPath);
/*
* int count;
* byte data[] = new byte[bufferSize];
* FileOutputStream fos = new FileOutputStream(new File(targetDir +
* SystemProperties.fileSeparator + newPath), false);
* try (BufferedOutputStream dest = new BufferedOutputStream(fos, bufferSize)) {
* while ((count = ais.read(data, 0, bufferSize)) != -1) {
* dest.write(data, 0, count);
* }
* }
*/
}
}
System.out.println("Extract completed successfully!");
} catch (IOException e) {
logger.error(e);
} finally {
if (ais != null) {
ais.close();
}
}
}