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

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

Introduction

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

Prototype

public ArchiveEntry createArchiveEntry(File inputFile, String entryName) throws IOException 

Source Link

Usage

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//from w w w.j  av  a  2s .  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:de.uzk.hki.da.pkg.TarGZArchiveBuilder.java

/**
 * @param tOut/*  w ww  .ja  v a 2 s.co m*/
 * @param the actual file that should be added
 * @param base
 * @throws IOException
 */
private void addFileToTarGZ(TarArchiveOutputStream tOut, File file, String base) throws IOException {

    String entryName = base + file.getName();
    logger.debug("addFileToTarGZ: " + entryName);

    TarArchiveEntry entry = (TarArchiveEntry) tOut.createArchiveEntry(file, entryName);
    tOut.putArchiveEntry(entry);

    if (file.isFile()) {

        FileInputStream fis = new FileInputStream(file);
        IOUtils.copy(fis, tOut);
        tOut.closeArchiveEntry();
        fis.close();
    }

    if (file.isDirectory()) {

        tOut.closeArchiveEntry();

        File children[] = file.listFiles();
        if (children == null)
            return;

        for (int i = 0; i < children.length; i++) {

            addFileToTarGZ(tOut, children[i], entryName + "/");
        }
    }
}

From source file:de.uzk.hki.da.pkg.NativeJavaTarArchiveBuilder.java

/**
 * @param tOut/*  w  w  w.j  a  v a  2  s .  c o  m*/
 * @param the actual file that should be added
 * @param base
 * @throws IOException
 */
private void addFileToTar(TarArchiveOutputStream tOut, File file, String base) throws IOException {

    String entryName = base + file.getName();
    logger.debug("addFileToTar: " + entryName);

    TarArchiveEntry entry = (TarArchiveEntry) tOut.createArchiveEntry(file, entryName);
    tOut.putArchiveEntry(entry);

    if (file.isFile()) {

        FileInputStream fileInputStream = new FileInputStream(file);
        IOUtils.copy(fileInputStream, tOut);
        fileInputStream.close();
        tOut.closeArchiveEntry();

    }

    if (file.isDirectory()) {

        tOut.closeArchiveEntry();

        File children[] = file.listFiles();
        if (children == null)
            return;

        for (int i = 0; i < children.length; i++) {
            addFileToTar(tOut, children[i], entryName + "/");
        }
    }
}

From source file:de.uzk.hki.da.pkg.ArchiveBuilder.java

/**
 * Adds the given file to the archive/*ww w .j a  va 2s  .c o m*/
 * 
 * @param tOut The tar archive output stream
 * @param file The file to add
 * @param base The relative path to the file inside the archive (without the file name)
 * @throws IOException
 */
private boolean addFileToArchive(TarArchiveOutputStream tOut, File file, String base) throws IOException {

    if (sipBuildingProcess.isAborted())
        return false;

    String entryName = base + file.getName();

    TarArchiveEntry entry = (TarArchiveEntry) tOut.createArchiveEntry(file, entryName);
    tOut.putArchiveEntry(entry);

    if (file.isFile()) {
        FileInputStream fis = new FileInputStream(file);
        IOUtils.copy(fis, tOut);
        tOut.closeArchiveEntry();
        fis.close();

        progressManager.archiveProgress(jobId, FileUtils.sizeOf(file));
    }

    if (file.isDirectory()) {
        tOut.closeArchiveEntry();

        File children[] = file.listFiles();
        if (children == null)
            return true;

        for (int i = 0; i < children.length; i++) {

            if (!addFileToArchive(tOut, children[i], entryName + "/"))
                return false;
        }
    }

    return true;
}

From source file:com.mobilesorcery.sdk.builder.linux.deb.BuilderUtil.java

/**
 * Recursivly add a directory structure to a tar output stream
 *
 * @param t Tar output stream//from w  w  w  . j av  a2 s  .  co m
 * @param r Relative path up to this point
 * @param c Current file
 */
private void doAddFileToTar(TarArchiveOutputStream t, String r, File c) throws IOException {
    // Update relative path
    r += (r.isEmpty() ? "" : (r.endsWith("/") ? "" : "/")) + c.getName();

    // Is it a file?
    if (c.isFile() == true) {
        ArchiveEntry e = t.createArchiveEntry(c, r);
        t.putArchiveEntry(e);
        copyFileToOutputStream(t, c);
        t.closeArchiveEntry();
        return;
    }

    // It's a directory
    for (File f : c.listFiles())
        doAddFileToTar(t, r, f);
}

From source file:gov.nih.nci.nbia.servlet.DownloadServletV2.java

private void sendAnnotationData(List<AnnotationDTO> annoResults, TarArchiveOutputStream tos)
        throws IOException {
    InputStream annoIn = null;//from   ww w . j a  v a 2s  .  c o m
    for (AnnotationDTO a : annoResults) {
        String filePath = a.getFilePath();
        String fileName = a.getFileName();

        try {
            File annotationFile = new File(filePath);
            ArchiveEntry tarArchiveEntry = tos.createArchiveEntry(annotationFile, fileName);
            annoIn = new FileInputStream(annotationFile);
            tos.putArchiveEntry(tarArchiveEntry);
            IOUtils.copy(annoIn, tos);
            tos.closeArchiveEntry();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            // just print the exception and continue the loop so rest of
            // images will get download.
        } finally {
            IOUtils.closeQuietly(annoIn);
            logger.info("DownloadServlet Annotation transferred at " + new Date().getTime());
        }
    }
}

From source file:gov.nih.nci.nbia.servlet.DownloadServletV2.java

private void sendImagesData(List<ImageDTO2> imageResults, TarArchiveOutputStream tos) throws IOException {
    InputStream dicomIn = null;//  w ww . j av a  2s.  c  o m
    try {
        for (ImageDTO2 imageDto : imageResults) {
            String filePath = imageDto.getFileName();
            String sop = imageDto.getSOPInstanceUID();

            logger.info("filepath: " + filePath + " filename: " + sop);
            try {
                File dicomFile = new File(filePath);
                ArchiveEntry tarArchiveEntry = tos.createArchiveEntry(dicomFile, sop + ".dcm");
                dicomIn = new FileInputStream(dicomFile);
                tos.putArchiveEntry(tarArchiveEntry);
                IOUtils.copy(dicomIn, tos);
                tos.closeArchiveEntry();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                // just print the exception and continue the loop so rest of
                // images will get download.
            } finally {
                IOUtils.closeQuietly(dicomIn);
                logger.info("DownloadServlet Image transferred at " + new Date().getTime());
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:de.uzk.hki.da.pkg.NativeJavaTarArchiveBuilder.java

/**
 * There is an option to override the name of the first level entry if you want to pack 
 * a directory. Set includeFolder = true so that it not only packs the contents but also
 * the containing folder. Then use the setter setFirstLevelEntryName and set the name
 * of the folder which contains the files to pack. The name of the folder then gets replaced
 * in the resulting tar. Note that after calling archiveFolder once, the variable gets automatically
 * reset so that you have to call the setter again if you want to set the override setting again.
 *///from  w w w  .  j  a v  a  2s . co m
public void archiveFolder(File srcFolder, File destFile, boolean includeFolder) throws Exception {

    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    TarArchiveOutputStream tOut = null;

    fOut = new FileOutputStream(destFile);
    bOut = new BufferedOutputStream(fOut);
    tOut = new TarArchiveOutputStream(bOut);

    tOut.setLongFileMode(longFileMode);
    tOut.setBigNumberMode(bigNumberMode);

    try {

        String base = "";
        if (firstLevelEntryName.isEmpty())
            firstLevelEntryName = srcFolder.getName() + "/";

        if (includeFolder) {
            logger.debug("addFileToTar: " + firstLevelEntryName);
            TarArchiveEntry entry = (TarArchiveEntry) tOut.createArchiveEntry(srcFolder, firstLevelEntryName);
            tOut.putArchiveEntry(entry);
            tOut.closeArchiveEntry();
            base = firstLevelEntryName;
        }

        File children[] = srcFolder.listFiles();
        for (int i = 0; i < children.length; i++) {
            addFileToTar(tOut, children[i], base);
        }

    } finally {
        tOut.finish();

        tOut.close();
        bOut.close();
        fOut.close();

        firstLevelEntryName = "";
    }
}

From source file:net.zyuiop.remoteworldloader.utils.CompressionUtils.java

private static void addToZip(File directoryToZip, File file, TarArchiveOutputStream zos) throws IOException {

    FileInputStream fis = new FileInputStream(file);

    String filePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
            file.getCanonicalPath().length());
    Bukkit.getLogger().info(filePath);//from  w w  w.j a  v a 2  s  .  c  om
    ArchiveEntry zipEntry = zos.createArchiveEntry(file, filePath);
    zos.putArchiveEntry(zipEntry);

    final byte[] buf = new byte[8192];
    int bytesRead;
    while (-1 != (bytesRead = fis.read(buf)))
        zos.write(buf, 0, bytesRead);

    zos.closeArchiveEntry();
    fis.close();
}

From source file:org.slc.sli.bulk.extract.files.ExtractFile.java

private static void archiveFile(TarArchiveOutputStream tarArchiveOutputStream, File fileToArchive)
        throws IOException {
    tarArchiveOutputStream//from w  w w .java2 s  .c o  m
            .putArchiveEntry(tarArchiveOutputStream.createArchiveEntry(fileToArchive, fileToArchive.getName()));
    FileUtils.copyFile(fileToArchive, tarArchiveOutputStream);
    tarArchiveOutputStream.closeArchiveEntry();
    fileToArchive.delete();
}