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

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

Introduction

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

Prototype

public TarArchiveInputStream(InputStream is) 

Source Link

Document

Constructor for TarInputStream.

Usage

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

private void unpackSources(File sourceArchive) throws IOException {
    FileInputStream in = new FileInputStream(sourceArchive);
    GZIPInputStream gzipIn = new GZIPInputStream(in);
    TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn);

    TarArchiveEntry entry;//from   w w  w.  j  av a2  s .c o  m
    while ((entry = tarIn.getNextTarEntry()) != null) {
        if (entry.getName().endsWith(".Rd")) {

        } else if (entry.getName().startsWith(pkg + "/src/") && entry.getSize() != 0) {

        } else if (entry.getName().startsWith(pkg + "/R/") && entry.getSize() != 0) {
            extractTo(entry, tarIn, rSourcesDir);

        } else if (entry.getName().equals(pkg + "/DESCRIPTION")) {
            extractTo(entry, tarIn, baseDir);

        } else if (entry.getName().equals(pkg + "/NAMESPACE")) {
            extractTo(entry, tarIn, baseDir);

        } else if (entry.getName().startsWith(pkg + "/tests/") && entry.getSize() != 0) {
            extractTo(entry, tarIn, rTestsDir);

        } else if (entry.getName().startsWith(pkg + "/data/") && entry.getSize() != 0) {
            extractTo(entry, tarIn, resourcesDir);
            addDataset(entry);
        }
    }
}

From source file:org.richfaces.tests.qa.plugin.utils.files.SimpleExtractor.java

@Override
public void extract(File baseDir, File archive) throws IOException {
    if (archive.getAbsolutePath().endsWith("zip")) {
        getLog().info(MessageFormat.format("Extracting zip file <{0}> to directory <{1}>.",
                archive.getAbsolutePath(), baseDir));
        final ZipInputStream is = new ZipInputStream(new BufferedInputStream(new FileInputStream(archive)));
        ZipEntry entry;//from   w  w w . j av a  2  s.com
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final File file = new File(baseDir, entry.getName());
                if (file.exists()) {
                    file.delete();
                }
                Files.createParentDirs(file);
                Files.write(ByteStreams.toByteArray(is), file);
            }
        }
        is.close();
    } else if (archive.getAbsolutePath().endsWith("tar.bz2") || archive.getAbsolutePath().endsWith("tar.gz")) {
        getLog().info(MessageFormat.format("Extracting tar.bz2/tar.gz file <{0}> to directory <{1}>.",
                archive.getAbsolutePath(), baseDir));

        // unzip to tar
        File tarfile = new File(baseDir, "archive.tar");
        tarfile.delete();
        BZip2CompressorInputStream from = new BZip2CompressorInputStream(
                new BufferedInputStream(new FileInputStream(archive)));
        FileOutputStream to = new FileOutputStream(tarfile);
        to.getChannel().transferFrom(Channels.newChannel(from), 0, Long.MAX_VALUE);
        // untar
        final TarArchiveInputStream is = new TarArchiveInputStream(
                new BufferedInputStream(new FileInputStream(tarfile)));
        TarArchiveEntry entry;
        File file;
        while ((entry = is.getNextTarEntry()) != null) {
            if (!entry.isDirectory()) {
                file = new File(baseDir, entry.getName());
                Files.createParentDirs(file);
                Files.write(ByteStreams.toByteArray(is), file);
            }
        }
        is.close();
        tarfile.delete();
    } else {
        throw new UnsupportedOperationException("Not supported file format " + archive.getName());
    }
    getLog().info(MessageFormat.format("Extracting of <{0}> completed.", archive.getAbsolutePath()));
}

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  w  w  . j  a v a2  s  .  c  o  m
    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.robovm.eclipse.RoboVMPlugin.java

private static void extractTarGz(File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;/*  ww w. j a v  a  2s .co  m*/
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            if (entry instanceof TarArchiveEntry) {
                int mode = ((TarArchiveEntry) entry).getMode();
                if ((mode & 00100) > 0) {
                    // Preserve execute permissions
                    f.setExecutable(true, (mode & 00001) == 0);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.robovm.gradle.tasks.AbstractRoboVMTask.java

private static void extractTarGz(File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;//  w  w  w  .j av  a  2  s . com
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            f.setLastModified(entry.getLastModifiedDate().getTime());
            if (entry instanceof TarArchiveEntry) {
                int mode = ((TarArchiveEntry) entry).getMode();
                if ((mode & 00100) > 0) {
                    // Preserve execute permissions
                    f.setExecutable(true, (mode & 00001) == 0);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.robovm.idea.RoboVmPlugin.java

private static void extractArchive(String archive, File dest) {
    archive = "/" + archive;
    TarArchiveInputStream in = null;//from w w w.  j  ava2s.  com
    boolean isSnapshot = Version.getVersion().toLowerCase().contains("snapshot");
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(RoboVmPlugin.class.getResourceAsStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(dest, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                if (!isSnapshot && f.exists()) {
                    continue;
                }
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
        }
        logInfo(null, "Installed RoboVM SDK %s to %s", Version.getVersion(), dest.getAbsolutePath());

        // make all files in bin executable
        for (File file : new File(getSdkHome(), "bin").listFiles()) {
            file.setExecutable(true);
        }
    } catch (Throwable t) {
        logError(null, "Couldn't extract SDK to %s", dest.getAbsolutePath());
        throw new RuntimeException("Couldn't extract SDK to " + dest.getAbsolutePath(), t);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.robovm.maven.resolver.Archiver.java

public static void unarchive(Logger logger, File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;/* w  ww  . ja  v a  2  s .  c  o  m*/
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                logger.debug(f.getAbsolutePath());
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            f.setLastModified(entry.getLastModifiedDate().getTime());
            if (entry instanceof TarArchiveEntry) {
                int mode = ((TarArchiveEntry) entry).getMode();
                if ((mode & 00100) > 0) {
                    // Preserve execute permissions
                    f.setExecutable(true, (mode & 00001) == 0);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.robovm.templater.Templater.java

private void extractArchive(File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;//  w  w w  .  j  av a  2  s.  c o m
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, substitutePlaceholdersInFileName(entry.getName()));
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
                substitutePlaceholdersInFile(f);
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

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

private void expandTAR(File tar, File dir) {
    try {/*from w ww .  j ava2 s .c  o  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;//from w  w  w.  j  av a2 s  . c om
    try {
        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();
    }
}