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

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

Introduction

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

Prototype

public boolean isDirectory() 

Source Link

Document

Return whether or not this entry represents a directory.

Usage

From source file:org.vafer.jdeb.mapping.PermMapper.java

public TarArchiveEntry map(final TarArchiveEntry entry) {
    final String name = entry.getName();

    final TarArchiveEntry newEntry = new TarArchiveEntry(prefix + '/' + Utils.stripPath(strip, name), true);

    // Set ownership
    if (uid > -1) {
        newEntry.setUserId(uid);/*from w w w .  j  av a 2s.c  om*/
    } else {
        newEntry.setUserId(entry.getUserId());
    }
    if (gid > -1) {
        newEntry.setGroupId(gid);
    } else {
        newEntry.setGroupId(entry.getGroupId());
    }
    if (user != null) {
        newEntry.setUserName(user);
    } else {
        newEntry.setUserName(entry.getUserName());
    }
    if (group != null) {
        newEntry.setGroupName(group);
    } else {
        newEntry.setGroupName(entry.getGroupName());
    }

    // Set permissions
    if (newEntry.isDirectory()) {
        if (dirMode > -1) {
            newEntry.setMode(dirMode);
        } else {
            newEntry.setMode(entry.getMode());
        }
    } else {
        if (fileMode > -1) {
            newEntry.setMode(fileMode);
        } else {
            newEntry.setMode(entry.getMode());

        }
    }

    newEntry.setSize(entry.getSize());

    return newEntry;
}

From source file:org.waarp.common.tar.TarUtility.java

/**
 * Extract all files from Tar into the specified directory
 * /*from w ww  .j  a va 2 s .c om*/
 * @param tarFile
 * @param directory
 * @return the list of extracted filenames
 * @throws IOException
 */
public static List<String> unTar(File tarFile, File directory) throws IOException {
    List<String> result = new ArrayList<String>();
    InputStream inputStream = new FileInputStream(tarFile);
    TarArchiveInputStream in = new TarArchiveInputStream(inputStream);
    TarArchiveEntry entry = in.getNextTarEntry();
    while (entry != null) {
        if (entry.isDirectory()) {
            entry = in.getNextTarEntry();
            continue;
        }
        File curfile = new File(directory, entry.getName());
        File parent = curfile.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        OutputStream out = new FileOutputStream(curfile);
        IOUtils.copy(in, out);
        out.close();
        result.add(entry.getName());
        entry = in.getNextTarEntry();
    }
    in.close();
    return result;
}

From source file:org.xenmaster.setup.debian.Bootstrapper.java

protected boolean downloadNetbootFiles() {
    if (System.currentTimeMillis() - lastChecked < 50 * 60 * 1000) {
        return false;
    }//from w w  w .j  a  v a 2 s .  c o  m

    File localVersionFile = new File(Settings.getInstance().getString("StorePath") + "/netboot/version");
    int localVersion = -1;
    try (FileInputStream fis = new FileInputStream(localVersionFile)) {
        localVersion = Integer.parseInt(IOUtils.toString(fis));
    } catch (IOException | NumberFormatException ex) {
        Logger.getLogger(getClass()).error("Failed to retrieve local version file", ex);
    }

    int remoteVersion = -1;
    try {
        remoteVersion = Integer.parseInt(IOUtils.toString(XenMasterSite.getFileAsStream("/netboot/version")));
    } catch (IOException | NumberFormatException ex) {
        Logger.getLogger(getClass()).error("Failed to retrieve remote version file", ex);
    }

    lastChecked = System.currentTimeMillis();

    if (localVersion < remoteVersion) {
        Logger.getLogger(getClass())
                .info("New version " + remoteVersion + " found. Please hold while downloading netboot data");
        try {
            TarArchiveInputStream tais = new TarArchiveInputStream(
                    new GZIPInputStream(XenMasterSite.getFileAsStream("/netboot/netboot.tar.gz")));
            TarArchiveEntry tae = null;
            FileOutputStream fos = null;
            while ((tae = tais.getNextTarEntry()) != null) {
                if (tais.canReadEntryData(tae)) {
                    String target = Settings.getInstance().getString("StorePath") + "/" + tae.getName();

                    if (tae.isSymbolicLink()) {
                        Path targetFile = FileSystems.getDefault().getPath(tae.getLinkName());
                        Path linkName = FileSystems.getDefault().getPath(target);

                        // Link might already have been written as null file
                        if (targetFile.toFile().exists()) {
                            targetFile.toFile().delete();
                        }

                        Files.createSymbolicLink(linkName, targetFile);
                        Logger.getLogger(getClass()).info(
                                "Created sym link " + linkName.toString() + " -> " + targetFile.toString());
                    } else if (tae.isDirectory()) {
                        new File(target).mkdir();

                        Logger.getLogger(getClass()).info("Created dir " + target);
                    } else if (tae.isFile()) {
                        fos = new FileOutputStream(target);
                        byte[] b = new byte[1024];
                        int curPos = 0;
                        while (tais.available() > 0) {
                            tais.read(b, curPos, 1024);
                            fos.write(b, curPos, 1024);
                        }

                        fos.flush();
                        fos.close();

                        Logger.getLogger(getClass()).info("Wrote file " + target);
                    }
                }
            }

            tais.close();
            // Write local version
            IOUtils.write("" + remoteVersion, new FileOutputStream(localVersionFile));
        } catch (IOException ex) {
            Logger.getLogger(getClass()).error("Failed to download netboot files", ex);
        }
    } else {
        return false;
    }

    return true;
}

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

public static Reader getTarBZ2InputStream(File file) {
    try {/*from ww  w  .  j  a  va  2  s  . co 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;
}

From source file:uk.ac.man.cs.mdsd.webgen.ui.wizards.NewWebgenProjectOperation.java

@SuppressWarnings("unused")
private void extractTGZ(final URL sourceURL, final IPath rootPath, final int removeSegemntsCount,
        final IPath[] ignoreFilter, final Map<IPath, IPath> renames) {

    try {/*ww  w.j a va 2  s  . co m*/
        final File tgzPath = new File(FileLocator.resolve(sourceURL).toURI());
        final TarArchiveInputStream tarIn = new TarArchiveInputStream(
                new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzPath))));
        TarArchiveEntry entry = (TarArchiveEntry) tarIn.getNextEntry();
        while (entry != null) {
            IPath path = new Path(entry.getName()).removeFirstSegments(removeSegemntsCount);
            if (renames.containsKey(path)) {
                path = renames.get(path);
            }
            if (rootPath != null) {
                path = new Path(rootPath.toString() + path.toString());
            }
            if (!path.isEmpty() && !ignoreEntry(path, ignoreFilter)) {
                if (entry.isDirectory()) {
                    final IFolder folder = projectHandle.getFolder(path);
                    if (!folder.exists()) {
                        folder.create(false, true, null);
                    }
                } else {
                    byte[] contents = new byte[(int) entry.getSize()];
                    tarIn.read(contents);
                    final IFile file = projectHandle.getFile(path);
                    file.create(new ByteArrayInputStream(contents), false, null);
                }
            }
            entry = (TarArchiveEntry) tarIn.getNextEntry();
        }
        tarIn.close();
    } catch (URISyntaxException | IOException | CoreException e) {
        WebgenUiPlugin.log(e);
    }
}