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

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

Introduction

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

Prototype

public void closeArchiveEntry() throws IOException 

Source Link

Document

Close an entry.

Usage

From source file:org.apache.tika.server.TarWriter.java

private static void tarStoreBuffer(TarArchiveOutputStream zip, String name, byte[] dataBuffer)
        throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(name);

    entry.setSize(dataBuffer.length);/*  ww  w  . j ava2  s  .co  m*/

    zip.putArchiveEntry(entry);

    zip.write(dataBuffer);

    zip.closeArchiveEntry();
}

From source file:org.apache.whirr.util.Tarball.java

private static void addFile(TarArchiveOutputStream tarOutputStream, String path, String base)
        throws IOException {
    File file = new File(path);
    String entryName = base + file.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);

    tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tarOutputStream.putArchiveEntry(tarEntry);

    if (file.isFile()) {
        IOUtils.copy(new FileInputStream(file), tarOutputStream);
        tarOutputStream.closeArchiveEntry();
    } else {//from  w ww . j av  a  2  s.c o m
        tarOutputStream.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addFile(tarOutputStream, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}

From source file:org.cloudbyexample.dc.agent.util.ArchiveUtil.java

/**
 * Add a file to the archive or recurse if a directory is specified.
 *///from   ww  w  .j a  va  2  s.c  o  m
private static void addFiles(TarArchiveOutputStream taos, File file, String dir) throws IOException {
    taos.putArchiveEntry(new TarArchiveEntry(file, dir));

    if (file.isFile()) {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, taos);
        taos.closeArchiveEntry();
        bis.close();
    } else if (file.isDirectory()) {
        taos.closeArchiveEntry();
        for (File childFile : file.listFiles()) {
            addFiles(taos, childFile, childFile.getName());
        }
    }
}

From source file:org.cloudifysource.esc.util.TarGzUtils.java

private static void addFileToTarGz(final TarArchiveOutputStream tOut, final String path, final String base,
        final boolean addRoot) throws IOException {
    File f = new File(path);
    String entryName = base + f.getName();

    if (f.isFile()) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
        tOut.putArchiveEntry(tarEntry);/*ww w  .ja va2  s  .com*/
        IOUtils.copy(new FileInputStream(f), tOut);
        tOut.closeArchiveEntry();
    } else {
        if (addRoot) {
            TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
            tOut.putArchiveEntry(tarEntry);
            tOut.closeArchiveEntry();
        }
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                if (addRoot) {
                    addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/", true);
                } else {
                    addFileToTarGz(tOut, child.getAbsolutePath(), "", true);
                }
            }
        }
    }
}

From source file:org.codehaus.plexus.archiver.tar.TarArchiver.java

/**
 * tar a file//from   w ww. j a v a 2  s. co  m
 *
 * @param entry the file to tar
 * @param tOut  the output stream
 * @param vPath the path name of the file to tar
 * @throws IOException on error
 */
protected void tarFile(ArchiveEntry entry, TarArchiveOutputStream tOut, String vPath)
        throws ArchiverException, IOException {

    // don't add "" to the archive
    if (vPath.length() <= 0) {
        return;
    }

    if (entry.getResource().isDirectory() && !vPath.endsWith("/")) {
        vPath += "/";
    }

    if (vPath.startsWith("/") && !options.getPreserveLeadingSlashes()) {
        int l = vPath.length();
        if (l <= 1) {
            // we would end up adding "" to the archive
            return;
        }
        vPath = vPath.substring(1, l);
    }

    int pathLength = vPath.length();
    InputStream fIn = null;

    try {
        TarArchiveEntry te;
        if (!longFileMode.isGnuMode()
                && pathLength >= org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN) {
            int maxPosixPathLen = org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN
                    + org.apache.commons.compress.archivers.tar.TarConstants.PREFIXLEN;
            if (longFileMode.isPosixMode()) {
            } else if (longFileMode.isPosixWarnMode()) {
                if (pathLength > maxPosixPathLen) {
                    getLogger().warn("Entry: " + vPath + " longer than " + maxPosixPathLen + " characters.");
                    if (!longWarningGiven) {
                        getLogger().warn("Resulting tar file can only be processed "
                                + "successfully by GNU compatible tar commands");
                        longWarningGiven = true;
                    }
                }
            } else if (longFileMode.isOmitMode()) {
                getLogger().info("Omitting: " + vPath);
                return;
            } else if (longFileMode.isWarnMode()) {
                getLogger().warn("Entry: " + vPath + " longer than "
                        + org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN + " characters.");
                if (!longWarningGiven) {
                    getLogger().warn("Resulting tar file can only be processed "
                            + "successfully by GNU compatible tar commands");
                    longWarningGiven = true;
                }
            } else if (longFileMode.isFailMode()) {
                throw new ArchiverException("Entry: " + vPath + " longer than "
                        + org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN + " characters.");
            } else {
                throw new IllegalStateException("Non gnu mode should never get here?");
            }
        }

        if (entry.getType() == ArchiveEntry.SYMLINK) {
            final SymlinkDestinationSupplier plexusIoSymlinkResource = (SymlinkDestinationSupplier) entry
                    .getResource();
            te = new TarArchiveEntry(vPath, TarArchiveEntry.LF_SYMLINK);
            te.setLinkName(plexusIoSymlinkResource.getSymlinkDestination());
        } else {
            te = new TarArchiveEntry(vPath);
        }

        long teLastModified = entry.getResource().getLastModified();
        te.setModTime(teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE ? System.currentTimeMillis()
                : teLastModified);

        if (entry.getType() == ArchiveEntry.SYMLINK) {
            te.setSize(0);

        } else if (!entry.getResource().isDirectory()) {
            final long size = entry.getResource().getSize();
            te.setSize(size == PlexusIoResource.UNKNOWN_RESOURCE_SIZE ? 0 : size);
        }
        te.setMode(entry.getMode());

        PlexusIoResourceAttributes attributes = entry.getResourceAttributes();

        te.setUserName((attributes != null && attributes.getUserName() != null) ? attributes.getUserName()
                : options.getUserName());
        te.setGroupName((attributes != null && attributes.getGroupName() != null) ? attributes.getGroupName()
                : options.getGroup());

        final int userId = (attributes != null && attributes.getUserId() != null) ? attributes.getUserId()
                : options.getUid();
        if (userId >= 0) {
            te.setUserId(userId);
        }

        final int groupId = (attributes != null && attributes.getGroupId() != null) ? attributes.getGroupId()
                : options.getGid();
        if (groupId >= 0) {
            te.setGroupId(groupId);
        }

        tOut.putArchiveEntry(te);

        try {
            if (entry.getResource().isFile() && !(entry.getType() == ArchiveEntry.SYMLINK)) {
                fIn = entry.getInputStream();

                Streams.copyFullyDontCloseOutput(fIn, tOut, "xAR");
            }

        } catch (Throwable e) {
            getLogger().warn("When creating tar entry", e);
        } finally {
            tOut.closeArchiveEntry();
        }
    } finally {
        IOUtil.close(fIn);
    }
}

From source file:org.codehaus.plexus.archiver.tar.TarRoundTripTest.java

/**
 * test round-tripping long (GNU) entries
 *//*from   w w w  .j av  a 2 s .c  o m*/
public void testLongRoundTripping() throws IOException {
    TarArchiveEntry original = new TarArchiveEntry(LONG_NAME);
    assertEquals("over 100 chars", true, LONG_NAME.length() > 100);
    assertEquals("original name", LONG_NAME, original.getName());

    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    TarArchiveOutputStream tos = new TarArchiveOutputStream(buff);
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tos.putArchiveEntry(original);
    tos.closeArchiveEntry();
    tos.close();

    TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(buff.toByteArray()));
    TarArchiveEntry tripped = tis.getNextTarEntry();
    assertEquals("round-tripped name", LONG_NAME, tripped.getName());
    assertNull("no more entries", tis.getNextEntry());
    tis.close();
}

From source file:org.dcm4chee.storage.tar.TarContainerProvider.java

@Override
public void writeEntriesTo(StorageContext context, List<ContainerEntry> entries, OutputStream out)
        throws IOException {
    TarArchiveOutputStream tar = new TarArchiveOutputStream(out);
    String checksumEntry = container.getChecksumEntry();
    if (checksumEntry != null) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ContainerEntry.writeChecksumsTo(entries, bout);
        TarArchiveEntry tarEntry = new TarArchiveEntry(checksumEntry);
        tarEntry.setSize(bout.size());//from   w  w w.  j a va2  s .  co  m
        tar.putArchiveEntry(tarEntry);
        tar.write(bout.toByteArray());
        tar.closeArchiveEntry();
    }
    for (ContainerEntry entry : entries) {
        Path path = entry.getSourcePath();
        TarArchiveEntry tarEntry = new TarArchiveEntry(entry.getName());
        tarEntry.setModTime(Files.getLastModifiedTime(path).toMillis());
        tarEntry.setSize(Files.size(path));
        tar.putArchiveEntry(tarEntry);
        Files.copy(path, tar);
        tar.closeArchiveEntry();
    }
    tar.finish();
}

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

private void addTarEntry(VirtualFile virtualFile, TarArchiveOutputStream tarOutputStream)
        throws ServerException {
    try {// www.j a va2s  .  c o m
        TarArchiveEntry tarEntry = new TarArchiveEntry(getTarEntryName(virtualFile));
        if (virtualFile.isFolder()) {
            tarEntry.setModTime(0);
            tarOutputStream.putArchiveEntry(tarEntry);
        } else {
            tarEntry.setSize(virtualFile.getLength());
            tarEntry.setModTime(virtualFile.getLastModificationDate());
            tarOutputStream.putArchiveEntry(tarEntry);
            try (InputStream content = virtualFile.getContent()) {
                ByteStreams.copy(content, tarOutputStream);
            }
        }
        tarOutputStream.closeArchiveEntry();
    } catch (ForbiddenException e) {
        throw new ServerException(e.getServiceError());
    } catch (IOException e) {
        throw new ServerException(e.getMessage(), e);
    }
}

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 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);/*from w  w  w .  ja  v  a2  s  . c o  m*/
    tarOut.write(TEST_CONTENT_BYTES);
    tarOut.closeArchiveEntry();
}