Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry TarArchiveEntry

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

Introduction

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

Prototype

public TarArchiveEntry(byte[] headerBuf) 

Source Link

Document

Construct an entry from an archive's header bytes.

Usage

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  a2s .  co  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

@Override
public void addFile(final ContentProvider contentProvider, String fileName, EntryInformation entryInformation)
        throws IOException {
    if (entryInformation == null) {
        entryInformation = EntryInformation.DEFAULT_FILE;
    }/*from   w w  w.  ja  v  a  2 s  .  co  m*/

    try {
        fileName = cleanupPath(fileName);

        if (entryInformation.isConfigurationFile()) {
            this.confFiles.add(fileName.substring(1)); // without the leading dot
        }

        final TarArchiveEntry entry = new TarArchiveEntry(fileName);
        entry.setSize(contentProvider.getSize());
        applyInfo(entry, entryInformation);

        checkCreateParents(fileName);

        this.dataStream.putArchiveEntry(entry);

        final Map<String, byte[]> results = new HashMap<>();
        try (final ChecksumInputStream in = new ChecksumInputStream(contentProvider.createInputStream(),
                results, MessageDigest.getInstance("MD5"))) {
            this.installedSize += IOUtils.copyLarge(in, this.dataStream);
        }

        this.dataStream.closeArchiveEntry();

        // record the checksum
        recordChecksum(fileName, results.get("MD5"));
    } catch (final Exception e) {
        throw new IOException(e);
    }
}

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

protected void internalAddDirectory(final String path, final EntryInformation entryInformation)
        throws IOException {
    final TarArchiveEntry entry = new TarArchiveEntry(path);
    applyInfo(entry, entryInformation);/*from  w  w w.  ja  v  a2  s.co  m*/

    this.dataStream.putArchiveEntry(entry);
    this.dataStream.closeArchiveEntry();

    this.paths.add(path);
}

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;/*  ww 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

@Override
public void addFile(final ContentProvider contentProvider, String fileName, EntryInformation entryInformation)
        throws IOException {
    if (entryInformation == null) {
        entryInformation = EntryInformation.DEFAULT_FILE;
    }/*from  w w  w.j a  v  a  2  s.c  om*/

    try {
        fileName = cleanupPath(fileName);

        if (entryInformation.isConfigurationFile()) {
            this.confFiles.add(fileName.substring(1)); // without the leading dot
        }

        final TarArchiveEntry entry = new TarArchiveEntry(fileName);
        entry.setSize(contentProvider.getSize());
        applyInfo(entry, entryInformation, this.getTimestampProvider());

        checkCreateParents(fileName);

        this.dataStream.putArchiveEntry(entry);

        final Map<String, byte[]> results = new HashMap<>();
        try (final ChecksumInputStream in = new ChecksumInputStream(contentProvider.createInputStream(),
                results, MessageDigest.getInstance("MD5"))) {
            this.installedSize += ByteStreams.copy(in, this.dataStream);
        }

        this.dataStream.closeArchiveEntry();

        // record the checksum
        recordChecksum(fileName, results.get("MD5"));
    } catch (final Exception e) {
        throw new IOException(e);
    }
}

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

protected void internalAddDirectory(final String path, final EntryInformation entryInformation)
        throws IOException {
    final TarArchiveEntry entry = new TarArchiveEntry(path);
    applyInfo(entry, entryInformation, this.getTimestampProvider());

    this.dataStream.putArchiveEntry(entry);
    this.dataStream.closeArchiveEntry();

    this.paths.add(path);
}

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;//  w  w w .j av  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());
    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;//  w ww  . j av  a2  s .  c o  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.tracecompass.internal.tmf.ui.project.wizards.importtrace.TarLeveledStructureProvider.java

/**
 * Creates a new container tar entry with the specified name, iff it has
 * not already been created. If the parent of the given element does not
 * already exist it will be recursively created as well.
 * @param pathName The path representing the container
 * @return The element represented by this pathname (it may have already existed)
 *//*ww w .  j  ava  2 s.com*/
protected TarArchiveEntry createContainer(IPath pathName) {
    IPath newPathName = pathName;
    TarArchiveEntry existingEntry = directoryEntryCache.get(newPathName);
    if (existingEntry != null) {
        return existingEntry;
    }

    TarArchiveEntry parent;
    if (newPathName.segmentCount() == 1) {
        parent = root;
    } else {
        parent = createContainer(newPathName.removeLastSegments(1));
    }
    // Add trailing / so that the entry knows it's a folder
    newPathName = newPathName.addTrailingSeparator();
    TarArchiveEntry newEntry = new TarArchiveEntry(newPathName.toString());
    directoryEntryCache.put(newPathName, newEntry);
    List<TarArchiveEntry> childList = new ArrayList<>();
    children.put(newEntry, childList);

    List<TarArchiveEntry> parentChildList = children.get(parent);
    NonNullUtils.checkNotNull(parentChildList).add(newEntry);
    return newEntry;
}

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

@Override
protected Object newEntry(String name) {
    return new TarArchiveEntry(name);
}