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:com.linuxbox.enkive.workspace.searchFolder.SearchFolder.java

/**
 * Writes a tar.gz file to the provided outputstream
 * // w  ww  . j  a  v  a 2 s  . c  om
 * @param outputStream
 * @throws IOException
 */
public void exportSearchFolder(OutputStream outputStream) throws IOException {
    BufferedOutputStream out = new BufferedOutputStream(outputStream);
    GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out);
    TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut);

    File mboxFile = File.createTempFile("mbox-export", ".mbox");
    BufferedWriter mboxWriter = new BufferedWriter(new FileWriter(mboxFile));
    // Write mbox to tempfile?
    for (String messageId : getMessageIds()) {
        try {
            Message message = retrieverService.retrieve(messageId);

            mboxWriter.write("From " + message.getDateStr() + "\r\n");
            BufferedReader reader = new BufferedReader(new StringReader(message.getReconstitutedEmail()));
            String tmpLine;
            while ((tmpLine = reader.readLine()) != null) {
                if (tmpLine.startsWith("From "))
                    mboxWriter.write(">" + tmpLine);
                else
                    mboxWriter.write(tmpLine);
                mboxWriter.write("\r\n");
            }
        } catch (CannotRetrieveException e) {
            // Add errors to report
            // if (LOGGER.isErrorEnabled())
            // LOGGER.error("Could not retrieve message with id"
            // + messageId);
        }
    }
    mboxWriter.flush();
    mboxWriter.close();
    // Add mbox to tarfile
    TarArchiveEntry mboxEntry = new TarArchiveEntry(mboxFile, "filename.mbox");
    tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tOut.putArchiveEntry(mboxEntry);
    IOUtils.copy(new FileInputStream(mboxFile), tOut);
    tOut.flush();
    tOut.closeArchiveEntry();
    mboxWriter.close();
    mboxFile.delete();
    // Create report in tempfile?

    // Add report to tarfile

    // Close out stream
    tOut.finish();
    outputStream.flush();
    tOut.close();
    outputStream.close();

}

From source file:ca.nrc.cadc.caom2.pkg.TarWriter.java

public TarWriter(OutputStream ostream) {
    this.tout = new TarArchiveOutputStream(ostream);
    tout.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
}

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 {// www. j a  v a2s  .  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:deployer.TestUtils.java

public static ByteBuffer createSampleAppTarBall(ArtifactType type) throws IOException, ArchiveException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8096);
    CompressorOutputStream gzs = new GzipCompressorOutputStream(bos);
    ArchiveOutputStream aos = new TarArchiveOutputStream(gzs);
    {//from  w  ww. j a  v a 2  s  .c om
        TarArchiveEntry nextEntry = new TarArchiveEntry(CONFIG_DIRECTORY + "/app.conf");
        byte[] sampleConf = SAMPLE_CONF_DATA.getBytes();
        nextEntry.setSize(sampleConf.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(sampleConf, aos);
        aos.closeArchiveEntry();
    }
    if (type != ArtifactType.WebApp) {
        TarArchiveEntry nextEntry = new TarArchiveEntry("bin/myApplication.jar");
        byte[] jarData = SAMPLE_JAR_DATA.getBytes();
        nextEntry.setSize(jarData.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(jarData, aos);
        aos.closeArchiveEntry();
    } else {
        TarArchiveEntry nextEntry = new TarArchiveEntry("deployments/ROOT.war");
        byte[] jarData = SAMPLE_JAR_DATA.getBytes();
        nextEntry.setSize(jarData.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(jarData, aos);
        aos.closeArchiveEntry();
    }
    aos.finish();
    gzs.close();
    bos.flush();
    return ByteBuffer.wrap(bos.toByteArray());
}

From source file:com.fizzed.stork.deploy.Archive.java

static private ArchiveOutputStream newArchiveOutputStream(Path file, String format) throws IOException {
    switch (format) {
    case "tar.gz":
        TarArchiveOutputStream tgzout = new TarArchiveOutputStream(
                new GzipCompressorOutputStream(Files.newOutputStream(file)));
        tgzout.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
        tgzout.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        return tgzout;
    case "zip":
        return new ZipArchiveOutputStream(Files.newOutputStream(file));
    default:/*from w w w  . j  ava 2 s .c om*/
        throw new IOException("Unsupported archive file type (we support .tar.gz and .zip)");
    }
}

From source file:com.flurry.proguard.UploadProGuardMapping.java

/**
 * Create a gzipped tar archive containing the ProGuard mapping file
 *
 * @param file the mapping.txt file/*from  w ww. j  ava2  s .  c  o m*/
 * @param uuid the build uuid
 * @return the tar-gzipped archive
 */
private static File createArchive(File file, String uuid) {
    try {
        File tarZippedFile = File.createTempFile("tar-zipped-file", ".tgz");
        TarArchiveOutputStream taos = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(tarZippedFile))));
        taos.putArchiveEntry(new TarArchiveEntry(file, uuid + ".txt"));
        IOUtils.copy(new FileInputStream(file), taos);
        taos.closeArchiveEntry();
        taos.finish();
        taos.close();
        return tarZippedFile;
    } catch (IOException e) {
        failWithError("IO Exception while trying to tar and zip the file.", e);
        return null;
    }
}

From source file:com.netflix.spinnaker.halyard.backup.services.v1.BackupService.java

private void tarHalconfig(String halconfigDir, String halconfigTar) throws IOException {
    FileOutputStream tarOutput = null;
    BufferedOutputStream bufferedTarOutput = null;
    TarArchiveOutputStream tarArchiveOutputStream = null;
    IOException fatalCleanup = null;
    try {/*from  ww  w  .j  a  va2s  .c  om*/
        tarOutput = new FileOutputStream(new File(halconfigTar));
        bufferedTarOutput = new BufferedOutputStream(tarOutput);
        tarArchiveOutputStream = new TarArchiveOutputStream(bufferedTarOutput);
        TarArchiveOutputStream finalTarArchiveOutputStream = tarArchiveOutputStream;
        Arrays.stream(new File(halconfigDir).listFiles()).filter(Objects::nonNull)
                .forEach(f -> addFileToTar(finalTarArchiveOutputStream, f.getAbsolutePath(), ""));
    } catch (HalException e) {
        log.info("HalException caught during tar operation", e);
        throw e;
    } catch (IOException e) {
        log.info("IOException caught during tar operation", e);
        throw new HalException(Problem.Severity.FATAL, "Failed to backup halconfig: " + e.getMessage(), e);
    } finally {
        if (tarArchiveOutputStream != null) {
            try {
                tarArchiveOutputStream.finish();
                tarArchiveOutputStream.close();
            } catch (IOException e) {
                fatalCleanup = e;
            }
        }

        if (bufferedTarOutput != null) {
            bufferedTarOutput.close();
        }

        if (tarOutput != null) {
            tarOutput.close();
        }
    }

    if (fatalCleanup != null) {
        throw fatalCleanup;
    }
}

From source file:com.gitblit.servlet.PtServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//from  w  w  w  .j  a v a 2 s . com
        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:com.facebook.buck.artifact_cache.ArtifactUploader.java

/** Archive and compress 'pathsToIncludeInArchive' into 'out', using tar+zstandard. */
@VisibleForTesting//  www . java 2 s  . co m
static void compress(ProjectFilesystem projectFilesystem, Collection<Path> pathsToIncludeInArchive, Path out)
        throws IOException {
    try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(out));
            OutputStream z = new ZstdCompressorOutputStream(o);
            TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
        archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        for (Path path : pathsToIncludeInArchive) {
            boolean isRegularFile = !projectFilesystem.isDirectory(path);

            // Add a file entry.
            TarArchiveEntry e = new TarArchiveEntry(path.toString() + (isRegularFile ? "" : "/"));
            e.setMode((int) projectFilesystem.getPosixFileMode(path));
            e.setModTime(ZipConstants.getFakeTime());

            if (isRegularFile) {
                e.setSize(projectFilesystem.getFileSize(path));
                archive.putArchiveEntry(e);
                try (InputStream input = projectFilesystem.newFileInputStream(path)) {
                    ByteStreams.copy(input, archive);
                }
            } else {
                archive.putArchiveEntry(e);
            }
            archive.closeArchiveEntry();
        }
        archive.finish();
    }
}

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

/**
 * This method opens a path and recursively adds all files into tar, then
 * inserts it to HDFS./*from   w ww  .j a va 2s .co  m*/
 */
public void tarAndInsert(String directoryPath, String tarGzPath) throws IOException {
    OutputStream fOut = null;
    //FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    TarArchiveOutputStream tOut = null;
    //tarGzPath = "test.tar";
    //File f = new File(tarGzPath);
    Path f = new Path(tarGzPath);

    String skipDirectoryPath = "";
    File l = new File(directoryPath);
    if (l.isDirectory())
        skipDirectoryPath = directoryPath;
    try {
        fs.delete(f, true);
        fOut = fs.create(f);
        //fOut = new FileOutputStream(f);
        bOut = new BufferedOutputStream(fOut);
        tOut = new TarArchiveOutputStream(bOut);

        addFileToTar(tOut, directoryPath, "", skipDirectoryPath);

        tOut.finish();
        tOut.close();
        bOut.close();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
        Logger.getLogger(HadoopInstance.class.getName()).log(Level.SEVERE, null, e);
    }
}