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

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

Introduction

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

Prototype

public TarArchiveOutputStream(OutputStream os) 

Source Link

Document

Constructor for TarInputStream.

Usage

From source file:org.apache.openaz.xacml.admin.components.PolicyWorkspace.java

@Override
public InputStream getStream() {
    ///* ww w . ja va2s.co m*/
    // Grab our working repository
    //
    final Path repoPath = ((XacmlAdminUI) getUI()).getUserGitPath();
    Path workspacePath = ((XacmlAdminUI) getUI()).getUserWorkspace();
    final Path tarFile = Paths.get(workspacePath.toString(), "Repository.tgz");

    try (OutputStream os = Files.newOutputStream(tarFile)) {
        try (GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(os)) {
            try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzOut)) {

                tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

                Files.walkFileTree(repoPath, new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                            throws IOException {
                        if (dir.getFileName().toString().startsWith(".git")) {
                            return FileVisitResult.SKIP_SUBTREE;
                        }
                        Path relative = repoPath.relativize(dir);
                        if (relative.toString().isEmpty()) {
                            return super.preVisitDirectory(dir, attrs);
                        }
                        TarArchiveEntry entry = new TarArchiveEntry(relative.toFile());
                        tarOut.putArchiveEntry(entry);
                        tarOut.closeArchiveEntry();
                        return super.preVisitDirectory(dir, attrs);
                    }

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        if (file.getFileName().toString().endsWith(".xml") == false) {
                            return super.visitFile(file, attrs);
                        }
                        Path relative = repoPath.relativize(file);
                        TarArchiveEntry entry = new TarArchiveEntry(relative.toFile());
                        entry.setSize(Files.size(file));
                        tarOut.putArchiveEntry(entry);
                        try {
                            IOUtils.copy(Files.newInputStream(file), tarOut);
                        } catch (IOException e) {
                            logger.error(e);
                        }
                        tarOut.closeArchiveEntry();
                        return super.visitFile(file, attrs);
                    }

                });
                tarOut.finish();
            }
        }
    } catch (IOException e) {
        logger.error(e);
    }
    try {
        return Files.newInputStream(tarFile);
    } catch (IOException e) {
        logger.error(e);
    }
    return null;
}

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

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    super.execute();

    if (formats == null) {
        formats = Collections.emptyMap();
    }//from  w w  w.  jav a2s.c o m

    String prefix = catalinaBase.getParentFile().getAbsolutePath();
    if (!prefix.endsWith(File.separator)) {
        prefix += File.separator;
    }
    if (skipArchiveRootFolder) {
        prefix += catalinaBase.getName() + File.separator;
    }

    if (zip || formats.containsKey("zip")) {
        getLog().info("Zipping Custom TomEE Distribution");

        final String zip = formats.get("zip");
        final File output = zip != null ? new File(zip) : zipFile;
        try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(output))) {
            for (final String entry : catalinaBase.list()) {
                zip(zos, new File(catalinaBase, entry), prefix);
            }
        } catch (final IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }

        attach("zip", output);
    }
    if (formats != null) {
        formats.remove("zip"); //handled previously for compatibility

        for (final Map.Entry<String, String> format : formats.entrySet()) {
            final String key = format.getKey();
            getLog().info(key + "-ing Custom TomEE Distribution");

            if ("tar.gz".equals(key)) {
                final String out = format.getValue();
                final File output = out != null ? new File(out)
                        : new File(base.getParentFile(), base.getName() + "." + key);
                Files.mkdirs(output.getParentFile());

                try (final TarArchiveOutputStream tarGz = new TarArchiveOutputStream(
                        new GZIPOutputStream(new FileOutputStream(output)))) {
                    tarGz.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
                    for (final String entry : catalinaBase.list()) {
                        tarGz(tarGz, new File(catalinaBase, entry), prefix);
                    }
                } catch (final IOException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                }

                attach(key, output);
            } else {
                throw new MojoExecutionException(key + " format not supported");
            }
        }
    }
}

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

private String getReefTarUri(final String jobIdentifier) {
    try {//w ww  .  j  a v  a2s.c om
        // 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

public void writeTo(Map<String, byte[]> parts, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    TarArchiveOutputStream zip = new TarArchiveOutputStream(entityStream);

    for (Map.Entry<String, byte[]> entry : parts.entrySet()) {
        tarStoreBuffer(zip, entry.getKey(), entry.getValue());
    }/*from w ww. j a  v  a 2  s. co  m*/

    zip.close();
}

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

/**
 * Creates a tarball from the source directory and writes it into the target directory.
 *
 * @param sourceDirectory directory whose files will be added to the tarball
 * @param targetName      directory where tarball will be written to
 * @throws IOException when an exception occurs on creating the tarball
 *///from   w w  w  .j a  va 2s .c  o  m
public static void createFromDirectory(String sourceDirectory, String targetName) throws IOException {
    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    GzipCompressorOutputStream gzipOutputStream = null;
    TarArchiveOutputStream tarArchiveOutputStream = null;

    try {
        fileOutputStream = new FileOutputStream(new File(targetName));
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
        tarArchiveOutputStream = new TarArchiveOutputStream(gzipOutputStream);

        addFilesInDirectory(tarArchiveOutputStream, sourceDirectory);
    } finally {
        if (tarArchiveOutputStream != null) {
            tarArchiveOutputStream.finish();
        }
        if (tarArchiveOutputStream != null) {
            tarArchiveOutputStream.close();
        }
        if (gzipOutputStream != null) {
            gzipOutputStream.close();
        }
        if (bufferedOutputStream != null) {
            bufferedOutputStream.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }
}

From source file:org.artifactory.util.ArchiveUtils.java

public static ArchiveOutputStream createArchiveOutputStream(OutputStream outputStream, ArchiveType archiveType)
        throws IOException {
    ArchiveOutputStream result = null;/*from  w w w .j a  v  a2 s . co m*/
    switch (archiveType) {
    case ZIP:
        result = new ZipArchiveOutputStream(outputStream);
        break;
    case TAR:
        result = new TarArchiveOutputStream(outputStream);
        ((TarArchiveOutputStream) result).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        break;
    case TARGZ:
        result = new TarArchiveOutputStream(new GzipCompressorOutputStream(outputStream));
        ((TarArchiveOutputStream) result).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        break;
    case TGZ:
        result = new TarArchiveOutputStream(new GzipCompressorOutputStream(outputStream));
        ((TarArchiveOutputStream) result).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        break;
    }

    if (result == null) {
        throw new IllegalArgumentException("Unsupported archive type: '" + archiveType + "'");
    }

    return result;
}

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

/**
 * <p>Create an archive from a directory.</p>
 * /*w  ww.j a  v a2  s.  c o  m*/
 * <p><strong>Note</strong>: Archive can not be created in the directory being archived.</p>
 */
public static File createArchive(File archive, File dir) throws IOException {
    TarArchiveOutputStream taos = null;

    try {
        taos = new TarArchiveOutputStream(new FileOutputStream(archive));

        addFiles(taos, dir, "");
    } finally {
        IOUtils.closeQuietly(taos);
    }

    return archive;
}

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

/**
 * Create a tar.gz file./*from  ww w .j  ava2s. c o  m*/
 * 
 * @param sourcePaths
 *            Folders or files to add in the archive.
 * @param base
 *            The name to be use in the archive.
 * @param addRoot
 *            When <code>sourcePath</code> is a folder. if true, it will add the folder in the archive.<br/>
 *            <i>i.e: if sourcepath=/tmp/folderToInclude, archive.tar.gz will include the folder
 *            <b>folderToInclude</b> in the archive.</i>
 * @return The created archive.
 * @throws IOException
 *             If the archive cannot be create.
 */
public static File createTarGz(final String[] sourcePaths, final String base, final boolean addRoot)
        throws IOException {
    File tarGzFile = createTempTarGzFile();

    if (!FilenameUtils.getExtension(tarGzFile.getName().toLowerCase()).equals("gz")) {
        throw new IllegalArgumentException("Expecting tar.gz file: " + tarGzFile.getAbsolutePath());
    }

    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;
    try {
        fOut = new FileOutputStream(tarGzFile);
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);
        for (String path : sourcePaths) {
            addFileToTarGz(tOut, path, base, addRoot);
        }
    } finally {
        if (tOut != null) {
            tOut.close();
        }
        if (gzOut != null) {
            gzOut.close();
        }
        if (bOut != null) {
            bOut.close();
        }
        if (fOut != null) {
            fOut.close();
        }
    }

    return tarGzFile;
}

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());// ww w .  j  a v a  2  s .co 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();
}