use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project halyard by spinnaker.
the class BackupService method tarHalconfig.
private void tarHalconfig(String halconfigDir, String halconfigTar) throws IOException {
FileOutputStream tarOutput = null;
BufferedOutputStream bufferedTarOutput = null;
TarArchiveOutputStream tarArchiveOutputStream = null;
IOException fatalCleanup = null;
try {
tarOutput = new FileOutputStream(new File(halconfigTar));
bufferedTarOutput = new BufferedOutputStream(tarOutput);
tarArchiveOutputStream = new TarArchiveOutputStream(bufferedTarOutput);
tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
TarArchiveOutputStream finalTarArchiveOutputStream = tarArchiveOutputStream;
Arrays.stream(new File(halconfigDir).listFiles()).filter(Objects::nonNull).forEach(f -> addFileToTar(finalTarArchiveOutputStream, f.getAbsolutePath(), ""));
} catch (HalException e) {
log.info("HalException caught during tar operation", e);
throw e;
} catch (IOException e) {
log.info("IOException caught during tar operation", e);
throw new HalException(Problem.Severity.FATAL, "Failed to backup halconfig: " + e.getMessage(), e);
} finally {
if (tarArchiveOutputStream != null) {
try {
tarArchiveOutputStream.finish();
tarArchiveOutputStream.close();
} catch (IOException e) {
fatalCleanup = e;
}
}
if (bufferedTarOutput != null) {
bufferedTarOutput.close();
}
if (tarOutput != null) {
tarOutput.close();
}
}
if (fatalCleanup != null) {
throw fatalCleanup;
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project halyard by spinnaker.
the class BackupService method addFileToTar.
private void addFileToTar(TarArchiveOutputStream tarArchiveOutputStream, String path, String base) {
File file = new File(path);
String fileName = file.getName();
if (Arrays.stream(omitPaths).anyMatch(s -> s.equals(fileName))) {
return;
}
String tarEntryName = String.join("/", base, fileName);
try {
if (file.isFile()) {
TarArchiveEntry tarEntry = new TarArchiveEntry(file, tarEntryName);
tarArchiveOutputStream.putArchiveEntry(tarEntry);
IOUtils.copy(new FileInputStream(file), tarArchiveOutputStream);
tarArchiveOutputStream.closeArchiveEntry();
} else if (file.isDirectory()) {
Arrays.stream(file.listFiles()).filter(Objects::nonNull).forEach(f -> addFileToTar(tarArchiveOutputStream, f.getAbsolutePath(), tarEntryName));
} else {
log.warn("Unknown file type: " + file + " - skipping addition to tar archive");
}
} catch (IOException e) {
throw new HalException(Problem.Severity.FATAL, "Unable to file " + file.getName() + " to archive entry: " + tarEntryName + " " + e.getMessage(), e);
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project halyard by spinnaker.
the class GitProfileReader method readArchiveProfile.
@Override
public InputStream readArchiveProfile(String artifactName, String version, String profileName) throws IOException {
Path profilePath = Paths.get(profilePath(artifactName, version, profileName));
ByteArrayOutputStream os = new ByteArrayOutputStream();
TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os);
ArrayList<Path> filePathsToAdd = java.nio.file.Files.walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS).filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new));
for (Path path : filePathsToAdd) {
TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString());
int permissions = FileModeUtils.getFileMode(Files.getPosixFilePermissions(path));
permissions = FileModeUtils.setFileBit(permissions);
tarEntry.setMode(permissions);
tarArchive.putArchiveEntry(tarEntry);
IOUtils.copy(Files.newInputStream(path), tarArchive);
tarArchive.closeArchiveEntry();
}
tarArchive.finish();
tarArchive.close();
return new ByteArrayInputStream(os.toByteArray());
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project halyard by spinnaker.
the class LocalDiskProfileReader method readArchiveProfileFrom.
public InputStream readArchiveProfileFrom(Path profilePath) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os);
ArrayList<Path> filePathsToAdd = java.nio.file.Files.walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS).filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new));
for (Path path : filePathsToAdd) {
TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString());
tarArchive.putArchiveEntry(tarEntry);
IOUtils.copy(Files.newInputStream(path), tarArchive);
tarArchive.closeArchiveEntry();
}
tarArchive.finish();
tarArchive.close();
return new ByteArrayInputStream(os.toByteArray());
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project mycore by MyCoRe-Org.
the class MCRTarServlet method createContainer.
@Override
protected TarArchiveOutputStream createContainer(ServletOutputStream sout, String comment) {
LOGGER.info("Constructing tar archive: {}", comment);
TarArchiveOutputStream tout = new TarArchiveOutputStream(sout, "UTF8");
tout.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
tout.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
return tout;
}
Aggregations