Search in sources :

Example 11 with TarArchiveEntry

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();
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Path(com.google.devtools.build.lib.vfs.Path) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Date(java.util.Date) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)

Example 12 with TarArchiveEntry

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);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) File(java.io.File) FileInputStream(java.io.FileInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 13 with TarArchiveEntry

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);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 14 with TarArchiveEntry

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 15 with TarArchiveEntry

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();
}
Also used : Map(java.util.Map) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)200 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)99 File (java.io.File)86 FileInputStream (java.io.FileInputStream)55 IOException (java.io.IOException)55 FileOutputStream (java.io.FileOutputStream)45 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)38 InputStream (java.io.InputStream)31 BufferedInputStream (java.io.BufferedInputStream)29 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)25 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Path (java.nio.file.Path)21 BufferedOutputStream (java.io.BufferedOutputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)18 OutputStream (java.io.OutputStream)17 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)15 HashMap (java.util.HashMap)11 GZIPInputStream (java.util.zip.GZIPInputStream)11