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

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

Introduction

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

Prototype

public TarArchiveEntry(File file, String fileName) 

Source Link

Document

Construct an entry for a file.

Usage

From source file:com.twitter.heron.apiserver.utils.FileHelper.java

private static void addFileToArchive(TarArchiveOutputStream archiveOutputStream, File file, String base)
        throws IOException {
    final File absoluteFile = file.getAbsoluteFile();
    final String entryName = base + file.getName();
    final TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file, entryName);
    archiveOutputStream.putArchiveEntry(tarArchiveEntry);

    if (absoluteFile.isFile()) {
        Files.copy(file.toPath(), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();
    } else {//from ww  w.j  av a2 s  .com
        archiveOutputStream.closeArchiveEntry();
        if (absoluteFile.listFiles() != null) {
            for (File f : absoluteFile.listFiles()) {
                addFileToArchive(archiveOutputStream, f, entryName + "/");
            }
        }
    }
}

From source file:net.orpiske.ssps.common.archive.RecursiveArchiver.java

@Override
protected void handleFile(File file, int depth, Collection results) throws IOException {

    String path;//from   w ww .j  a  v a  2s.  c  o m

    path = file.getPath();

    if (logger.isTraceEnabled()) {
        logger.trace("Archiving file " + path);
    }

    TarArchiveEntry entry = new TarArchiveEntry(file, path);

    outputStream.putArchiveEntry(entry);
    IOUtils.copy(new FileInputStream(file), outputStream);

    outputStream.closeArchiveEntry();
}

From source file:fr.gael.ccsds.sip.archive.TgzArchiveManager.java

@Override
public File copy(final File src, final File tar_file, final String dst) throws Exception {
    final ArchiveStreamFactory asf = new ArchiveStreamFactory();
    final CompressorStreamFactory csf = new CompressorStreamFactory();

    // Case of tar already exist: all the entries must be copied..
    if (tar_file.exists()) {
        final FileInputStream fis = new FileInputStream(tar_file);
        final CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP, fis);
        final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

        final File tempFile = File.createTempFile("updateTar", "tar");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

        // copy the existing entries
        ArchiveEntry nextEntry;/*from   w ww. j  a  v  a 2 s.co  m*/
        while ((nextEntry = ais.getNextEntry()) != null) {
            aos.putArchiveEntry(nextEntry);
            IOUtils.copy(ais, aos);
            aos.closeArchiveEntry();
        }

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        ais.close();
        aos.close();
        fis.close();

        // copies the new file over the old
        tar_file.delete();
        tempFile.renameTo(tar_file);
        return tar_file;
    } else {
        final FileOutputStream fos = new FileOutputStream(tar_file);
        final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        aos.close();
        fos.close();
    }
    return tar_file;
}

From source file:com.linkedin.thirdeye.bootstrap.util.TarGzCompressionUtils.java

/**
 * Creates a tar entry for the path specified with a name built from the base
 * passed in and the file/directory name. If the path is a directory, a
 * recursive call is made such that the full directory is added to the tar.
 *
 * @param tOut/* www  .j av a2 s  .  co  m*/
 *          The tar file's output stream
 * @param path
 *          The filesystem path of the file/directory being added
 * @param base
 *          The base prefix to for the name of the tar file entry
 *
 * @throws IOException
 *           If anything goes wrong
 */
private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
    File f = new File(path);
    String entryName = base + f.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
    FileInputStream fis = null;

    try {
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        tOut.putArchiveEntry(tarEntry);

        if (f.isFile()) {
            fis = new FileInputStream(f);
            IOUtils.copy(fis, tOut);

            tOut.closeArchiveEntry();
        } else {
            tOut.closeArchiveEntry();

            File[] children = f.listFiles();

            if (children != null) {
                for (File child : children) {
                    addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
                }
            }
        }
    } finally {
        if (fis != null) {
            fis.close();
        }
    }

}

From source file:com.salsaberries.narchiver.Writer.java

private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir)
        throws IOException {
    // Create an entry for the file
    taos.putArchiveEntry(new TarArchiveEntry(file, dir + FILE_SEPARATOR + file.getName()));
    if (file.isFile()) {
        // Add the file to the archive
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, taos);//  w ww.  j  a v a 2 s  .  c o m
        taos.closeArchiveEntry();
        bis.close();
    } else if (file.isDirectory()) {
        // Close the archive entry
        taos.closeArchiveEntry();
        // Go through all the files in the directory and using recursion, add them to the archive
        for (File childFile : file.listFiles()) {
            addFilesToCompression(taos, childFile, file.getName());
        }
    }
}

From source file:com.ttech.cordovabuild.infrastructure.archive.ArchiveUtils.java

private static void addFileToTarGz(TarArchiveOutputStream tOut, Path path, String base) throws IOException {
    File f = path.toFile();//from   w  w w  . ja  v  a2  s .  co m
    String entryName = base + f.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
    tOut.putArchiveEntry(tarEntry);
    if (f.isFile()) {
        IOUtils.copy(new FileInputStream(f), tOut);
        tOut.closeArchiveEntry();
    } else {
        tOut.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToTarGz(tOut, child.toPath().toAbsolutePath(), entryName + "/");
            }
        }
    }
}

From source file:com.github.trask.comet.loadtest.aws.ChefBootstrap.java

private static void addToTarWithBase(File file, TarArchiveOutputStream tarOut, String base) throws IOException {

    String entryName = base + file.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);

    tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tarOut.putArchiveEntry(tarEntry);//from  w w  w.  j a va  2 s  .c o m

    if (file.isFile()) {
        IOUtils.copy(new FileInputStream(file), tarOut);
        tarOut.closeArchiveEntry();
    } else {
        tarOut.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addToTarWithBase(child, tarOut, entryName + "/");
            }
        }
    }
}

From source file:com.streamsets.datacollector.restapi.TarEdgeArchiveBuilder.java

protected void addArchiveEntry(ArchiveOutputStream archiveOutput, Object fileContent, String pipelineId,
        String fileName) throws IOException {
    File pipelineFile = File.createTempFile(pipelineId, fileName);
    FileOutputStream pipelineOutputStream = new FileOutputStream(pipelineFile);
    ObjectMapperFactory.get().writeValue(pipelineOutputStream, fileContent);
    pipelineOutputStream.flush();/* w  ww .j  av a  2  s.co  m*/
    pipelineOutputStream.close();
    TarArchiveEntry archiveEntry = new TarArchiveEntry(pipelineFile,
            DATA_PIPELINES_FOLDER + pipelineId + "/" + fileName);
    archiveEntry.setSize(pipelineFile.length());
    archiveOutput.putArchiveEntry(archiveEntry);
    IOUtils.copy(new FileInputStream(pipelineFile), archiveOutput);
    archiveOutput.closeArchiveEntry();
}

From source file:com.netflix.spinnaker.halyard.core.registry.v1.GitProfileReader.java

@Override
public InputStream readArchiveProfile(String artifactName, String version, String profileName)
        throws IOException {
    Path profilePath = Paths.get(profilePath(artifactName, version, profileName));

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os);

    ArrayList<Path> filePathsToAdd = java.nio.file.Files
            .walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)
            .filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new));

    for (Path path : filePathsToAdd) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString());
        int permissions = FileModeUtils.getFileMode(Files.getPosixFilePermissions(path));
        permissions = FileModeUtils.setFileBit(permissions);
        tarEntry.setMode(permissions);/* w  ww .j  av  a  2  s  .  c  o m*/
        tarArchive.putArchiveEntry(tarEntry);
        IOUtils.copy(Files.newInputStream(path), tarArchive);
        tarArchive.closeArchiveEntry();
    }

    tarArchive.finish();
    tarArchive.close();

    return new ByteArrayInputStream(os.toByteArray());
}

From source file:com.linkedin.pinot.common.utils.TarGzCompressionUtils.java

/**
 * Creates a tar entry for the path specified with a name built from the base
 * passed in and the file/directory name. If the path is a directory, a
 * recursive call is made such that the full directory is added to the tar.
 *
 * @param tOut//  w  ww . java2  s  . com
 *          The tar file's output stream
 * @param path
 *          The filesystem path of the file/directory being added
 * @param base
 *          The base prefix to for the name of the tar file entry
 *
 * @throws IOException
 *           If anything goes wrong
 */
private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
    File f = new File(path);
    String entryName = base + f.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);

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

    if (f.isFile()) {
        IOUtils.copy(new FileInputStream(f), tOut);

        tOut.closeArchiveEntry();
    } else {
        tOut.closeArchiveEntry();

        File[] children = f.listFiles();

        if (children != null) {
            for (File child : children) {
                addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}