Example usage for org.apache.commons.compress.archivers.tar TarArchiveOutputStream putArchiveEntry

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream putArchiveEntry

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveOutputStream putArchiveEntry.

Prototype

public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException 

Source Link

Document

Put an entry on the output stream.

Usage

From source file:org.apache.openejb.maven.plugin.BuildTomEEMojo.java

private void tarGz(final TarArchiveOutputStream tarGz, final File f, final String prefix) throws IOException {
    final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
    final TarArchiveEntry archiveEntry = new TarArchiveEntry(f, path);
    if (isSh(path)) {
        archiveEntry.setMode(0755);/*from w w w.  ja  v  a 2s  .c  o  m*/
    }
    tarGz.putArchiveEntry(archiveEntry);
    if (f.isDirectory()) {
        tarGz.closeArchiveEntry();
        final File[] files = f.listFiles();
        if (files != null) {
            for (final File child : files) {
                tarGz(tarGz, child, prefix);
            }
        }
    } else {
        IO.copy(f, tarGz);
        tarGz.closeArchiveEntry();
    }
}

From source file:org.apache.reef.runtime.mesos.driver.REEFScheduler.java

private String getReefTarUri(final String jobIdentifier) {
    try {/*from  w ww. j ava2s  .c  o  m*/
        // Create REEF_TAR
        final FileOutputStream fileOutputStream = new FileOutputStream(REEF_TAR);
        final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(
                new GZIPOutputStream(fileOutputStream));
        final File globalFolder = new File(this.fileNames.getGlobalFolderPath());
        final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(globalFolder.toPath());

        for (final Path path : directoryStream) {
            tarArchiveOutputStream.putArchiveEntry(
                    new TarArchiveEntry(path.toFile(), globalFolder + "/" + path.getFileName()));

            final BufferedInputStream bufferedInputStream = new BufferedInputStream(
                    new FileInputStream(path.toFile()));
            IOUtils.copy(bufferedInputStream, tarArchiveOutputStream);
            bufferedInputStream.close();

            tarArchiveOutputStream.closeArchiveEntry();
        }
        directoryStream.close();
        tarArchiveOutputStream.close();
        fileOutputStream.close();

        // Upload REEF_TAR to HDFS
        final FileSystem fileSystem = FileSystem.get(new Configuration());
        final org.apache.hadoop.fs.Path src = new org.apache.hadoop.fs.Path(REEF_TAR);
        final String reefTarUriValue = fileSystem.getUri().toString() + this.jobSubmissionDirectoryPrefix + "/"
                + jobIdentifier + "/" + REEF_TAR;
        final org.apache.hadoop.fs.Path dst = new org.apache.hadoop.fs.Path(reefTarUriValue);
        fileSystem.copyFromLocalFile(src, dst);

        return reefTarUriValue;
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.tika.server.TarWriter.java

private static void tarStoreBuffer(TarArchiveOutputStream zip, String name, byte[] dataBuffer)
        throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(name);

    entry.setSize(dataBuffer.length);//from  w ww  .  jav  a  2 s .  c o m

    zip.putArchiveEntry(entry);

    zip.write(dataBuffer);

    zip.closeArchiveEntry();
}

From source file:org.apache.whirr.util.Tarball.java

private static void addFile(TarArchiveOutputStream tarOutputStream, String path, String base)
        throws IOException {
    File file = new File(path);
    String entryName = base + file.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);

    tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tarOutputStream.putArchiveEntry(tarEntry);

    if (file.isFile()) {
        IOUtils.copy(new FileInputStream(file), tarOutputStream);
        tarOutputStream.closeArchiveEntry();
    } else {/* w ww  .j  a va  2  s .c  o m*/
        tarOutputStream.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addFile(tarOutputStream, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}

From source file:org.cloudbyexample.dc.agent.util.ArchiveUtil.java

/**
 * Add a file to the archive or recurse if a directory is specified.
 *///www  .ja  va 2  s  .c om
private static void addFiles(TarArchiveOutputStream taos, File file, String dir) throws IOException {
    taos.putArchiveEntry(new TarArchiveEntry(file, dir));

    if (file.isFile()) {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, taos);
        taos.closeArchiveEntry();
        bis.close();
    } else if (file.isDirectory()) {
        taos.closeArchiveEntry();
        for (File childFile : file.listFiles()) {
            addFiles(taos, childFile, childFile.getName());
        }
    }
}

From source file:org.cloudifysource.esc.util.TarGzUtils.java

private static void addFileToTarGz(final TarArchiveOutputStream tOut, final String path, final String base,
        final boolean addRoot) throws IOException {
    File f = new File(path);
    String entryName = base + f.getName();

    if (f.isFile()) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
        tOut.putArchiveEntry(tarEntry);
        IOUtils.copy(new FileInputStream(f), tOut);
        tOut.closeArchiveEntry();//w ww .  j a  v  a  2 s .c  om
    } else {
        if (addRoot) {
            TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
            tOut.putArchiveEntry(tarEntry);
            tOut.closeArchiveEntry();
        }
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                if (addRoot) {
                    addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/", true);
                } else {
                    addFileToTarGz(tOut, child.getAbsolutePath(), "", true);
                }
            }
        }
    }
}

From source file:org.codehaus.plexus.archiver.tar.TarArchiver.java

/**
 * tar a file/*  w ww  . j a  va  2  s. co  m*/
 *
 * @param entry the file to tar
 * @param tOut  the output stream
 * @param vPath the path name of the file to tar
 * @throws IOException on error
 */
protected void tarFile(ArchiveEntry entry, TarArchiveOutputStream tOut, String vPath)
        throws ArchiverException, IOException {

    // don't add "" to the archive
    if (vPath.length() <= 0) {
        return;
    }

    if (entry.getResource().isDirectory() && !vPath.endsWith("/")) {
        vPath += "/";
    }

    if (vPath.startsWith("/") && !options.getPreserveLeadingSlashes()) {
        int l = vPath.length();
        if (l <= 1) {
            // we would end up adding "" to the archive
            return;
        }
        vPath = vPath.substring(1, l);
    }

    int pathLength = vPath.length();
    InputStream fIn = null;

    try {
        TarArchiveEntry te;
        if (!longFileMode.isGnuMode()
                && pathLength >= org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN) {
            int maxPosixPathLen = org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN
                    + org.apache.commons.compress.archivers.tar.TarConstants.PREFIXLEN;
            if (longFileMode.isPosixMode()) {
            } else if (longFileMode.isPosixWarnMode()) {
                if (pathLength > maxPosixPathLen) {
                    getLogger().warn("Entry: " + vPath + " longer than " + maxPosixPathLen + " characters.");
                    if (!longWarningGiven) {
                        getLogger().warn("Resulting tar file can only be processed "
                                + "successfully by GNU compatible tar commands");
                        longWarningGiven = true;
                    }
                }
            } else if (longFileMode.isOmitMode()) {
                getLogger().info("Omitting: " + vPath);
                return;
            } else if (longFileMode.isWarnMode()) {
                getLogger().warn("Entry: " + vPath + " longer than "
                        + org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN + " characters.");
                if (!longWarningGiven) {
                    getLogger().warn("Resulting tar file can only be processed "
                            + "successfully by GNU compatible tar commands");
                    longWarningGiven = true;
                }
            } else if (longFileMode.isFailMode()) {
                throw new ArchiverException("Entry: " + vPath + " longer than "
                        + org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN + " characters.");
            } else {
                throw new IllegalStateException("Non gnu mode should never get here?");
            }
        }

        if (entry.getType() == ArchiveEntry.SYMLINK) {
            final SymlinkDestinationSupplier plexusIoSymlinkResource = (SymlinkDestinationSupplier) entry
                    .getResource();
            te = new TarArchiveEntry(vPath, TarArchiveEntry.LF_SYMLINK);
            te.setLinkName(plexusIoSymlinkResource.getSymlinkDestination());
        } else {
            te = new TarArchiveEntry(vPath);
        }

        long teLastModified = entry.getResource().getLastModified();
        te.setModTime(teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE ? System.currentTimeMillis()
                : teLastModified);

        if (entry.getType() == ArchiveEntry.SYMLINK) {
            te.setSize(0);

        } else if (!entry.getResource().isDirectory()) {
            final long size = entry.getResource().getSize();
            te.setSize(size == PlexusIoResource.UNKNOWN_RESOURCE_SIZE ? 0 : size);
        }
        te.setMode(entry.getMode());

        PlexusIoResourceAttributes attributes = entry.getResourceAttributes();

        te.setUserName((attributes != null && attributes.getUserName() != null) ? attributes.getUserName()
                : options.getUserName());
        te.setGroupName((attributes != null && attributes.getGroupName() != null) ? attributes.getGroupName()
                : options.getGroup());

        final int userId = (attributes != null && attributes.getUserId() != null) ? attributes.getUserId()
                : options.getUid();
        if (userId >= 0) {
            te.setUserId(userId);
        }

        final int groupId = (attributes != null && attributes.getGroupId() != null) ? attributes.getGroupId()
                : options.getGid();
        if (groupId >= 0) {
            te.setGroupId(groupId);
        }

        tOut.putArchiveEntry(te);

        try {
            if (entry.getResource().isFile() && !(entry.getType() == ArchiveEntry.SYMLINK)) {
                fIn = entry.getInputStream();

                Streams.copyFullyDontCloseOutput(fIn, tOut, "xAR");
            }

        } catch (Throwable e) {
            getLogger().warn("When creating tar entry", e);
        } finally {
            tOut.closeArchiveEntry();
        }
    } finally {
        IOUtil.close(fIn);
    }
}

From source file:org.codehaus.plexus.archiver.tar.TarRoundTripTest.java

/**
 * test round-tripping long (GNU) entries
 *//*from   w  w  w. ja va 2  s. co m*/
public void testLongRoundTripping() throws IOException {
    TarArchiveEntry original = new TarArchiveEntry(LONG_NAME);
    assertEquals("over 100 chars", true, LONG_NAME.length() > 100);
    assertEquals("original name", LONG_NAME, original.getName());

    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    TarArchiveOutputStream tos = new TarArchiveOutputStream(buff);
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tos.putArchiveEntry(original);
    tos.closeArchiveEntry();
    tos.close();

    TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(buff.toByteArray()));
    TarArchiveEntry tripped = tis.getNextTarEntry();
    assertEquals("round-tripped name", LONG_NAME, tripped.getName());
    assertNull("no more entries", tis.getNextEntry());
    tis.close();
}

From source file:org.dcm4chee.storage.tar.TarContainerProvider.java

@Override
public void writeEntriesTo(StorageContext context, List<ContainerEntry> entries, OutputStream out)
        throws IOException {
    TarArchiveOutputStream tar = new TarArchiveOutputStream(out);
    String checksumEntry = container.getChecksumEntry();
    if (checksumEntry != null) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ContainerEntry.writeChecksumsTo(entries, bout);
        TarArchiveEntry tarEntry = new TarArchiveEntry(checksumEntry);
        tarEntry.setSize(bout.size());//from  w ww .j  a v a2  s.  c o  m
        tar.putArchiveEntry(tarEntry);
        tar.write(bout.toByteArray());
        tar.closeArchiveEntry();
    }
    for (ContainerEntry entry : entries) {
        Path path = entry.getSourcePath();
        TarArchiveEntry tarEntry = new TarArchiveEntry(entry.getName());
        tarEntry.setModTime(Files.getLastModifiedTime(path).toMillis());
        tarEntry.setSize(Files.size(path));
        tar.putArchiveEntry(tarEntry);
        Files.copy(path, tar);
        tar.closeArchiveEntry();
    }
    tar.finish();
}

From source file:org.eclipse.che.api.vfs.TarArchiver.java

private void addTarEntry(VirtualFile virtualFile, TarArchiveOutputStream tarOutputStream)
        throws ServerException {
    try {//from  ww  w.  j a v  a2s .  co  m
        TarArchiveEntry tarEntry = new TarArchiveEntry(getTarEntryName(virtualFile));
        if (virtualFile.isFolder()) {
            tarEntry.setModTime(0);
            tarOutputStream.putArchiveEntry(tarEntry);
        } else {
            tarEntry.setSize(virtualFile.getLength());
            tarEntry.setModTime(virtualFile.getLastModificationDate());
            tarOutputStream.putArchiveEntry(tarEntry);
            try (InputStream content = virtualFile.getContent()) {
                ByteStreams.copy(content, tarOutputStream);
            }
        }
        tarOutputStream.closeArchiveEntry();
    } catch (ForbiddenException e) {
        throw new ServerException(e.getServiceError());
    } catch (IOException e) {
        throw new ServerException(e.getMessage(), e);
    }
}