use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project RecordManager2 by moravianlibrary.
the class TarGzUtils method extract.
public static void extract(File tarFile, File destFile) throws IOException {
logger.info("Extracting file: " + tarFile.getName());
if (destFile.exists())
throw new FileAlreadyExistsException(destFile.getAbsolutePath());
if (!destFile.mkdir())
throw new IOException("Can't make directory for " + destFile.getAbsolutePath());
TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile))));
TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
while (tarEntry != null) {
// create a file with the same name as the tarEntry
File destPath = new File(destFile, tarEntry.getName());
if (tarEntry.isDirectory()) {
if (!destPath.mkdirs())
logger.info("Can't make directory for " + destPath.getAbsolutePath());
} else {
if (!destPath.createNewFile())
logger.info("Name of file already exists " + destPath.getAbsolutePath());
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
byte[] btoRead = new byte[1024];
int len;
while ((len = tarIn.read(btoRead)) != -1) {
bout.write(btoRead, 0, len);
}
bout.close();
}
tarEntry = tarIn.getNextTarEntry();
}
tarIn.close();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry 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.TarArchiveEntry 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());
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.TarArchiveEntry in project zalenium by zalando.
the class DockerSeleniumRemoteProxy method copyVideos.
@SuppressWarnings("ResultOfMethodCallIgnored")
@VisibleForTesting
void copyVideos(final String containerId) {
boolean videoWasCopied = false;
TarArchiveInputStream tarStream = new TarArchiveInputStream(containerClient.copyFiles(containerId, "/videos/"));
try {
TarArchiveEntry entry;
while ((entry = tarStream.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
String fileExtension = entry.getName().substring(entry.getName().lastIndexOf('.'));
testInformation.setFileExtension(fileExtension);
File videoFile = new File(testInformation.getVideoFolderPath(), testInformation.getFileName());
File parent = videoFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
OutputStream outputStream = new FileOutputStream(videoFile);
IOUtils.copy(tarStream, outputStream);
outputStream.close();
videoWasCopied = true;
LOGGER.info(String.format("%s Video file copied to: %s/%s", getId(), testInformation.getVideoFolderPath(), testInformation.getFileName()));
}
} catch (IOException e) {
// This error happens in k8s, but the file is ok, nevertheless the size is not accurate
boolean isPipeClosed = e.getMessage().toLowerCase().contains("pipe closed");
if (ContainerFactory.getIsKubernetes().get() && isPipeClosed) {
LOGGER.info(String.format("%s Video file copied to: %s/%s", getId(), testInformation.getVideoFolderPath(), testInformation.getFileName()));
} else {
LOGGER.warn(getId() + " Error while copying the video", e);
}
ga.trackException(e);
} finally {
if (!videoWasCopied) {
testInformation.setVideoRecorded(false);
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project syndesis by syndesisio.
the class ProjectGeneratorTest method generate.
// *****************************
// Helpers
// *****************************
private Path generate(Integration integration, ProjectGeneratorConfiguration generatorConfiguration, TestResourceManager resourceManager) throws IOException {
final IntegrationProjectGenerator generator = new ProjectGenerator(generatorConfiguration, resourceManager, getMavenProperties());
try (InputStream is = generator.generate(integration)) {
Path ret = testFolder.newFolder("integration-project").toPath();
try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
TarArchiveEntry tarEntry = tis.getNextTarEntry();
// tarIn is a TarArchiveInputStream
while (tarEntry != null) {
// create a file with the same name as the tarEntry
File destPath = new File(ret.toFile(), tarEntry.getName());
if (tarEntry.isDirectory()) {
destPath.mkdirs();
} else {
destPath.getParentFile().mkdirs();
destPath.createNewFile();
try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) {
IOUtils.copy(tis, bout);
}
}
tarEntry = tis.getNextTarEntry();
}
}
return ret;
}
}
Aggregations