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:company.gonapps.loghut.utils.FileUtils.java

private static void addFileToArchive(TarArchiveOutputStream tarArchiveOutputStream, String path, String base)
        throws IOException {
    File file = new File(path);
    String entryName = base + file.getName();
    tarArchiveOutputStream.putArchiveEntry(new TarArchiveEntry(file, entryName));

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

From source file:com.amazonaws.codepipeline.jenkinsplugin.ArchiveEntryFactory.java

public ArchiveEntry create(final File file, final String fileName) {
    switch (compressionType) {
    case None:/*from   w  ww .  j  a  v a2s.c om*/
    case Zip:
        return new ZipArchiveEntry(file, fileName);
    case Tar:
    case TarGz:
        return new TarArchiveEntry(file, fileName);
    }

    return null;
}

From source file:fr.insalyon.creatis.vip.applicationimporter.server.business.TargzUtils.java

private static void addFileToTarGz(TarArchiveOutputStream tOut, File f, String dir) throws BusinessException {
    try {/*from ww w  .  j  a v a 2s .c om*/
        TarArchiveEntry tarEntry;
        if (dir == null) {
            tarEntry = new TarArchiveEntry(f, f.getName());
        } else {
            tarEntry = new TarArchiveEntry(f, dir + "/" + f.getName());
        }
        tOut.putArchiveEntry(tarEntry);
        if (!f.isDirectory()) {
            FileInputStream in = new FileInputStream(f);
            IOUtils.copy(in, tOut);
            tOut.closeArchiveEntry();
            in.close();
        } else {
            tOut.closeArchiveEntry();
            String name = f.getName();
            File[] children = f.listFiles();
            if (children != null) {
                for (File child : children) {

                    addFileToTarGz(tOut, new File(child.getParent() + "/" + child.getName()), name);
                }
            }
        }
    } catch (IOException ex) {
        throw new BusinessException(ex);
    }
}

From source file:com.ipcglobal.fredimport.util.FredUtils.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  w w  .  j  ava  2s.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 {
    try {
        File f = new File(path);
        String entryName = base + f.getName();
        TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
        tOut.putArchiveEntry(tarEntry);
        if (f.isFile()) {
            // had to do this in 3 steps due to memory leak as per http://stackoverflow.com/questions/13461393/compress-directory-to-tar-gz-with-commons-compress/23524963#23524963 
            FileInputStream in = new FileInputStream(f);
            IOUtils.copy(in, tOut);
            in.close();

            tOut.closeArchiveEntry();
        } else {
            tOut.closeArchiveEntry();
            File[] children = f.listFiles();
            if (children != null) {
                for (File child : children) {
                    addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        throw e;
    }
}

From source file:hudson.util.io.TarArchiver.java

@Override
public void visitSymlink(File link, String target, String relativePath) throws IOException {
    TarArchiveEntry e = new TarArchiveEntry(relativePath, LF_SYMLINK);
    try {/*from  w ww.jav  a  2 s  . com*/
        int mode = IOUtils.mode(link);
        if (mode != -1) {
            e.setMode(mode);
        }
    } catch (PosixException x) {
        // ignore
    }

    e.setLinkName(target);

    tar.putArchiveEntry(e);
    tar.closeArchiveEntry();
    entriesWritten++;
}

From source file:com.puppetlabs.geppetto.forge.util.TarUtils.java

private static void append(File file, FileFilter filter, int baseNameLen, String addedTopFolder,
        TarArchiveOutputStream tarOut) throws IOException {

    String name = file.getAbsolutePath();
    if (name.length() <= baseNameLen)
        name = "";
    else//w w  w  .  j  a  v a 2 s  . c  o m
        name = name.substring(baseNameLen);
    if (File.separatorChar == '\\')
        name = name.replace('\\', '/');
    if (addedTopFolder != null)
        name = addedTopFolder + '/' + name;

    if (FileUtils.isSymlink(file)) {
        String linkTarget = FileUtils.readSymbolicLink(file);
        if (linkTarget != null) {
            TarArchiveEntry entry = new TarArchiveEntry(name, TarConstants.LF_SYMLINK);
            entry.setName(name);
            entry.setLinkName(linkTarget);
            tarOut.putArchiveEntry(entry);
        }
        return;
    }

    ArchiveEntry entry = tarOut.createArchiveEntry(file, name);
    tarOut.putArchiveEntry(entry);
    File[] children = file.listFiles(filter);
    if (children != null) {
        tarOut.closeArchiveEntry();
        // This is a directory. Append its children
        for (File child : children)
            append(child, filter, baseNameLen, addedTopFolder, tarOut);
        return;
    }

    // Append the content of the file
    InputStream input = new FileInputStream(file);
    try {
        StreamUtil.copy(input, tarOut);
        tarOut.closeArchiveEntry();
    } finally {
        StreamUtil.close(input);
    }
}

From source file:com.fizzed.stork.assembly.AssemblyUtils.java

static public void addFileToTGZStream(TarArchiveOutputStream tgzout, File f, String base, boolean appendName)
        throws IOException {
    //File f = new File(path);
    String entryName = base;/*ww  w.  j a  va 2 s  . c  om*/
    if (appendName) {
        if (!entryName.equals("")) {
            if (!entryName.endsWith("/")) {
                entryName += "/" + f.getName();
            } else {
                entryName += f.getName();
            }
        } else {
            entryName += f.getName();
        }
    }
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);

    if (f.isFile()) {
        if (f.canExecute()) {
            // -rwxr-xr-x
            tarEntry.setMode(493);
        } else {
            // keep default mode
        }
    }

    tgzout.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        try (FileInputStream in = new FileInputStream(f)) {
            IOUtils.copy(in, tgzout);
        }
        tgzout.closeArchiveEntry();
    } else {
        tgzout.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                logger.info(" adding: " + entryName + "/" + child.getName());
                addFileToTGZStream(tgzout, child, entryName + "/", true);
            }
        }
    }
}

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

@Override
protected boolean handleDirectory(File directory, int depth, Collection results) throws IOException {

    String path = directory.getPath();

    if (logger.isTraceEnabled()) {
        logger.trace("Archiving directory " + path);
    }/*from www . j  a  va2s  . c om*/

    TarArchiveEntry entry = new TarArchiveEntry(directory, path);

    outputStream.putArchiveEntry(entry);
    outputStream.closeArchiveEntry();

    return true;
}

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

/**
 * Produces TAR archive/* ww w  . j  a v  a2 s.c o m*/
 */
@Override
public File copy(final File src, final File tar_file, final String dst) throws Exception {
    final ArchiveStreamFactory asf = new ArchiveStreamFactory();

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

        final File tempFile = File.createTempFile("updateTar", "tar");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);

        // copy the existing entries
        ArchiveEntry nextEntry;
        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 ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);

        // 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.fizzed.stork.util.AssemblyUtils.java

static public void addFileToTGZStream(TarArchiveOutputStream tgzout, File f, String base, boolean appendName)
        throws IOException {
    //File f = new File(path);
    String entryName = base;// w w w. j  a v  a 2 s  .c  o  m
    if (appendName) {
        if (!entryName.equals("")) {
            if (!entryName.endsWith("/")) {
                entryName += "/" + f.getName();
            } else {
                entryName += f.getName();
            }
        } else {
            entryName += f.getName();
        }
    }
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);

    if (f.isFile()) {
        if (f.canExecute()) {
            // -rwxr-xr-x
            tarEntry.setMode(493);
        } else {
            // keep default mode
        }
    }

    tgzout.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        FileInputStream in = new FileInputStream(f);
        IOUtils.copy(in, tgzout);
        in.close();
        tgzout.closeArchiveEntry();

    } else {
        tgzout.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                logger.info(" adding: " + entryName + "/" + child.getName());
                addFileToTGZStream(tgzout, child, entryName + "/", true);
            }
        }
    }
}