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

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

Introduction

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

Prototype

public TarArchiveEntry getNextTarEntry() throws IOException 

Source Link

Document

Get the next entry in this tar archive.

Usage

From source file:com.netflix.spinnaker.clouddriver.appengine.artifacts.StorageUtils.java

public static void untarStreamToPath(InputStream inputStream, String basePath) throws IOException {
    class DirectoryTimestamp {
        public DirectoryTimestamp(File d, long m) {
            directory = d;/*from  w ww . j  a  v a 2s  . c om*/
            millis = m;
        }

        public File directory;
        public long millis;
    }
    ;
    // Directories come in hierarchical order within the stream, but
    // we need to set their timestamps after their children have been written.
    Stack<DirectoryTimestamp> directoryStack = new Stack<DirectoryTimestamp>();

    File baseDirectory = new File(basePath);
    baseDirectory.mkdir();

    TarArchiveInputStream tarStream = new TarArchiveInputStream(inputStream);
    for (TarArchiveEntry entry = tarStream.getNextTarEntry(); entry != null; entry = tarStream
            .getNextTarEntry()) {
        File target = new File(baseDirectory, entry.getName());
        if (entry.isDirectory()) {
            directoryStack.push(new DirectoryTimestamp(target, entry.getModTime().getTime()));
            continue;
        }
        writeStreamToFile(tarStream, target);
        target.setLastModified(entry.getModTime().getTime());
    }

    while (!directoryStack.empty()) {
        DirectoryTimestamp info = directoryStack.pop();
        info.directory.setLastModified(info.millis);
    }
    tarStream.close();
}

From source file:net.rwx.maven.asciidoc.utils.FileUtils.java

public static String uncompress(InputStream is, String destination) throws IOException {

    BufferedInputStream in = new BufferedInputStream(is);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
    TarArchiveInputStream tarInput = new TarArchiveInputStream(gzIn);

    TarArchiveEntry entry = tarInput.getNextTarEntry();
    do {// w  ww .j av  a2  s.c  o m
        File f = new File(destination + "/" + entry.getName());
        FileUtils.forceMkdir(f.getParentFile());

        if (!f.isDirectory()) {
            OutputStream os = new FileOutputStream(f);
            byte[] content = new byte[(int) entry.getSize()];
            int byteRead = 0;
            while (byteRead < entry.getSize()) {
                byteRead += tarInput.read(content, byteRead, content.length - byteRead);
                os.write(content, 0, byteRead);
            }

            os.close();
            forceDeleteOnExit(f);
        }
        entry = tarInput.getNextTarEntry();
    } while (entry != null);

    gzIn.close();

    return destination;
}

From source file:co.cask.cdap.internal.app.runtime.LocalizationUtils.java

private static void extractTar(final TarArchiveInputStream tis, File targetDir) throws IOException {
    TarArchiveEntry entry = tis.getNextTarEntry();
    while (entry != null) {
        File output = new File(targetDir, new File(entry.getName()).getName());
        if (entry.isDirectory()) {
            //noinspection ResultOfMethodCallIgnored
            output.mkdirs();//from   www . ja v  a 2s. co  m
        } else {
            //noinspection ResultOfMethodCallIgnored
            output.getParentFile().mkdirs();
            ByteStreams.copy(tis, Files.newOutputStreamSupplier(output));
        }
        entry = tis.getNextTarEntry();
    }
}

From source file:gobblin.data.management.copy.converter.UnGzipConverterTest.java

private static String readGzipStreamAsString(InputStream is) throws Exception {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(is);
    try {//  w w w.j  av a 2 s.c  om
        TarArchiveEntry tarEntry;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (tarEntry.isFile() && tarEntry.getName().endsWith(".txt")) {
                return IOUtils.toString(tarIn, "UTF-8");
            }
        }
    } finally {
        tarIn.close();
    }

    return Strings.EMPTY;
}

From source file:com.pinterest.deployservice.common.TarUtils.java

/**
 * Unbundle the given tar bar as a map, with key as file name and value as content.
 *//* www .  j av a2 s.c o  m*/
public static Map<String, String> untar(InputStream is) throws Exception {
    TarArchiveInputStream tais = new TarArchiveInputStream(new GZIPInputStream(is));
    Map<String, String> data = new HashMap<String, String>();
    TarArchiveEntry entry;
    while ((entry = tais.getNextTarEntry()) != null) {
        String name = entry.getName();
        byte[] content = new byte[(int) entry.getSize()];
        tais.read(content, 0, content.length);
        data.put(name, new String(content, "UTF8"));
    }
    tais.close();
    return data;
}

From source file:msec.org.TarUtil.java

private static void dearchive(File destFile, TarArchiveInputStream tais) throws Exception {

    TarArchiveEntry entry = null;/*ww  w. ja  v  a  2s.  c o  m*/
    while ((entry = tais.getNextTarEntry()) != null) {

        // 
        String dir = destFile.getPath() + File.separator + entry.getName();

        File dirFile = new File(dir);

        // 
        fileProber(dirFile);

        if (entry.isDirectory()) {
            dirFile.mkdirs();
        } else {
            dearchiveFile(dirFile, tais);
        }

    }
}

From source file:com.github.pemapmodder.pocketminegui.utils.Utils.java

private static File installWindowsPHP(File home, InstallProgressReporter progress) {
    progress.report(0.0);// w w w . j a v  a  2s  . com
    try {
        String link = System.getProperty("os.arch").contains("64")
                ? "https://bintray.com/artifact/download/pocketmine/PocketMine/PHP_7.0.3_x64_Windows.tar.gz"
                : "https://bintray.com/artifact/download/pocketmine/PocketMine/PHP_7.0.3_x86_Windows.tar.gz";
        URL url = new URL(link);
        InputStream gz = url.openStream();
        GzipCompressorInputStream tar = new GzipCompressorInputStream(gz);
        TarArchiveInputStream is = new TarArchiveInputStream(tar);
        TarArchiveEntry entry;
        while ((entry = is.getNextTarEntry()) != null) {
            if (!entry.isDirectory()) {
                String name = entry.getName();
                byte[] buffer = new byte[(int) entry.getSize()];
                IOUtils.read(is, buffer);
                File real = new File(home, name);
                real.getParentFile().mkdirs();
                IOUtils.write(buffer, new FileOutputStream(real));
            }
        }
        File output = new File(home, "bin/php/php.exe");
        progress.completed(output);
        return output;
    } catch (IOException e) {
        e.printStackTrace();
        progress.errored();
        return null;
    }
}

From source file:com.openshift.client.utils.TarFileUtils.java

public static boolean hasGitFolder(InputStream inputStream) throws IOException {
    TarArchiveInputStream tarInputStream = null;
    try {//www  .j a v a 2 s .com
        boolean gitFolderPresent = false;
        tarInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(inputStream));
        for (TarArchiveEntry entry = null; (entry = tarInputStream.getNextTarEntry()) != null;) {
            if (GIT_FOLDER_NAME.equals(entry.getName()) && entry.isDirectory()) {
                gitFolderPresent = true;
                break;
            }
        }
        return gitFolderPresent;
    } finally {
        StreamUtils.close(tarInputStream);
    }
}

From source file:ch.devmine.javaparser.Main.java

private static void parseAsTarArchive(Project project, HashMap<String, Package> packs, List<Language> languages,
        Language language, String arg) {

    int slashIndex = arg.lastIndexOf("/");
    String projectName = arg.substring(slashIndex + 1, arg.lastIndexOf("."));
    String projectRoute = arg.substring(0, slashIndex + 1);
    project.setName(projectName);/*w  w w . j a  v a 2  s  .  c om*/

    try {
        TarArchiveInputStream tarInput = new TarArchiveInputStream(new FileInputStream(arg));
        TarArchiveEntry entry;
        while (null != (entry = tarInput.getNextTarEntry())) {
            String entryName = entry.getName();
            String fileName = entryName.substring(entryName.lastIndexOf("/") + 1);
            if (entryName.endsWith(".java") && !(fileName.startsWith("~") || fileName.startsWith("."))) {
                // add package containing source file to hashmap
                Package pack = new Package();
                String packName = FileWalkerUtils.extractFolderName(entry.getName());
                String packPath = extractPackagePath(entry.getName());
                pack.setName(packName);
                pack.setPath(packPath);
                if (packs.get(packName) == null) {
                    packs.put(packName, pack);
                }

                // parse java file

                String entryPath = projectRoute.concat(entryName);
                Parser parser = new Parser(entryPath, tarInput);
                SourceFile sourceFile = new SourceFile();
                parseAndFillSourceFile(parser, sourceFile, entryPath, language, packs, packName);
            }
        }
    } catch (IOException ex) {
        Log.e(TAG, ex.getMessage());
    }

}

From source file:com.codenvy.commons.lang.TarUtils.java

public static void untar(InputStream in, File targetDir) throws IOException {
    final TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
    byte[] b = new byte[BUF_SIZE];
    TarArchiveEntry tarEntry;/*from  www  . jav a 2s  .c o  m*/
    while ((tarEntry = tarIn.getNextTarEntry()) != null) {
        final File file = new File(targetDir, tarEntry.getName());
        if (tarEntry.isDirectory()) {
            file.mkdirs();
        } else {
            final File parent = file.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            try (FileOutputStream fos = new FileOutputStream(file)) {
                int r;
                while ((r = tarIn.read(b)) != -1) {
                    fos.write(b, 0, r);
                }
            }
        }
    }
}