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

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

Introduction

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

Prototype

public void setLongFileMode(int longFileMode) 

Source Link

Document

Set the long file mode.

Usage

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.  ja  v  a 2s . c om
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:com.gitblit.servlet.PtServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//from  www. ja  va2 s .  co  m
        response.setContentType("application/octet-stream");
        response.setDateHeader("Last-Modified", lastModified);
        response.setHeader("Cache-Control", "none");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);

        boolean windows = false;
        try {
            String useragent = request.getHeader("user-agent").toString();
            windows = useragent.toLowerCase().contains("windows");
        } catch (Exception e) {
        }

        byte[] pyBytes;
        File file = runtimeManager.getFileOrFolder("tickets.pt", "${baseFolder}/pt.py");
        if (file.exists()) {
            // custom script
            pyBytes = readAll(new FileInputStream(file));
        } else {
            // default script
            pyBytes = readAll(getClass().getResourceAsStream("/pt.py"));
        }

        if (windows) {
            // windows: download zip file with pt.py and pt.cmd
            response.setHeader("Content-Disposition", "attachment; filename=\"pt.zip\"");

            OutputStream os = response.getOutputStream();
            ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);

            // add the Python script
            ZipArchiveEntry pyEntry = new ZipArchiveEntry("pt.py");
            pyEntry.setSize(pyBytes.length);
            pyEntry.setUnixMode(FileMode.EXECUTABLE_FILE.getBits());
            pyEntry.setTime(lastModified);
            zos.putArchiveEntry(pyEntry);
            zos.write(pyBytes);
            zos.closeArchiveEntry();

            // add a Python launch cmd file
            byte[] cmdBytes = readAll(getClass().getResourceAsStream("/pt.cmd"));
            ZipArchiveEntry cmdEntry = new ZipArchiveEntry("pt.cmd");
            cmdEntry.setSize(cmdBytes.length);
            cmdEntry.setUnixMode(FileMode.REGULAR_FILE.getBits());
            cmdEntry.setTime(lastModified);
            zos.putArchiveEntry(cmdEntry);
            zos.write(cmdBytes);
            zos.closeArchiveEntry();

            // add a brief readme
            byte[] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt"));
            ZipArchiveEntry txtEntry = new ZipArchiveEntry("readme.txt");
            txtEntry.setSize(txtBytes.length);
            txtEntry.setUnixMode(FileMode.REGULAR_FILE.getBits());
            txtEntry.setTime(lastModified);
            zos.putArchiveEntry(txtEntry);
            zos.write(txtBytes);
            zos.closeArchiveEntry();

            // cleanup
            zos.finish();
            zos.close();
            os.flush();
        } else {
            // unix: download a tar.gz file with pt.py set with execute permissions
            response.setHeader("Content-Disposition", "attachment; filename=\"pt.tar.gz\"");

            OutputStream os = response.getOutputStream();
            CompressorOutputStream cos = new CompressorStreamFactory()
                    .createCompressorOutputStream(CompressorStreamFactory.GZIP, os);
            TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
            tos.setAddPaxHeadersForNonAsciiNames(true);
            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);

            // add the Python script
            TarArchiveEntry pyEntry = new TarArchiveEntry("pt");
            pyEntry.setMode(FileMode.EXECUTABLE_FILE.getBits());
            pyEntry.setModTime(lastModified);
            pyEntry.setSize(pyBytes.length);
            tos.putArchiveEntry(pyEntry);
            tos.write(pyBytes);
            tos.closeArchiveEntry();

            // add a brief readme
            byte[] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt"));
            TarArchiveEntry txtEntry = new TarArchiveEntry("README");
            txtEntry.setMode(FileMode.REGULAR_FILE.getBits());
            txtEntry.setModTime(lastModified);
            txtEntry.setSize(txtBytes.length);
            tos.putArchiveEntry(txtEntry);
            tos.write(txtBytes);
            tos.closeArchiveEntry();

            // cleanup
            tos.finish();
            tos.close();
            cos.close();
            os.flush();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

public void archiveFolder(File srcFolder, File destFile, boolean includeFolder) throws Exception {

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

    try {// w w  w  .  ja va  2s .c o  m
        fOut = new FileOutputStream(destFile);
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);

        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        tOut.setBigNumberMode(2);

        if (includeFolder)
            addFileToTarGZ(tOut, srcFolder, "");
        else {
            File children[] = srcFolder.listFiles();
            for (int i = 0; i < children.length; i++) {
                addFileToTarGZ(tOut, children[i], "");
            }
        }
    } finally {
        tOut.finish();

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

From source file:com.mulesoft.jockey.maven.GenerateMojo.java

private void createTarGz(File distDir) throws MojoExecutionException {
    File output = new File(buildDirectory, distributionName + ".tar.gz");
    try {//from   w  w w  . j  ava 2 s.  c o  m
        final OutputStream out = new FileOutputStream(output);
        TarArchiveOutputStream os = new TarArchiveOutputStream(new GZIPOutputStream(out));
        os.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        copyArchiveFile(distDir, os, false);

        os.finish();

        os.close();
        out.close();
    } catch (IOException e) {
        throw new MojoExecutionException("Could not create zip file.", e);
    }
    projectHelper.attachArtifact(project, "tar.gz", "", output);
}

From source file:freenet.client.async.ContainerInserter.java

/**
** OutputStream os will be close()d if this method returns successfully.
*///from   w  w  w  .  j av a 2s  .  c  om
private String createTarBucket(OutputStream os) throws IOException {
    if (logMINOR)
        Logger.minor(this, "Create a TAR Bucket");

    TarArchiveOutputStream tarOS = new TarArchiveOutputStream(os);
    try {
        tarOS.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        TarArchiveEntry ze;

        for (ContainerElement ph : containerItems) {
            if (logMINOR)
                Logger.minor(this, "Putting into tar: " + ph + " data length " + ph.data.size() + " name "
                        + ph.targetInArchive);
            ze = new TarArchiveEntry(ph.targetInArchive);
            ze.setModTime(0);
            long size = ph.data.size();
            ze.setSize(size);
            tarOS.putArchiveEntry(ze);
            BucketTools.copyTo(ph.data, tarOS, size);
            tarOS.closeArchiveEntry();
        }
    } finally {
        tarOS.close();
    }

    return ARCHIVE_TYPE.TAR.mimeTypes[0];
}

From source file:com.dotcms.publisher.myTest.PushPublisher.java

/**
 * Compress (tar.gz) the input files to the output file
 *
 * @param files The files to compress/*from   w w  w  . ja  va  2s  .co  m*/
 * @param output The resulting output file (should end in .tar.gz)
 * @param bundleRoot
 * @throws IOException
 */
private void compressFiles(Collection<File> files, File output, String bundleRoot) throws IOException {
    Logger.info(this.getClass(), "Compressing " + files.size() + " to " + output.getAbsoluteFile());
    // Create the output stream for the output file
    FileOutputStream fos = new FileOutputStream(output);
    // Wrap the output file stream in streams that will tar and gzip everything
    TarArchiveOutputStream taos = new TarArchiveOutputStream(
            new GZIPOutputStream(new BufferedOutputStream(fos)));

    // TAR originally didn't support long file names, so enable the support for it
    taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    // Get to putting all the files in the compressed output file
    for (File f : files) {
        addFilesToCompression(taos, f, ".", bundleRoot);
    }

    // Close everything up
    taos.close();
    fos.close();
}

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

/**
 * Create an archive file out of the given source folder
 * //w w w . ja  v  a2 s.com
 * @param srcFolder The folder to archive
 * @param destFile The archive file to build
 * @param includeFolder Indicates if the source folder will be included to the archive file on
 * the first level or not
 * @param compress Indicates if the archive file will be compressed (tgz file) or not (tar file)
 * @return false if the SIP creation process was aborted during the archive file creation process, otherwise true
 * @throws Exception
 */
public boolean archiveFolder(File srcFolder, File destFile, boolean includeFolder, boolean compress)
        throws Exception {

    FileOutputStream fOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;

    try {
        fOut = new FileOutputStream(destFile);

        if (compress) {
            gzOut = new GzipCompressorOutputStream(fOut);
            tOut = new TarArchiveOutputStream(gzOut, "UTF-8");
        } else
            tOut = new TarArchiveOutputStream(fOut, "UTF-8");

        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        tOut.setBigNumberMode(2);

        if (includeFolder) {
            if (!addFileToArchive(tOut, srcFolder, ""))
                return false;
        } else {
            File children[] = srcFolder.listFiles();

            for (int i = 0; i < children.length; i++) {
                if (!addFileToArchive(tOut, children[i], ""))
                    return false;
            }
        }
    } finally {
        tOut.finish();
        tOut.close();
        if (gzOut != null)
            gzOut.close();
        fOut.close();
    }

    return true;
}

From source file:hudson.gridmaven.gridlayer.HadoopInstance.java

private void addFileToTar(TarArchiveOutputStream tOut, String path, String base, String root)
        throws IOException {
    if (!root.equals(path)) {
        File f = new File(path);
        String entryName = base + f.getName();
        TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        tOut.putArchiveEntry(tarEntry);/*from  w ww.j a v  a 2  s  . c om*/

        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) {
                    addFileToTar(tOut, child.getAbsolutePath(), entryName + "/", root);
                }
            }
        }
    } else {
        File f = new File(path);
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToTar(tOut, child.getAbsolutePath(), "", root);
            }
        }
    }
}

From source file:com.st.maven.debian.DebianPackageMojo.java

private void fillDataTar(Config config, ArFileOutputStream output) throws MojoExecutionException {
    TarArchiveOutputStream tar = null;
    try {/*from   ww w .  ja  v a2s.c o m*/
        tar = new TarArchiveOutputStream(new GZIPOutputStream(new ArWrapper(output)));
        tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        if (Boolean.TRUE.equals(javaServiceWrapper)) {
            byte[] daemonData = processTemplate(freemarkerConfig, config, "daemon.ftl");
            TarArchiveEntry initScript = new TarArchiveEntry("etc/init.d/" + project.getArtifactId());
            initScript.setSize(daemonData.length);
            initScript.setMode(040755);
            tar.putArchiveEntry(initScript);
            tar.write(daemonData);
            tar.closeArchiveEntry();
        }
        String packageBaseDir = "home/" + unixUserId + "/" + project.getArtifactId() + "/";
        if (fileSets != null && !fileSets.isEmpty()) {
            writeDirectory(tar, packageBaseDir);

            Collections.sort(fileSets, MappingPathComparator.INSTANCE);
            for (Fileset curPath : fileSets) {
                curPath.setTarget(packageBaseDir + curPath.getTarget());
                addRecursively(config, tar, curPath);
            }
        }

    } catch (Exception e) {
        throw new MojoExecutionException("unable to create data tar", e);
    } finally {
        IOUtils.closeQuietly(tar);
    }
}

From source file:com.st.maven.debian.DebianPackageMojo.java

private void fillControlTar(Config config, ArFileOutputStream output) throws MojoExecutionException {
    TarArchiveOutputStream tar = null;
    try {//from w  ww  .  ja va  2s. c o m
        tar = new TarArchiveOutputStream(new GZIPOutputStream(new ArWrapper(output)));
        tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        TarArchiveEntry rootDir = new TarArchiveEntry("./");
        tar.putArchiveEntry(rootDir);
        tar.closeArchiveEntry();

        byte[] controlData = processTemplate(freemarkerConfig, config, "control.ftl");
        TarArchiveEntry controlEntry = new TarArchiveEntry("./control");
        controlEntry.setSize(controlData.length);
        tar.putArchiveEntry(controlEntry);
        tar.write(controlData);
        tar.closeArchiveEntry();

        byte[] preinstBaseData = processTemplate("preinst", freemarkerConfig, config,
                combine("preinst.ftl", BASE_DIR + File.separator + "preinst", false));
        long size = preinstBaseData.length;
        TarArchiveEntry preinstEntry = new TarArchiveEntry("./preinst");
        preinstEntry.setSize(size);
        preinstEntry.setMode(0755);
        tar.putArchiveEntry(preinstEntry);
        tar.write(preinstBaseData);
        tar.closeArchiveEntry();

        byte[] postinstBaseData = processTemplate("postinst", freemarkerConfig, config,
                combine("postinst.ftl", BASE_DIR + File.separator + "postinst", true));
        size = postinstBaseData.length;
        TarArchiveEntry postinstEntry = new TarArchiveEntry("./postinst");
        postinstEntry.setSize(size);
        postinstEntry.setMode(0755);
        tar.putArchiveEntry(postinstEntry);
        tar.write(postinstBaseData);
        tar.closeArchiveEntry();

        byte[] prermBaseData = processTemplate("prerm", freemarkerConfig, config,
                combine("prerm.ftl", BASE_DIR + File.separator + "prerm", false));
        size = prermBaseData.length;
        TarArchiveEntry prermEntry = new TarArchiveEntry("./prerm");
        prermEntry.setSize(size);
        prermEntry.setMode(0755);
        tar.putArchiveEntry(prermEntry);
        tar.write(prermBaseData);
        tar.closeArchiveEntry();

        byte[] postrmBaseData = processTemplate("postrm", freemarkerConfig, config,
                combine("postrm.ftl", BASE_DIR + File.separator + "postrm", false));
        size = postrmBaseData.length;
        TarArchiveEntry postrmEntry = new TarArchiveEntry("./postrm");
        postrmEntry.setSize(size);
        postrmEntry.setMode(0755);
        tar.putArchiveEntry(postrmEntry);
        tar.write(postrmBaseData);
        tar.closeArchiveEntry();

    } catch (Exception e) {
        throw new MojoExecutionException("unable to create control tar", e);
    } finally {
        if (tar != null) {
            try {
                tar.close();
            } catch (IOException e) {
                getLog().error("unable to finish tar", e);
            }
        }
    }
}