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

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Get this entry's name.

Usage

From source file:org.renjin.cran.ProjectBuilder.java

private void addDataset(TarArchiveEntry entry) {
    String path = entry.getName();
    int lastSlash = path.lastIndexOf('/');
    String name = path.substring(lastSlash + 1);
    datasets.setProperty(stripExt(name), name);
}

From source file:org.renjin.cran.ProjectBuilder.java

private void extractTo(TarArchiveEntry entry, InputStream in, File targetDir) throws IOException {
    targetDir.mkdirs();//from ww w. j  a va2  s.c  o m
    int slash = entry.getName().lastIndexOf('/');
    String name = entry.getName().substring(slash + 1);
    File targetFile = new File(targetDir, name);
    FileOutputStream fos = new FileOutputStream(targetFile);
    ByteStreams.copy(in, fos);
    fos.close();
}

From source file:org.rioproject.impl.util.DownloadManager.java

private static void unTar(File tarFile, File target) throws IOException {
    InputStream is = new FileInputStream(tarFile);
    ArchiveInputStream in;// w ww .  j a  va  2  s.  c  o  m
    try {
        in = new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    } catch (ArchiveException e) {
        IOException ioe = new IOException("Unarchving " + tarFile.getName());
        ioe.initCause(e);
        throw ioe;
    }
    try {
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) {
            File f = new File(target, entry.getName());
            if (entry.isDirectory()) {
                if (f.mkdirs()) {
                    logger.trace("Created directory {}", f.getPath());
                }
            } else {
                if (!f.getParentFile().exists()) {
                    if (f.getParentFile().mkdirs()) {
                        logger.trace("Created {}", f.getParentFile().getPath());
                    }
                }
                if (f.createNewFile()) {
                    logger.trace("Created {}", f.getName());
                }
                OutputStream out = new FileOutputStream(f);
                IOUtils.copy(in, out);
                out.close();
            }
            setPerms(f, entry.getMode());
        }
    } finally {
        in.close();
    }
}

From source file:org.rioproject.impl.util.DownloadManager.java

private static void removeExtractedTarFiles(File root, File tarFile) throws IOException {
    InputStream is = new FileInputStream(tarFile);
    ArchiveInputStream in;//w  ww .j a  v a  2s.  c o m
    try {
        in = new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    } catch (ArchiveException e) {
        IOException ioe = new IOException("Removing " + tarFile.getName());
        ioe.initCause(e);
        throw ioe;
    }
    File parent = null;
    try {
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) {
            File f = new File(root, entry.getName());
            if (parent == null) {
                parent = new File(getExtractedToPath(f, root));
            }
            FileUtils.remove(f);

        }
    } finally {
        in.close();
    }
    FileUtils.remove(parent);
}

From source file:org.robovm.compilerhelper.Archiver.java

public static void unarchive(File archiveFile, File destinationDirectory) throws IOException {

    TarArchiveInputStream tar = new TarArchiveInputStream(
            new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archiveFile))));

    destinationDirectory.mkdirs();/*from   w ww . j  a v a2  s . com*/
    TarArchiveEntry entry = tar.getNextTarEntry();
    while (entry != null) {
        File f = new File(destinationDirectory, entry.getName());
        if (entry.isDirectory()) {
            f.mkdirs();
            entry = tar.getNextTarEntry();
            continue;
        }

        // TODO make this a bit cleaner
        String parentDir = f.getPath();
        if (parentDir != null) {
            new File(parentDir.substring(0, parentDir.lastIndexOf(File.separator))).mkdirs();
        }

        f.createNewFile();
        byte[] bytes = new byte[1024];
        int count;
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
        while ((count = tar.read(bytes)) > 0) {
            out.write(bytes, 0, count);
        }
        out.flush();
        out.close();

        entry = tar.getNextTarEntry();
    }
}

From source file:org.rsna.ctp.stdstages.ArchiveImportService.java

private void expandTAR(File tar, File dir) {
    try {/*from   w w  w  .  j  a  v  a  2  s  .  co m*/
        TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(tar));
        TarArchiveEntry tae;
        while ((tae = tais.getNextTarEntry()) != null) {
            if (!tae.isDirectory()) {
                FileOutputStream fos = new FileOutputStream(new File(dir, tae.getName()));
                byte[] buf = new byte[4096];
                long count = tae.getSize();
                while (count > 0) {
                    int n = tais.read(buf, 0, buf.length);
                    fos.write(buf, 0, n);
                    count -= n;
                }
                fos.flush();
                fos.close();
            }
        }
        tais.close();
    } catch (Exception ex) {
        logger.warn("Unable to expand: \"" + tar + "\"", ex);
    }
}

From source file:org.sakaiproject.vtlgen.api.PackageUtil.java

public static void untar(String fileName, String targetPath) throws IOException {
    File tarArchiveFile = new File(fileName);
    BufferedOutputStream dest = null;
    FileInputStream tarArchiveStream = new FileInputStream(tarArchiveFile);
    TarArchiveInputStream tis = new TarArchiveInputStream(new BufferedInputStream(tarArchiveStream));
    TarArchiveEntry entry = null;
    try {/*from   ww  w . j  ava 2 s.c o  m*/
        while ((entry = tis.getNextTarEntry()) != null) {
            int count;
            File outputFile = new File(targetPath, entry.getName());

            if (entry.isDirectory()) { // entry is a directory
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }
            } else { // entry is a file
                byte[] data = new byte[BUFFER_MAX];
                FileOutputStream fos = new FileOutputStream(outputFile);
                dest = new BufferedOutputStream(fos, BUFFER_MAX);
                while ((count = tis.read(data, 0, BUFFER_MAX)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dest != null) {
            dest.flush();
            dest.close();
        }
        tis.close();
    }
}

From source file:org.savantbuild.io.tar.TarBuilderTest.java

private static void assertTarFileEquals(Path tarFile, String entry, Path original) throws IOException {
    InputStream is = Files.newInputStream(tarFile);
    if (tarFile.toString().endsWith(".gz")) {
        is = new GZIPInputStream(is);
    }//  w w w  . ja  va2 s .  co m

    try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
        TarArchiveEntry tarArchiveEntry = tis.getNextTarEntry();
        while (tarArchiveEntry != null && !tarArchiveEntry.getName().equals(entry)) {
            tarArchiveEntry = tis.getNextTarEntry();
        }

        if (tarArchiveEntry == null) {
            fail("Tar [" + tarFile + "] is missing entry [" + entry + "]");
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int length;
        while ((length = tis.read(buf)) != -1) {
            baos.write(buf, 0, length);
        }

        assertEquals(Files.readAllBytes(original), baos.toByteArray());
        assertEquals(tarArchiveEntry.getSize(), Files.size(original));
        assertEquals(tarArchiveEntry.getUserName(), Files.getOwner(original).getName());
        assertEquals(tarArchiveEntry.getGroupName(),
                Files.readAttributes(original, PosixFileAttributes.class).group().getName());
    }
}

From source file:org.savantbuild.io.tar.TarBuilderTest.java

private static void assertTarContainsDirectory(Path tarFile, String entry, Integer mode, String userName,
        String groupName) throws IOException {
    InputStream is = Files.newInputStream(tarFile);
    if (tarFile.toString().endsWith(".gz")) {
        is = new GZIPInputStream(is);
    }/* ww  w  . j  a  v  a2s  . c  om*/

    try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
        TarArchiveEntry tarArchiveEntry = tis.getNextTarEntry();
        while (tarArchiveEntry != null && !tarArchiveEntry.getName().equals(entry)) {
            tarArchiveEntry = tis.getNextTarEntry();
        }

        if (tarArchiveEntry == null) {
            fail("Tar [" + tarFile + "] is missing entry [" + entry + "]");
        }

        assertTrue(tarArchiveEntry.isDirectory());
        if (mode != null) {
            assertEquals(tarArchiveEntry.getMode(), FileTools.toMode(mode));
        }
        if (userName != null) {
            assertEquals(tarArchiveEntry.getUserName(), userName);
        }
        if (groupName != null) {
            assertEquals(tarArchiveEntry.getGroupName(), groupName);
        }
    }
}

From source file:org.savantbuild.io.tar.TarTools.java

/**
 * Untars a TAR file. This also handles tar.gz files by checking the file extension. If the file extension ends in .gz
 * it will read the tarball through a GZIPInputStream.
 *
 * @param file     The TAR file./*from   w  w w. ja v  a 2s  . co m*/
 * @param to       The directory to untar to.
 * @param useGroup Determines if the group name in the archive is used.
 * @param useOwner Determines if the owner name in the archive is used.
 * @throws IOException If the untar fails.
 */
public static void untar(Path file, Path to, boolean useGroup, boolean useOwner) throws IOException {
    if (Files.notExists(to)) {
        Files.createDirectories(to);
    }

    InputStream is = Files.newInputStream(file);
    if (file.toString().endsWith(".gz")) {
        is = new GZIPInputStream(is);
    }

    try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
        TarArchiveEntry entry;
        while ((entry = tis.getNextTarEntry()) != null) {
            Path entryPath = to.resolve(entry.getName());
            if (entry.isDirectory()) {
                // Skip directory entries that don't add any value
                if (entry.getMode() == 0 && entry.getGroupName() == null && entry.getUserName() == null) {
                    continue;
                }

                if (Files.notExists(entryPath)) {
                    Files.createDirectories(entryPath);
                }

                if (entry.getMode() != 0) {
                    Set<PosixFilePermission> permissions = FileTools.toPosixPermissions(entry.getMode());
                    Files.setPosixFilePermissions(entryPath, permissions);
                }

                if (useGroup && entry.getGroupName() != null && !entry.getGroupName().trim().isEmpty()) {
                    GroupPrincipal group = FileSystems.getDefault().getUserPrincipalLookupService()
                            .lookupPrincipalByGroupName(entry.getGroupName());
                    Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setGroup(group);
                }

                if (useOwner && entry.getUserName() != null && !entry.getUserName().trim().isEmpty()) {
                    UserPrincipal user = FileSystems.getDefault().getUserPrincipalLookupService()
                            .lookupPrincipalByName(entry.getUserName());
                    Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setOwner(user);
                }
            } else {
                if (Files.notExists(entryPath.getParent())) {
                    Files.createDirectories(entryPath.getParent());
                }

                if (Files.isRegularFile(entryPath)) {
                    if (Files.size(entryPath) == entry.getSize()) {
                        continue;
                    } else {
                        Files.delete(entryPath);
                    }
                }

                Files.createFile(entryPath);

                try (OutputStream os = Files.newOutputStream(entryPath)) {
                    byte[] ba = new byte[1024];
                    int read;
                    while ((read = tis.read(ba)) != -1) {
                        if (read > 0) {
                            os.write(ba, 0, read);
                        }
                    }
                }

                if (entry.getMode() != 0) {
                    Set<PosixFilePermission> permissions = FileTools.toPosixPermissions(entry.getMode());
                    Files.setPosixFilePermissions(entryPath, permissions);
                }

                if (useGroup && entry.getGroupName() != null && !entry.getGroupName().trim().isEmpty()) {
                    GroupPrincipal group = FileSystems.getDefault().getUserPrincipalLookupService()
                            .lookupPrincipalByGroupName(entry.getGroupName());
                    Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setGroup(group);
                }

                if (useOwner && entry.getUserName() != null && !entry.getUserName().trim().isEmpty()) {
                    UserPrincipal user = FileSystems.getDefault().getUserPrincipalLookupService()
                            .lookupPrincipalByName(entry.getUserName());
                    Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setOwner(user);
                }
            }
        }
    }
}