use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project bazel by bazelbuild.
the class CompressedTarFunction method decompress.
@Override
public Path decompress(DecompressorDescriptor descriptor) throws RepositoryFunctionException {
Optional<String> prefix = descriptor.prefix();
boolean foundPrefix = false;
try (InputStream decompressorStream = getDecompressorStream(descriptor)) {
TarArchiveInputStream tarStream = new TarArchiveInputStream(decompressorStream);
TarArchiveEntry entry;
while ((entry = tarStream.getNextTarEntry()) != null) {
StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix);
foundPrefix = foundPrefix || entryPath.foundPrefix();
if (entryPath.skip()) {
continue;
}
Path filename = descriptor.repositoryPath().getRelative(entryPath.getPathFragment());
FileSystemUtils.createDirectoryAndParents(filename.getParentDirectory());
if (entry.isDirectory()) {
FileSystemUtils.createDirectoryAndParents(filename);
} else {
if (entry.isSymbolicLink() || entry.isLink()) {
PathFragment linkName = new PathFragment(entry.getLinkName());
boolean wasAbsolute = linkName.isAbsolute();
// Strip the prefix from the link path if set.
linkName = StripPrefixedPath.maybeDeprefix(linkName.getPathString(), prefix).getPathFragment();
if (wasAbsolute) {
// Recover the path to an absolute path as maybeDeprefix() relativize the path
// even if the prefix is not set
linkName = descriptor.repositoryPath().getRelative(linkName).asFragment();
}
if (entry.isSymbolicLink()) {
FileSystemUtils.ensureSymbolicLink(filename, linkName);
} else {
FileSystemUtils.createHardLink(filename, descriptor.repositoryPath().getRelative(linkName));
}
} else {
Files.copy(tarStream, filename.getPathFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
filename.chmod(entry.getMode());
// This can only be done on real files, not links, or it will skip the reader to
// the next "real" file to try to find the mod time info.
Date lastModified = entry.getLastModifiedDate();
filename.setLastModifiedTime(lastModified.getTime());
}
}
}
} catch (IOException e) {
throw new RepositoryFunctionException(e, Transience.TRANSIENT);
}
if (prefix.isPresent() && !foundPrefix) {
throw new RepositoryFunctionException(new IOException("Prefix " + prefix.get() + " was given, but not found in the archive"), Transience.PERSISTENT);
}
return descriptor.repositoryPath();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project camel by apache.
the class TarAggregationStrategyTest method testSplitter.
@Test
public void testSplitter() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:aggregateToTarEntry");
mock.expectedMessageCount(1);
mock.expectedHeaderReceived("foo", "bar");
assertMockEndpointsSatisfied();
Thread.sleep(500);
File[] files = new File("target/out").listFiles();
assertTrue(files != null);
assertTrue("Should be a file in target/out directory", files.length > 0);
File resultFile = files[0];
TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(resultFile));
try {
int fileCount = 0;
for (TarArchiveEntry te = tin.getNextTarEntry(); te != null; te = tin.getNextTarEntry()) {
fileCount = fileCount + 1;
}
assertEquals("Tar file should contains " + TarAggregationStrategyTest.EXPECTED_NO_FILES + " files", TarAggregationStrategyTest.EXPECTED_NO_FILES, fileCount);
} finally {
IOHelper.close(tin);
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project camel by apache.
the class TarAggregationStrategy method addFileToTar.
private void addFileToTar(File source, File file, String fileName) throws IOException, ArchiveException {
File tmpTar = File.createTempFile(source.getName(), null, parentDir);
tmpTar.delete();
if (!source.renameTo(tmpTar)) {
throw new IOException("Could not make temp file (" + source.getName() + ")");
}
FileInputStream fis = new FileInputStream(tmpTar);
TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source));
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
InputStream in = new FileInputStream(file);
// copy the existing entries
ArchiveEntry nextEntry;
while ((nextEntry = tin.getNextEntry()) != null) {
tos.putArchiveEntry(nextEntry);
IOUtils.copy(tin, tos);
tos.closeArchiveEntry();
}
// Add the new entry
TarArchiveEntry entry = new TarArchiveEntry(fileName == null ? file.getName() : fileName);
entry.setSize(file.length());
tos.putArchiveEntry(entry);
IOUtils.copy(in, tos);
tos.closeArchiveEntry();
IOHelper.close(fis, in, tin, tos);
LOG.trace("Deleting temporary file: {}", tmpTar);
FileUtil.deleteFile(tmpTar);
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project camel by apache.
the class TarUtils method getTaredText.
static byte[] getTaredText(String entryName) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(TEXT.getBytes("UTF-8"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TarArchiveOutputStream tos = new TarArchiveOutputStream(baos);
try {
TarArchiveEntry entry = new TarArchiveEntry(entryName);
entry.setSize(bais.available());
tos.putArchiveEntry(entry);
IOHelper.copy(bais, tos);
} finally {
tos.closeArchiveEntry();
IOHelper.close(bais, tos);
}
return baos.toByteArray();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project nifi by apache.
the class FlowFilePackagerV1 method writeAttributesEntry.
private void writeAttributesEntry(final Map<String, String> attributes, final TarArchiveOutputStream tout) throws IOException {
final StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE properties\n SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n");
sb.append("<properties>");
for (final Map.Entry<String, String> entry : attributes.entrySet()) {
final String escapedKey = StringEscapeUtils.escapeXml11(entry.getKey());
final String escapedValue = StringEscapeUtils.escapeXml11(entry.getValue());
sb.append("\n <entry key=\"").append(escapedKey).append("\">").append(escapedValue).append("</entry>");
}
sb.append("</properties>");
final byte[] metaBytes = sb.toString().getBytes(StandardCharsets.UTF_8);
final TarArchiveEntry attribEntry = new TarArchiveEntry(FILENAME_ATTRIBUTES);
attribEntry.setMode(tarPermissions);
attribEntry.setSize(metaBytes.length);
tout.putArchiveEntry(attribEntry);
tout.write(metaBytes);
tout.closeArchiveEntry();
}
Aggregations