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

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

Introduction

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

Prototype

public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException 

Source Link

Document

Put an entry on the output stream.

Usage

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

private void addDirectoryEntry(TarArchiveOutputStream tarOut, TarArchiveEntry archiveEntry) throws IOException {
    tarOut.putArchiveEntry(archiveEntry);
    tarOut.closeArchiveEntry();/*from  w  ww .  j a v  a  2  s. c o m*/
}

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

private void addFileEntry(TarArchiveOutputStream tarOut, String name) throws IOException {
    TarArchiveEntry entryA = new TarArchiveEntry(name);
    entryA.setSize(TEST_CONTENT_BYTES.length);
    tarOut.putArchiveEntry(entryA);
    tarOut.write(TEST_CONTENT_BYTES);//  ww  w .j a  v a2s . c  om
    tarOut.closeArchiveEntry();
}

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. //from  ww  w  . ja v a 2s .  c om
 * 
 * @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

private void addControlContent(final TarArchiveOutputStream out, final String name,
        final ContentProvider content, final int mode) throws IOException {
    if (content == null || !content.hasContent()) {
        return;//from w  w  w  .j a v  a  2 s.  c  o  m
    }

    final TarArchiveEntry entry = new TarArchiveEntry(name);
    if (mode >= 0) {
        entry.setMode(mode);
    }

    entry.setUserName("root");
    entry.setGroupName("root");
    entry.setSize(content.getSize());
    out.putArchiveEntry(entry);
    try (InputStream stream = content.createInputStream()) {
        IOUtils.copy(stream, out);
    }
    out.closeArchiveEntry();
}

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

private void addControlContent(final TarArchiveOutputStream out, final String name,
        final ContentProvider content, final int mode) throws IOException {
    if (content == null || !content.hasContent()) {
        return;/* ww  w.j  ava  2  s. c  o  m*/
    }

    final TarArchiveEntry entry = new TarArchiveEntry(name);
    if (mode >= 0) {
        entry.setMode(mode);
    }

    entry.setUserName("root");
    entry.setGroupName("root");
    entry.setSize(content.getSize());
    entry.setModTime(this.getTimestampProvider().getModTime());
    out.putArchiveEntry(entry);
    try (InputStream stream = content.createInputStream()) {
        ByteStreams.copy(stream, out);
    }
    out.closeArchiveEntry();
}

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

private static void addToArchive(TarArchiveOutputStream taos, File dir, File root) throws IOException {
    byte[] buffer = new byte[1024];
    int length;//from w w w. j av  a2  s .co m
    int index = root.getAbsolutePath().length();
    for (File file : dir.listFiles()) {
        String name = file.getAbsolutePath().substring(index);
        if (file.isDirectory()) {
            if (file.listFiles().length != 0) {
                addToArchive(taos, file, root);
            } else {
                taos.putArchiveEntry(new TarArchiveEntry(name + File.separator));
                taos.closeArchiveEntry();
            }
        } else {
            try (FileInputStream fis = new FileInputStream(file)) {
                TarArchiveEntry entry = new TarArchiveEntry(name);
                entry.setSize(file.length());
                taos.putArchiveEntry(entry);
                while ((length = fis.read(buffer)) > 0) {
                    taos.write(buffer, 0, length);
                }
                taos.closeArchiveEntry();
            }
        }
    }
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiver.java

private void addToTarRecursively(File tarRootDir, File source, TarArchiveOutputStream tarStream)
        throws IOException {
    TarArchiveEntry tarEntry = createTarEntry(tarRootDir, source);
    tarStream.putArchiveEntry(tarEntry);
    if (source.isFile() && !tarEntry.isSymbolicLink()) {
        copyFileContentToTarStream(source, tarStream);
    }/*from w ww .  jav  a 2 s . c om*/
    tarStream.closeArchiveEntry();
    if (source.isDirectory()) {
        File[] children = source.listFiles();
        if (children != null) {
            for (File child : children) {
                addToTarRecursively(tarRootDir, child, tarStream);
            }
        }
    }
}

From source file:org.finra.herd.service.helper.TarHelper.java

/**
 * Adds a TAR archive entry to the specified TAR archive stream. The method calls itself recursively for all directories/files found.
 *
 * @param tarArchiveOutputStream the TAR output stream that writes a UNIX tar archive as an output stream
 * @param path the path relative to the base for a directory or file to be added to the TAR archive stream
 * @param base the base for the directory or file path
 *
 * @throws IOException on error/*  w  w w  .ja  v  a  2  s  . c o m*/
 */
private void addEntryToTarArchive(TarArchiveOutputStream tarArchiveOutputStream, Path path, Path base)
        throws IOException {
    File file = path.toFile();
    Path entry = Paths.get(base.toString(), file.getName());
    TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file, entry.toString());
    tarArchiveOutputStream.putArchiveEntry(tarArchiveEntry);

    if (file.isFile()) {
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            // TODO: This method uses a default buffer size of 8K.
            // TODO: Taking a file size in consideration, a bigger buffer size we might increase the performance of the tar.
            IOUtils.copy(fileInputStream, tarArchiveOutputStream);
        } finally {
            tarArchiveOutputStream.closeArchiveEntry();
        }
    } else {
        tarArchiveOutputStream.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addEntryToTarArchive(tarArchiveOutputStream, Paths.get(child.getAbsolutePath()), entry);
            }
        }
    }
}

From source file:org.gradle.caching.internal.packaging.impl.TarBuildCacheEntryPacker.java

private static void createTarEntry(String path, long size, int mode, TarArchiveOutputStream tarOutput)
        throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(path, true);
    entry.setSize(size);//from w  w w  . j ava2s .  co m
    entry.setMode(mode);
    tarOutput.putArchiveEntry(entry);
}

From source file:org.gradle.caching.internal.tasks.CommonsTarPacker.java

@Override
public void pack(List<DataSource> inputs, DataTarget output) throws IOException {
    TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(output.openOutput());
    for (DataSource input : inputs) {
        TarArchiveEntry entry = new TarArchiveEntry(input.getName());
        entry.setSize(input.getLength());
        tarOutput.putArchiveEntry(entry);
        PackerUtils.packEntry(input, tarOutput, buffer);
        tarOutput.closeArchiveEntry();/*from w  ww .ja  v a 2 s .  c  o  m*/
    }
    tarOutput.close();
}