Java Code Examples for org.apache.commons.compress.archivers.tar.TarArchiveEntry#setMode()

The following examples show how to use org.apache.commons.compress.archivers.tar.TarArchiveEntry#setMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlowFilePackagerV1.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private void writeContentEntry(final TarArchiveOutputStream tarOut, final InputStream inStream, final long fileSize) throws IOException {
    final TarArchiveEntry entry = new TarArchiveEntry(FILENAME_CONTENT);
    entry.setMode(tarPermissions);
    entry.setSize(fileSize);
    tarOut.putArchiveEntry(entry);
    final byte[] buffer = new byte[512 << 10];//512KB
    int bytesRead = 0;
    while ((bytesRead = inStream.read(buffer)) != -1) { //still more data to read
        if (bytesRead > 0) {
            tarOut.write(buffer, 0, bytesRead);
        }
    }

    copy(inStream, tarOut);
    tarOut.closeArchiveEntry();
}
 
Example 2
Source File: FlowFilePackagerV1.java    From nifi with Apache License 2.0 6 votes vote down vote up
private void writeContentEntry(final TarArchiveOutputStream tarOut, final InputStream inStream, final long fileSize) throws IOException {
    final TarArchiveEntry entry = new TarArchiveEntry(FILENAME_CONTENT);
    entry.setMode(tarPermissions);
    entry.setSize(fileSize);
    tarOut.putArchiveEntry(entry);
    final byte[] buffer = new byte[512 << 10];//512KB
    int bytesRead = 0;
    while ((bytesRead = inStream.read(buffer)) != -1) { //still more data to read
        if (bytesRead > 0) {
            tarOut.write(buffer, 0, bytesRead);
        }
    }

    copy(inStream, tarOut);
    tarOut.closeArchiveEntry();
}
 
Example 3
Source File: FlowFilePackagerV1.java    From nifi with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 4
Source File: CompressedDirectory.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

  final Path relativePath = root.relativize(file);

  if (exclude(ignoreMatchers, relativePath)) {
    return FileVisitResult.CONTINUE;
  }

  final TarArchiveEntry entry = new TarArchiveEntry(file.toFile());
  entry.setName(relativePath.toString());
  entry.setMode(getFileMode(file));
  entry.setSize(attrs.size());
  tarStream.putArchiveEntry(entry);
  Files.copy(file, tarStream);
  tarStream.closeArchiveEntry();
  return FileVisitResult.CONTINUE;
}
 
Example 5
Source File: CompressedDirectory.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public FileVisitResult preVisitDirectory(Path dir,
                                         BasicFileAttributes attrs) throws IOException {
  if (Files.isSameFile(dir, root)) {
    return FileVisitResult.CONTINUE;
  }

  final Path relativePath = root.relativize(dir);

  if (exclude(ignoreMatchers, relativePath)) {
    return FileVisitResult.CONTINUE;
  }

  final TarArchiveEntry entry = new TarArchiveEntry(dir.toFile());
  entry.setName(relativePath.toString());
  entry.setMode(getFileMode(dir));
  tarStream.putArchiveEntry(entry);
  tarStream.closeArchiveEntry();
  return FileVisitResult.CONTINUE;
}
 
Example 6
Source File: DebianPackageWriter.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
private void addControlContent ( final TarArchiveOutputStream out, final String name, final ContentProvider content, final int mode, final Supplier<Instant> timestampSupplier ) throws IOException
{
    if ( content == null || !content.hasContent () )
    {
        return;
    }

    final TarArchiveEntry entry = new TarArchiveEntry ( name );
    if ( mode >= 0 )
    {
        entry.setMode ( mode );
    }

    entry.setUserName ( "root" );
    entry.setGroupName ( "root" );
    entry.setSize ( content.getSize () );
    entry.setModTime ( timestampSupplier.get ().toEpochMilli () );
    out.putArchiveEntry ( entry );
    try ( InputStream stream = content.createInputStream () )
    {
        IOUtils.copy ( stream, out );
    }
    out.closeArchiveEntry ();
}
 
Example 7
Source File: DebianPackageWriter.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
private static void applyInfo ( final TarArchiveEntry entry, final EntryInformation entryInformation )
{
    if ( entryInformation == null )
    {
        return;
    }

    if ( entryInformation.getUser () != null )
    {
        entry.setUserName ( entryInformation.getUser () );
    }
    if ( entryInformation.getGroup () != null )
    {
        entry.setGroupName ( entryInformation.getGroup () );
    }
    entry.setMode ( entryInformation.getMode () );
}
 
Example 8
Source File: GeneratorService.java    From vertx-starter with Apache License 2.0 6 votes vote down vote up
private void addFile(Path rootPath, Path filePath, ArchiveOutputStream stream) throws IOException {
  String relativePath = rootPath.relativize(filePath).toString();
  if (relativePath.length() == 0) return;
  String entryName = jarFileWorkAround(leadingDot(relativePath));
  ArchiveEntry entry = stream.createArchiveEntry(filePath.toFile(), entryName);
  if (EXECUTABLES.contains(entryName)) {
    if (entry instanceof ZipArchiveEntry) {
      ZipArchiveEntry zipArchiveEntry = (ZipArchiveEntry) entry;
      zipArchiveEntry.setUnixMode(0744);
    } else if (entry instanceof TarArchiveEntry) {
      TarArchiveEntry tarArchiveEntry = (TarArchiveEntry) entry;
      tarArchiveEntry.setMode(0100744);
    }
  }
  stream.putArchiveEntry(entry);
  if (filePath.toFile().isFile()) {
    try (InputStream i = Files.newInputStream(filePath)) {
      IOUtils.copy(i, stream);
    }
  }
  stream.closeArchiveEntry();
}
 
Example 9
Source File: DebianPackageWriter.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private void addControlContent ( final TarArchiveOutputStream out, final String name, final ContentProvider content, final int mode ) throws IOException
{
    if ( content == null || !content.hasContent () )
    {
        return;
    }

    final TarArchiveEntry entry = new TarArchiveEntry ( name );
    if ( mode >= 0 )
    {
        entry.setMode ( mode );
    }

    entry.setUserName ( "root" );
    entry.setGroupName ( "root" );
    entry.setSize ( content.getSize () );
    entry.setModTime ( this.getTimestampProvider ().getModTime () );
    out.putArchiveEntry ( entry );
    try ( InputStream stream = content.createInputStream () )
    {
        ByteStreams.copy ( stream, out );
    }
    out.closeArchiveEntry ();
}
 
Example 10
Source File: DebianPackageWriter.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private static void applyInfo ( final TarArchiveEntry entry, final EntryInformation entryInformation, TimestampProvider timestampProvider )
{
    if ( entryInformation == null )
    {
        return;
    }

    if ( entryInformation.getUser () != null )
    {
        entry.setUserName ( entryInformation.getUser () );
    }
    if ( entryInformation.getGroup () != null )
    {
        entry.setGroupName ( entryInformation.getGroup () );
    }
    entry.setMode ( entryInformation.getMode () );
    entry.setModTime ( timestampProvider.getModTime () );
}
 
Example 11
Source File: Packaging.java    From dekorate with Apache License 2.0 6 votes vote down vote up
public static void tar(Path inputPath, Path outputPath) throws IOException {
  if (!Files.exists(inputPath)) {
    throw new FileNotFoundException("File not found " + inputPath);
  }

  try (TarArchiveOutputStream tarArchiveOutputStream = buildTarStream(outputPath.toFile())) {
    if (!Files.isDirectory(inputPath)) {
      TarArchiveEntry tarEntry = new TarArchiveEntry(inputPath.toFile().getName());
      if (inputPath.toFile().canExecute()) {
        tarEntry.setMode(tarEntry.getMode() | 0755);
      }
      putTarEntry(tarArchiveOutputStream, tarEntry, inputPath);
    } else {
      Files.walkFileTree(inputPath,
                         new TarDirWalker(inputPath, tarArchiveOutputStream));
    }
    tarArchiveOutputStream.flush();
  }
}
 
Example 12
Source File: FlowFilePackagerV1.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 13
Source File: MountableFile.java    From testcontainers-java with MIT License 5 votes vote down vote up
private void recursiveTar(String entryFilename, String rootPath, String itemPath, TarArchiveOutputStream tarArchive) {
    try {
        final File sourceFile = new File(itemPath).getCanonicalFile();     // e.g. /foo/bar/baz
        final File sourceRootFile = new File(rootPath).getCanonicalFile();     // e.g. /foo
        final String relativePathToSourceFile = sourceRootFile.toPath().relativize(sourceFile.toPath()).toFile().toString();    // e.g. /bar/baz

        final String tarEntryFilename;
        if (relativePathToSourceFile.isEmpty()) {
            tarEntryFilename = entryFilename; // entry filename e.g. xyz => xyz
        } else {
            tarEntryFilename = entryFilename + "/" + relativePathToSourceFile; // entry filename e.g. /xyz/bar/baz => /foo/bar/baz
        }

        final TarArchiveEntry tarEntry = new TarArchiveEntry(sourceFile, tarEntryFilename.replaceAll("^/", ""));

        // TarArchiveEntry automatically sets the mode for file/directory, but we can update to ensure that the mode is set exactly (inc executable bits)
        tarEntry.setMode(getUnixFileMode(itemPath));
        tarArchive.putArchiveEntry(tarEntry);

        if (sourceFile.isFile()) {
            Files.copy(sourceFile.toPath(), tarArchive);
        }
        // a directory entry merely needs to exist in the TAR file - there is no data stored yet
        tarArchive.closeArchiveEntry();

        final File[] children = sourceFile.listFiles();
        if (children != null) {
            // recurse into child files/directories
            for (final File child : children) {
                recursiveTar(entryFilename, sourceRootFile.getCanonicalPath(), child.getCanonicalPath(), tarArchive);
            }
        }
    } catch (IOException e) {
        log.error("Error when copying TAR file entry: {}", itemPath, e);
        throw new UncheckedIOException(e); // fail fast
    }
}
 
Example 14
Source File: Transferable.java    From testcontainers-java with MIT License 5 votes vote down vote up
/**
 * transfer content of this Transferable to the output stream. <b>Must not</b> close the stream.
 *
 * @param tarArchiveOutputStream stream to output
 * @param destination
 */
default void transferTo(TarArchiveOutputStream tarArchiveOutputStream, final String destination) {
    TarArchiveEntry tarEntry = new TarArchiveEntry(destination);
    tarEntry.setSize(getSize());
    tarEntry.setMode(getFileMode());

    try {
        tarArchiveOutputStream.putArchiveEntry(tarEntry);
        IOUtils.write(getBytes(), tarArchiveOutputStream);
        tarArchiveOutputStream.closeArchiveEntry();
    } catch (IOException e) {
        throw new RuntimeException("Can't transfer " + getDescription(), e);
    }
}
 
Example 15
Source File: DockerComputerIOLauncher.java    From yet-another-docker-plugin with MIT License 5 votes vote down vote up
@Override
    public void afterContainerCreate(DockerClient client, String containerId) throws IOException {
        // upload archive
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
             TarArchiveOutputStream tarOut = new TarArchiveOutputStream(byteArrayOutputStream)) {

            // @see hudson.model.Slave.JnlpJar.getURL()
//            byte[] slavejar = IOUtils.toByteArray(Jenkins.getInstance().servletContext.getResourceAsStream("/WEB-INF/slave.jar"));
//            if (isNull(null)) {
//                // during the development this path doesn't have the files.
//                slavejar = Files.readAllBytes(Paths.get("./target/jenkins/WEB-INF/slave.jar"));
//            }
            byte[] slaveJar = new Slave.JnlpJar("slave.jar").readFully();

            TarArchiveEntry entry = new TarArchiveEntry(SLAVE_JAR);
            entry.setSize(slaveJar.length);
            entry.setMode(0664);
            tarOut.putArchiveEntry(entry);
            tarOut.write(slaveJar);
            tarOut.closeArchiveEntry();
            tarOut.close();

            try (InputStream is = new ByteArrayInputStream(byteArrayOutputStream.toByteArray())) {
                client.copyArchiveToContainerCmd(containerId)
                        .withTarInputStream(is)
                        .withRemotePath("/")
                        .exec();
            }
        }
    }
 
Example 16
Source File: ArchiveWorkItemHandler.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public void executeWorkItem(WorkItem workItem,
                            WorkItemManager manager) {
    String archive = (String) workItem.getParameter("Archive");
    List<File> files = (List<File>) workItem.getParameter("Files");

    try {
        RequiredParameterValidator.validate(this.getClass(),
                                            workItem);

        OutputStream outputStream = new FileOutputStream(new File(archive));
        ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar",
                                                                                      outputStream);

        if (files != null) {
            for (File file : files) {
                final TarArchiveEntry entry = new TarArchiveEntry("testdata/test1.xml");
                entry.setModTime(0);
                entry.setSize(file.length());
                entry.setUserId(0);
                entry.setGroupId(0);
                entry.setMode(0100000);
                os.putArchiveEntry(entry);
                IOUtils.copy(new FileInputStream(file),
                             os);
            }
        }
        os.closeArchiveEntry();
        os.close();
        manager.completeWorkItem(workItem.getId(),
                                 null);
    } catch (Exception e) {
        handleException(e);
        manager.abortWorkItem(workItem.getId());
    }
}
 
Example 17
Source File: Packaging.java    From dekorate with Apache License 2.0 5 votes vote down vote up
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  TarArchiveEntry tarEntry = new TarArchiveEntry(basePath.relativize(file).toFile());
  tarEntry.setSize(attrs.size());
  if (file.toFile().canExecute()) {
    tarEntry.setMode(tarEntry.getMode() | 0755);
  }
  Packaging.putTarEntry(tarArchiveOutputStream, tarEntry, file);
  return FileVisitResult.CONTINUE;
}
 
Example 18
Source File: DockerComputerSSHConnector.java    From docker-plugin with MIT License 5 votes vote down vote up
@Override
public void beforeContainerStarted(DockerAPI api, String workdir, String containerId) throws IOException, InterruptedException {
    final String key = sshKeyStrategy.getInjectedKey();
    if (key != null) {
        final String authorizedKeysCommand = "#!/bin/sh\n"
                + "[ \"$1\" = \"" + sshKeyStrategy.getUser() + "\" ] "
                + "&& echo '" + key + "'"
                + "|| :";
        final byte[] authorizedKeysCommandAsBytes = authorizedKeysCommand.getBytes(StandardCharsets.UTF_8);
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             TarArchiveOutputStream tar = new TarArchiveOutputStream(bos)) {
            TarArchiveEntry entry = new TarArchiveEntry("authorized_key");
            entry.setSize(authorizedKeysCommandAsBytes.length);
            entry.setMode(0700);
            tar.putArchiveEntry(entry);
            tar.write(authorizedKeysCommandAsBytes);
            tar.closeArchiveEntry();
            tar.close();
            try (InputStream is = new ByteArrayInputStream(bos.toByteArray());
                 DockerClient client = api.getClient()) {
                client.copyArchiveToContainerCmd(containerId)
                        .withTarInputStream(is)
                        .withRemotePath("/root")
                        .exec();
            }
        }
    }
}
 
Example 19
Source File: ArtifactUploader.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Archive and compress 'pathsToIncludeInArchive' into 'out', using tar+zstandard. */
@VisibleForTesting
static long compress(
    ProjectFilesystem projectFilesystem, Collection<Path> pathsToIncludeInArchive, Path out)
    throws IOException {
  long fullSize = 0L;
  try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(out));
      OutputStream z = new ZstdCompressorOutputStream(o);
      TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
    archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    for (Path path : pathsToIncludeInArchive) {
      boolean isRegularFile = !projectFilesystem.isDirectory(path);

      // Add a file entry.
      TarArchiveEntry e = new TarArchiveEntry(path.toString() + (isRegularFile ? "" : "/"));
      int mode = (int) projectFilesystem.getPosixFileMode(path);
      // If permissions don't allow for owner to r or w, update to u+=rw and g+=r
      e.setMode((mode & 384) == 0 ? (mode | 416) : mode);
      e.setModTime((long) ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP * 1000);

      if (isRegularFile) {
        long pathSize = projectFilesystem.getFileSize(path);
        e.setSize(pathSize);
        fullSize += pathSize;
        archive.putArchiveEntry(e);
        try (InputStream input = projectFilesystem.newFileInputStream(path)) {
          ByteStreams.copy(input, archive);
        }
      } else {
        archive.putArchiveEntry(e);
      }
      archive.closeArchiveEntry();
    }
    archive.finish();
  }

  return fullSize;
}
 
Example 20
Source File: BuildImageStepExecution.java    From kubernetes-pipeline-plugin with Apache License 2.0 4 votes vote down vote up
public void visit(File file, String relativePath) throws IOException {
    if (recursiveMatch(dockerIgnore, file.toPath())) {
         return;
    }

    if (relativePath.contains("/")) {
        relativePath = relativePath.substring(relativePath.indexOf("/") + 1);
    } else {
        relativePath = ".";
    }

    if(Functions.isWindows()) {
        relativePath = relativePath.replace('\\', '/');
    }

    if(file.isDirectory()) {
        relativePath += '/';
    }

    TarArchiveEntry te = new TarArchiveEntry(file);
    te.setName(relativePath);

    int mode = IOUtils.mode(file);
    if (mode!=-1) {
        te.setMode(mode);
    }
    te.setModTime(file.lastModified());

    if(!file.isDirectory()) {
        te.setSize(file.length());
    }

    tar.putArchiveEntry(te);

    if (!file.isDirectory()) {
        FileInputStream in = new FileInputStream(file);
        try {
            int len;
            while((len=in.read(buf))>=0)
                tar.write(buf,0,len);
        } finally {
            in.close();
        }
    }

    tar.closeArchiveEntry();
    entriesWritten++;
}