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

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

Introduction

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

Prototype

public TarArchiveEntry[] getDirectoryEntries() 

Source Link

Document

If this entry represents a file, and the file is a directory, return an array of TarEntries for this entry's children.

Usage

From source file:com.wenzani.maven.mongodb.TarUtils.java

private void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir)
        throws IOException {
    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        File subDir = new File(outputDir, entry.getName());

        for (TarArchiveEntry e : entry.getDirectoryEntries()) {
            unpackEntries(tis, e, subDir);
        }/*from  ww  w  . j  a va 2  s .c  o m*/

        return;
    }

    File outputFile = new File(outputDir, entry.getName());

    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    try {
        byte[] content = new byte[(int) entry.getSize()];

        tis.read(content);

        if (content.length > 0) {
            IOUtils.copy(new ByteArrayInputStream(content), outputStream);
        }
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:com.buaa.cfs.utils.FileUtil.java

private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir)
        throws IOException {
    if (entry.isDirectory()) {
        File subDir = new File(outputDir, entry.getName());
        if (!subDir.mkdirs() && !subDir.isDirectory()) {
            throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
        }//w  w  w .  ja  v a 2s.c  o  m

        for (TarArchiveEntry e : entry.getDirectoryEntries()) {
            unpackEntries(tis, e, subDir);
        }

        return;
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        if (!outputFile.getParentFile().mkdirs()) {
            throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
        }
    }

    int count;
    byte data[] = new byte[2048];
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    while ((count = tis.read(data)) != -1) {
        outputStream.write(data, 0, count);
    }

    outputStream.flush();
    outputStream.close();
}

From source file:gdt.data.entity.ArchiveHandler.java

private void getTarEntries(TarArchiveEntry tarEntry, Stack<TarArchiveEntry> s, String root$) {
    if (tarEntry == null)
        return;//from  w  ww . jav  a  2 s.  c o  m
    if (tarEntry.isDirectory()) {
        try {
            TarArchiveEntry[] tea = tarEntry.getDirectoryEntries();
            if (tea != null) {
                for (TarArchiveEntry aTea : tea) {
                    getTarEntries(aTea, s, root$);
                }
            }

        } catch (Exception e) {
            LOGGER.severe(":getTarEntities:" + e.toString());
        }
    } else {
        String entryName$;
        entryName$ = tarEntry.getName().substring(root$.length());
        tarEntry.setName(entryName$);
        s.push(tarEntry);
    }
}

From source file:org.apache.storm.utils.ServerUtils.java

private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir)
        throws IOException {
    if (entry.isDirectory()) {
        File subDir = new File(outputDir, entry.getName());
        if (!subDir.mkdirs() && !subDir.isDirectory()) {
            throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
        }/*from   www. j a  v  a2  s. co m*/
        for (TarArchiveEntry e : entry.getDirectoryEntries()) {
            unpackEntries(tis, e, subDir);
        }
        return;
    }
    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        if (!outputFile.getParentFile().mkdirs()) {
            throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
        }
    }
    int count;
    byte data[] = new byte[2048];
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    while ((count = tis.read(data)) != -1) {
        outputStream.write(data, 0, count);
    }
    outputStream.flush();
    outputStream.close();
}

From source file:rv.comm.rcssserver.TarBz2ZipUtil.java

public static Reader getTarBZ2InputStream(File file) {
    try {/*  w  w  w . ja va 2 s  . c  o  m*/
        // only works for the current layout of tar.bz2 files
        InputStream zStream = new BufferedInputStream(new FileInputStream(file));
        CompressorInputStream bz2InputStream = new CompressorStreamFactory()
                .createCompressorInputStream(CompressorStreamFactory.BZIP2, zStream);
        TarArchiveInputStream tarStream = new TarArchiveInputStream(bz2InputStream);
        TarArchiveEntry entry = tarStream.getNextTarEntry();

        // step into deepest directory
        while (entry != null && entry.isDirectory()) {
            TarArchiveEntry[] entries = entry.getDirectoryEntries();
            if (entries.length > 0) {
                entry = entries[0];
            } else {
                // empty directory
                entry = tarStream.getNextTarEntry();
            }
        }
        if (entry == null) {
            System.out.println("tar file does not contain logfile");
            return null;
        }

        // search for proper file
        while (entry != null && !entry.getName().endsWith("sparkmonitor.log")) {
            entry = tarStream.getNextTarEntry();
        }

        if (entry == null) {
            System.out.println("tar file does not contain logfile");
            return null;
        }

        // we have reached the proper position
        return new InputStreamReader(tarStream);

    } catch (IOException e) {
        // not a bz2 file
        System.out.println("File has bz2 ending, but seems to be not bz2");
        e.printStackTrace();
    } catch (CompressorException e) {
        e.printStackTrace();
    }
    return null;
}