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:org.dspace.pack.bagit.Bag.java

public void deflate(OutputStream out, String fmt) throws IOException {
    if (!filled) {
        throw new IllegalStateException("Cannot deflate unfilled bag");
    }//from  w  w  w  . j  av  a 2s .  c  o m
    if ("zip".equals(fmt)) {
        ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(out));
        fillZip(baseDir, baseDir.getName(), zout);
        zout.close();
    } else if ("tgz".equals(fmt)) {
        TarArchiveOutputStream tout = new TarArchiveOutputStream(
                new BufferedOutputStream(new GzipCompressorOutputStream(out)));
        fillArchive(baseDir, baseDir.getName(), tout);
        tout.close();
    }
}

From source file:org.eclipse.che.api.vfs.TarArchiver.java

@Override
public void compress(OutputStream tarOutput, VirtualFileFilter filter) throws IOException, ServerException {
    try (TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(tarOutput)) {
        tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        folder.accept(new VirtualFileVisitor() {
            @Override//  w w  w  . jav a  2  s.co  m
            public void visit(VirtualFile visitedVirtualFile) throws ServerException {
                if (filter.accept(visitedVirtualFile)) {
                    if (!visitedVirtualFile.equals(folder)) {
                        addTarEntry(visitedVirtualFile, tarOutputStream);
                    }
                    if (visitedVirtualFile.isFolder()) {
                        for (VirtualFile child : visitedVirtualFile.getChildren()) {
                            child.accept(this);
                        }
                    }
                }
            }
        });
    }
}

From source file:org.eclipse.che.api.vfs.TarArchiverTest.java

private byte[] createTestTarArchive() throws IOException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    TarArchiveOutputStream tarOut = new TarArchiveOutputStream(byteOut);
    addDirectoryEntry(tarOut, new TarArchiveEntry("arc/"));
    addDirectoryEntry(tarOut, new TarArchiveEntry("arc/a/"));
    addFileEntry(tarOut, "arc/a/_a.txt");
    addDirectoryEntry(tarOut, new TarArchiveEntry("arc/b/"));
    addFileEntry(tarOut, "arc/b/_b.txt");
    addDirectoryEntry(tarOut, new TarArchiveEntry("arc/c/"));
    addFileEntry(tarOut, "arc/c/_c.txt");
    tarOut.close();/*from w w w.java  2  s.c  o  m*/
    return byteOut.toByteArray();
}

From source file:org.eclipse.orion.server.docker.server.DockerFile.java

/**
 * Get the tar file containing the Dockerfile that can be sent to the Docker 
 * build API to create an image. /*  ww  w. j a v a 2 s.  c o m*/
 * 
 * @return The tar file.
 */
public File getTarFile() {
    try {
        // get the temporary folder location.
        String tmpDirName = System.getProperty("java.io.tmpdir");
        File tmpDir = new File(tmpDirName);
        if (!tmpDir.exists() || !tmpDir.isDirectory()) {
            if (logger.isDebugEnabled()) {
                logger.error("Cannot find the default temporary-file directory: " + tmpDirName);
            }
            return null;
        }

        // get a temporary folder name
        long n = random.nextLong();
        n = (n == Long.MIN_VALUE) ? 0 : Math.abs(n);
        String tmpDirStr = Long.toString(n);
        tempFolder = new File(tmpDir, tmpDirStr);
        if (!tempFolder.mkdir()) {
            if (logger.isDebugEnabled()) {
                logger.error("Cannot create a temporary directory at " + tempFolder.toString());
            }
            return null;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Dockerfile: Created a temporary directory at " + tempFolder.toString());
        }

        // create the Dockerfile
        dockerfile = new File(tempFolder, "Dockerfile");
        FileOutputStream fileOutputStream = new FileOutputStream(dockerfile);
        Charset utf8 = Charset.forName("UTF-8");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, utf8);
        outputStreamWriter.write(getDockerfileContent());
        outputStreamWriter.flush();
        outputStreamWriter.close();
        fileOutputStream.close();

        dockerTarFile = new File(tempFolder, "Dockerfile.tar");

        TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(
                new FileOutputStream(dockerTarFile));
        tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        TarArchiveEntry tarEntry = new TarArchiveEntry(dockerfile);
        tarEntry.setName(dockerfile.getName());
        tarArchiveOutputStream.putArchiveEntry(tarEntry);

        FileInputStream fileInputStream = new FileInputStream(dockerfile);
        BufferedInputStream inputStream = new BufferedInputStream(fileInputStream);
        byte[] buffer = new byte[4096];
        int bytes_read;
        while ((bytes_read = inputStream.read(buffer)) != -1) {
            tarArchiveOutputStream.write(buffer, 0, bytes_read);
        }
        inputStream.close();

        tarArchiveOutputStream.closeArchiveEntry();
        tarArchiveOutputStream.close();

        if (logger.isDebugEnabled()) {
            logger.debug("Dockerfile: Created a docker tar file at " + dockerTarFile.toString());
        }
        return dockerTarFile;
    } catch (IOException e) {
        logger.error(e.getLocalizedMessage(), e);
    }
    return null;
}

From source file:org.eclipse.packagedrone.utils.deb.build.DebianPackageWriter.java

public DebianPackageWriter(final OutputStream stream, final BinaryPackageControlFile packageControlFile)
        throws IOException {
    this.packageControlFile = packageControlFile;
    BinaryPackageControlFile.validate(packageControlFile);

    this.ar = new ArArchiveOutputStream(stream);

    this.ar.putArchiveEntry(new ArArchiveEntry("debian-binary", this.binaryHeader.length));
    this.ar.write(this.binaryHeader);
    this.ar.closeArchiveEntry();

    this.dataTemp = File.createTempFile("data", null);

    this.dataStream = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(this.dataTemp)));
    this.dataStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
}

From source file:org.eclipse.packagedrone.utils.deb.build.DebianPackageWriter.java

private void buildAndAddControlFile() throws IOException, FileNotFoundException {
    final File controlFile = File.createTempFile("control", null);
    try {//  ww w .j a  va  2s  .c o  m
        try (GZIPOutputStream gout = new GZIPOutputStream(new FileOutputStream(controlFile));
                TarArchiveOutputStream tout = new TarArchiveOutputStream(gout)) {
            tout.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

            addControlContent(tout, "control", createControlContent(), -1);
            addControlContent(tout, "md5sums", createChecksumContent(), -1);
            addControlContent(tout, "conffiles", createConfFilesContent(), -1);
            addControlContent(tout, "preinst", this.preinstScript,
                    EntryInformation.DEFAULT_FILE_EXEC.getMode());
            addControlContent(tout, "prerm", this.prermScript, EntryInformation.DEFAULT_FILE_EXEC.getMode());
            addControlContent(tout, "postinst", this.postinstScript,
                    EntryInformation.DEFAULT_FILE_EXEC.getMode());
            addControlContent(tout, "postrm", this.postrmScript, EntryInformation.DEFAULT_FILE_EXEC.getMode());
        }
        addArFile(controlFile, "control.tar.gz");
    } finally {
        controlFile.delete();
    }
}

From source file:org.eclipse.scada.utils.pkg.deb.DebianPackageWriter.java

public DebianPackageWriter(final OutputStream stream, final GenericControlFile packageControlFile,
        final TimestampProvider timestampProvider) throws IOException {
    this.packageControlFile = packageControlFile;
    this.timestampProvider = timestampProvider;
    if (getTimestampProvider() == null) {
        throw new IllegalArgumentException("'timestampProvider' must not be null");
    }// ww w . j a  v  a2 s.c  o  m
    BinaryPackageControlFile.validate(packageControlFile);

    this.ar = new ArArchiveOutputStream(stream);

    this.ar.putArchiveEntry(new ArArchiveEntry("debian-binary", this.binaryHeader.length, 0, 0,
            AR_ARCHIVE_DEFAULT_MODE, getTimestampProvider().getModTime() / 1000));
    this.ar.write(this.binaryHeader);
    this.ar.closeArchiveEntry();

    this.dataTemp = File.createTempFile("data", null);

    this.dataStream = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(this.dataTemp)));
    this.dataStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
}

From source file:org.eclipse.tracecompass.integration.swtbot.tests.projectexplorer.TestDirectoryStructureUtil.java

/**
 * Generate a directory structure as follows
 *
 * <pre>/* ww w  .j  av a 2s.co m*/
 * parentDir
 *   archives
 *    traces.zip
 *    traces.tar.gz
 *   customParsers
 *    ExampleCustomTxtParser.xml
 *    ExampleCustomXmlParser.xml
 *   import
 *       z-clashes
 *        ExampleCustomTxt.log
 *        ExampleCustomXml.xml
 *        kernel-overlap-testing
 *         stream
 *         metadata
 *        simple_server-thread1
 *         stream
 *         metadata
 *        simple_server-thread2
 *         stream
 *         metadata
 *        ust-overlap-testing
 *            stream
 *            metadata
 *       empty
 *       ExampleCustomTxt.log
 *       ExampleCustomXml.xml
 *       kernel-overlap-testing
 *        stream
 *        metadata
 *       simple_server-thread1
 *        metadata
 *        stream
 *       simple_server-thread2
 *        metadata
 *        stream
 *       unrecognized.log
 *       ust-overlap-testing
 *           stream
 *           metadata
 * </pre>
 *
 * @param parentDir
 *            the directory to use as the parent
 * @return the structure detailed above
 * @throws IOException
 *             out of space or permission problem
 */
public static File generateTraceStructure(File parentDir) throws IOException {
    File parent = (parentDir == null) ? File.createTempFile("Traces", "") : parentDir;
    if (!parent.isDirectory()) {
        parent.delete();
        parent.mkdir();
    }
    File customParser = createDir(parent, "customParsers");
    createFile(customParser, "ExampleCustomTxtParser.xml", CUSTOM_TEXT_PARSER_CONTENT);
    createFile(customParser, "ExampleCustomXmlParser.xml", CUSTOM_XML_PARSER_CONTENT);
    File importDir = createDir(parent, "import");
    createDir(importDir, "empty");
    createFile(importDir, "ExampleCustomTxt.log", CUSTOM_TEXT_CONTENT);
    createFile(importDir, "ExampleCustomXml.xml", CUSTOM_XML_CONTENT);
    createFile(importDir, "unrecognized.log", UNRECOGNIZED_LOG_CONTENT);
    // Using the z- prefix so that the traces in this folder are imported
    // last by the import wizard
    final String CLASHES_DIR_NAME = "z-clashes";
    File theClash = createDir(importDir, CLASHES_DIR_NAME);

    // We're making the clash version of each trace slightly different in content to help differentiate them
    createFile(theClash, "ExampleCustomTxt.log", CUSTOM_TEXT_CONTENT + CUSTOM_TEXT_LAST_LINE);
    createFile(theClash, "ExampleCustomXml.xml", CUSTOM_XML_CONTENT + CUSTOM_XML_PARSER_LAST_LINE);

    LttngTraceGenerator kernelGenerator = new LttngTraceGenerator(1000, 1000, 1);
    LttngTraceGenerator ustGenerator = new LttngTraceGenerator(1000, 1000, 1, false);
    kernelGenerator
            .writeTrace(new File(importDir.getAbsolutePath() + File.separator + "kernel-overlap-testing"));
    ustGenerator.writeTrace(new File(importDir.getAbsolutePath() + File.separator + "ust-overlap-testing"));
    ustGenerator.writeTrace(new File(importDir.getAbsolutePath() + File.separator + "simple_server-thread1"));
    ustGenerator.writeTrace(new File(importDir.getAbsolutePath() + File.separator + "simple_server-thread2"));

    kernelGenerator = new LttngTraceGenerator(1000, 1001, 1);
    ustGenerator = new LttngTraceGenerator(1000, 1001, 1, false);
    kernelGenerator.writeTrace(new File(importDir.getAbsolutePath() + File.separator + CLASHES_DIR_NAME
            + File.separator + "kernel-overlap-testing"));
    ustGenerator.writeTrace(new File(importDir.getAbsolutePath() + File.separator + CLASHES_DIR_NAME
            + File.separator + "ust-overlap-testing"));
    ustGenerator.writeTrace(new File(importDir.getAbsolutePath() + File.separator + CLASHES_DIR_NAME
            + File.separator + "simple_server-thread1"));
    ustGenerator.writeTrace(new File(importDir.getAbsolutePath() + File.separator + CLASHES_DIR_NAME
            + File.separator + "simple_server-thread2"));

    assertTrue(parent.listFiles().length > 0);

    File archivesDir = createDir(parent, "archives");
    File zipFile = new File(archivesDir.getAbsolutePath() + File.separator + "traces.zip");
    try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
        addToArchive(zos, importDir, importDir);
    }
    File targzFile = new File(archivesDir.getAbsolutePath() + File.separator + "traces.tar.gz");
    try (TarArchiveOutputStream tgzos = new TarArchiveOutputStream(
            new GZIPOutputStream(new FileOutputStream(targzFile)))) {
        addToArchive(tgzos, importDir, importDir);
    }
    return parent;
}

From source file:org.exist.xquery.modules.compression.TarFunction.java

@Override
protected OutputStream stream(ByteArrayOutputStream baos) {
    return new TarArchiveOutputStream(baos);
}

From source file:org.fabrician.maven.plugins.GridlibPackageMojo.java

private void createTar(File distroSourceFile, FilenamePatternFilter filter) throws MojoExecutionException {
    distroFilename.getParentFile().mkdirs();
    FileOutputStream out = null;/*from w  w  w . j a  va 2  s  .  co m*/
    CompressorOutputStream cout = null;
    TarArchiveOutputStream tout = null;
    try {
        out = new FileOutputStream(distroFilename);
        cout = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, out);
        tout = new TarArchiveOutputStream(cout);
        tout.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        if (distroSourceFile.isDirectory()) {
            CompressUtils.copyDirToArchiveOutputStream(distroSourceFile, filter, tout,
                    distroAlternateRootDirectory);
        } else if (CompressUtils.isZip(distroSourceFile)) {
            CompressUtils.copyZipToArchiveOutputStream(distroSourceFile, filter, tout,
                    distroAlternateRootDirectory);
        } else if (CompressUtils.isTargz(distroSourceFile)) {
            CompressUtils.copyTargzToArchiveOutputStream(distroSourceFile, filter, tout,
                    distroAlternateRootDirectory);
        } else {
            throw new MojoExecutionException("Unspported source type: " + distroSource);
        }
        if (distroResources != null && !"".equals(distroResources)) {
            if (filtered) {
                CompressUtils.copyFilteredDirToArchiveOutputStream(distroResources, getFilterProperties(),
                        tout);
            } else {
                CompressUtils.copyDirToArchiveOutputStream(distroResources, tout, distroAlternateRootDirectory);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new MojoExecutionException(e.getMessage());
    } finally {
        CompressUtils.close(tout);
        CompressUtils.close(cout);
        CompressUtils.close(out);
    }
}