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

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

Introduction

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

Prototype

public String getLinkName() 

Source Link

Document

Get this entry's link name.

Usage

From source file:com.opentable.db.postgres.embedded.BundledPostgresBinaryResolver.java

/**
 * Unpack archive compressed by tar with bzip2 compression. By default system tar is used
 * (faster). If not found, then the java implementation takes place.
 *
 * @param tbzPath/*from w w  w .j a  va  2 s  .  c  om*/
 *        The archive path.
 * @param targetDir
 *        The directory to extract the content to.
 */
private static void extractTxz(String tbzPath, String targetDir) throws IOException {
    try (InputStream in = Files.newInputStream(Paths.get(tbzPath));
            XZInputStream xzIn = new XZInputStream(in);
            TarArchiveInputStream tarIn = new TarArchiveInputStream(xzIn)) {
        TarArchiveEntry entry;

        while ((entry = tarIn.getNextTarEntry()) != null) {
            final String individualFile = entry.getName();
            final File fsObject = new File(targetDir + "/" + individualFile);

            if (entry.isSymbolicLink() || entry.isLink()) {
                Path target = FileSystems.getDefault().getPath(entry.getLinkName());
                Files.createSymbolicLink(fsObject.toPath(), target);
            } else if (entry.isFile()) {
                byte[] content = new byte[(int) entry.getSize()];
                int read = tarIn.read(content, 0, content.length);
                Verify.verify(read != -1, "could not read %s", individualFile);
                mkdirs(fsObject.getParentFile());
                try (OutputStream outputFile = new FileOutputStream(fsObject)) {
                    IOUtils.write(content, outputFile);
                }
            } else if (entry.isDirectory()) {
                mkdirs(fsObject);
            } else {
                throw new UnsupportedOperationException(
                        String.format("Unsupported entry found: %s", individualFile));
            }

            if (individualFile.startsWith("bin/") || individualFile.startsWith("./bin/")) {
                fsObject.setExecutable(true);
            }
        }
    }
}

From source file:io.sloeber.core.managers.InternalPackageManager.java

public static IStatus extract(ArchiveInputStream in, File destFolder, int stripPath, boolean overwrite,
        IProgressMonitor pMonitor) throws IOException, InterruptedException {

    // Folders timestamps must be set at the end of archive extraction
    // (because creating a file in a folder alters the folder's timestamp)
    Map<File, Long> foldersTimestamps = new HashMap<>();

    String pathPrefix = new String();

    Map<File, File> hardLinks = new HashMap<>();
    Map<File, Integer> hardLinksMode = new HashMap<>();
    Map<File, String> symLinks = new HashMap<>();
    Map<File, Long> symLinksModifiedTimes = new HashMap<>();

    // Cycle through all the archive entries
    while (true) {
        ArchiveEntry entry = in.getNextEntry();
        if (entry == null) {
            break;
        }/*from www  .  j  a  va  2 s. c o m*/

        // Extract entry info
        long size = entry.getSize();
        String name = entry.getName();
        boolean isDirectory = entry.isDirectory();
        boolean isLink = false;
        boolean isSymLink = false;
        String linkName = null;
        Integer mode = null;
        Long modifiedTime = new Long(entry.getLastModifiedDate().getTime());

        pMonitor.subTask("Processing " + name); //$NON-NLS-1$

        {
            // Skip MacOSX metadata
            // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
            int slash = name.lastIndexOf('/');
            if (slash == -1) {
                if (name.startsWith("._")) { //$NON-NLS-1$
                    continue;
                }
            } else {
                if (name.substring(slash + 1).startsWith("._")) { //$NON-NLS-1$
                    continue;
                }
            }
        }

        // Skip git metadata
        // http://www.unix.com/unix-for-dummies-questions-and-answers/124958-file-pax_global_header-means-what.html
        if (name.contains("pax_global_header")) { //$NON-NLS-1$
            continue;
        }

        if (entry instanceof TarArchiveEntry) {
            TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
            mode = new Integer(tarEntry.getMode());
            isLink = tarEntry.isLink();
            isSymLink = tarEntry.isSymbolicLink();
            linkName = tarEntry.getLinkName();
        }

        // On the first archive entry, if requested, detect the common path
        // prefix to be stripped from filenames
        int localstripPath = stripPath;
        if (localstripPath > 0 && pathPrefix.isEmpty()) {
            int slash = 0;
            while (localstripPath > 0) {
                slash = name.indexOf("/", slash); //$NON-NLS-1$
                if (slash == -1) {
                    throw new IOException(Messages.Manager_archiver_eror_single_root_folder_required);
                }
                slash++;
                localstripPath--;
            }
            pathPrefix = name.substring(0, slash);
        }

        // Strip the common path prefix when requested
        if (!name.startsWith(pathPrefix)) {
            throw new IOException(Messages.Manager_archive_error_root_folder_name_mismatch.replace(FILE, name)
                    .replace(FOLDER, pathPrefix));

        }
        name = name.substring(pathPrefix.length());
        if (name.isEmpty()) {
            continue;
        }
        File outputFile = new File(destFolder, name);

        File outputLinkedFile = null;
        if (isLink && linkName != null) {
            if (!linkName.startsWith(pathPrefix)) {
                throw new IOException(Messages.Manager_archive_error_root_folder_name_mismatch
                        .replace(FILE, name).replace(FOLDER, pathPrefix));
            }
            linkName = linkName.substring(pathPrefix.length());
            outputLinkedFile = new File(destFolder, linkName);
        }
        if (isSymLink) {
            // Symbolic links are referenced with relative paths
            outputLinkedFile = new File(linkName);
            if (outputLinkedFile.isAbsolute()) {
                System.err.println(Messages.Manager_archive_error_symbolic_link_to_absolute_path
                        .replace(FILE, outputFile.toString()).replace(FOLDER, outputLinkedFile.toString()));
                System.err.println();
            }
        }

        // Safety check
        if (isDirectory) {
            if (outputFile.isFile() && !overwrite) {
                throw new IOException(
                        Messages.Manager_Cant_create_folder_exists.replace(FILE, outputFile.getPath()));
            }
        } else {
            // - isLink
            // - isSymLink
            // - anything else
            if (outputFile.exists() && !overwrite) {
                throw new IOException(
                        Messages.Manager_Cant_extract_file_exist.replace(FILE, outputFile.getPath()));
            }
        }

        // Extract the entry
        if (isDirectory) {
            if (!outputFile.exists() && !outputFile.mkdirs()) {
                throw new IOException(Messages.Manager_Cant_create_folder.replace(FILE, outputFile.getPath()));
            }
            foldersTimestamps.put(outputFile, modifiedTime);
        } else if (isLink) {
            hardLinks.put(outputFile, outputLinkedFile);
            hardLinksMode.put(outputFile, mode);
        } else if (isSymLink) {
            symLinks.put(outputFile, linkName);
            symLinksModifiedTimes.put(outputFile, modifiedTime);
        } else {
            // Create the containing folder if not exists
            if (!outputFile.getParentFile().isDirectory()) {
                outputFile.getParentFile().mkdirs();
            }
            copyStreamToFile(in, size, outputFile);
            outputFile.setLastModified(modifiedTime.longValue());
        }

        // Set file/folder permission
        if (mode != null && !isSymLink && outputFile.exists()) {
            chmod(outputFile, mode.intValue());
        }
    }

    for (Map.Entry<File, File> entry : hardLinks.entrySet()) {
        if (entry.getKey().exists() && overwrite) {
            entry.getKey().delete();
        }
        link(entry.getValue(), entry.getKey());
        Integer mode = hardLinksMode.get(entry.getKey());
        if (mode != null) {
            chmod(entry.getKey(), mode.intValue());
        }
    }

    for (Map.Entry<File, String> entry : symLinks.entrySet()) {
        if (entry.getKey().exists() && overwrite) {
            entry.getKey().delete();
        }

        symlink(entry.getValue(), entry.getKey());
        entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey()).longValue());
    }

    // Set folders timestamps
    for (Map.Entry<File, Long> entry : foldersTimestamps.entrySet()) {
        entry.getKey().setLastModified(entry.getValue().longValue());
    }

    return Status.OK_STATUS;

}

From source file:io.sloeber.core.managers.Manager.java

public static IStatus extract(ArchiveInputStream in, File destFolder, int stripPath, boolean overwrite,
        IProgressMonitor pMonitor) throws IOException, InterruptedException {

    // Folders timestamps must be set at the end of archive extraction
    // (because creating a file in a folder alters the folder's timestamp)
    Map<File, Long> foldersTimestamps = new HashMap<>();

    String pathPrefix = ""; //$NON-NLS-1$

    Map<File, File> hardLinks = new HashMap<>();
    Map<File, Integer> hardLinksMode = new HashMap<>();
    Map<File, String> symLinks = new HashMap<>();
    Map<File, Long> symLinksModifiedTimes = new HashMap<>();

    // Cycle through all the archive entries
    while (true) {
        ArchiveEntry entry = in.getNextEntry();
        if (entry == null) {
            break;
        }//  w  w w. j av  a 2s  .  c o m

        // Extract entry info
        long size = entry.getSize();
        String name = entry.getName();
        boolean isDirectory = entry.isDirectory();
        boolean isLink = false;
        boolean isSymLink = false;
        String linkName = null;
        Integer mode = null;
        Long modifiedTime = new Long(entry.getLastModifiedDate().getTime());

        pMonitor.subTask("Processing " + name); //$NON-NLS-1$

        {
            // Skip MacOSX metadata
            // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
            int slash = name.lastIndexOf('/');
            if (slash == -1) {
                if (name.startsWith("._")) { //$NON-NLS-1$
                    continue;
                }
            } else {
                if (name.substring(slash + 1).startsWith("._")) { //$NON-NLS-1$
                    continue;
                }
            }
        }

        // Skip git metadata
        // http://www.unix.com/unix-for-dummies-questions-and-answers/124958-file-pax_global_header-means-what.html
        if (name.contains("pax_global_header")) { //$NON-NLS-1$
            continue;
        }

        if (entry instanceof TarArchiveEntry) {
            TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
            mode = new Integer(tarEntry.getMode());
            isLink = tarEntry.isLink();
            isSymLink = tarEntry.isSymbolicLink();
            linkName = tarEntry.getLinkName();
        }

        // On the first archive entry, if requested, detect the common path
        // prefix to be stripped from filenames
        int localstripPath = stripPath;
        if (localstripPath > 0 && pathPrefix.isEmpty()) {
            int slash = 0;
            while (localstripPath > 0) {
                slash = name.indexOf("/", slash); //$NON-NLS-1$
                if (slash == -1) {
                    throw new IOException(Messages.Manager_no_single_root_folder);
                }
                slash++;
                localstripPath--;
            }
            pathPrefix = name.substring(0, slash);
        }

        // Strip the common path prefix when requested
        if (!name.startsWith(pathPrefix)) {
            throw new IOException(Messages.Manager_no_single_root_folder_while_file + name
                    + Messages.Manager_is_outside + pathPrefix);
        }
        name = name.substring(pathPrefix.length());
        if (name.isEmpty()) {
            continue;
        }
        File outputFile = new File(destFolder, name);

        File outputLinkedFile = null;
        if (isLink && linkName != null) {
            if (!linkName.startsWith(pathPrefix)) {
                throw new IOException(Messages.Manager_no_single_root_folder_while_file + linkName
                        + Messages.Manager_is_outside + pathPrefix);
            }
            linkName = linkName.substring(pathPrefix.length());
            outputLinkedFile = new File(destFolder, linkName);
        }
        if (isSymLink) {
            // Symbolic links are referenced with relative paths
            outputLinkedFile = new File(linkName);
            if (outputLinkedFile.isAbsolute()) {
                System.err.println(Messages.Manager_Warning_file + outputFile
                        + Messages.Manager_links_to_absolute_path + outputLinkedFile);
                System.err.println();
            }
        }

        // Safety check
        if (isDirectory) {
            if (outputFile.isFile() && !overwrite) {
                throw new IOException(
                        Messages.Manager_Cant_create_folder + outputFile + Messages.Manager_File_exists);
            }
        } else {
            // - isLink
            // - isSymLink
            // - anything else
            if (outputFile.exists() && !overwrite) {
                throw new IOException(
                        Messages.Manager_Cant_extract_file + outputFile + Messages.Manager_File_already_exists);
            }
        }

        // Extract the entry
        if (isDirectory) {
            if (!outputFile.exists() && !outputFile.mkdirs()) {
                throw new IOException(Messages.Manager_Cant_create_folder + outputFile);
            }
            foldersTimestamps.put(outputFile, modifiedTime);
        } else if (isLink) {
            hardLinks.put(outputFile, outputLinkedFile);
            hardLinksMode.put(outputFile, mode);
        } else if (isSymLink) {
            symLinks.put(outputFile, linkName);
            symLinksModifiedTimes.put(outputFile, modifiedTime);
        } else {
            // Create the containing folder if not exists
            if (!outputFile.getParentFile().isDirectory()) {
                outputFile.getParentFile().mkdirs();
            }
            copyStreamToFile(in, size, outputFile);
            outputFile.setLastModified(modifiedTime.longValue());
        }

        // Set file/folder permission
        if (mode != null && !isSymLink && outputFile.exists()) {
            chmod(outputFile, mode.intValue());
        }
    }

    for (Map.Entry<File, File> entry : hardLinks.entrySet()) {
        if (entry.getKey().exists() && overwrite) {
            entry.getKey().delete();
        }
        link(entry.getValue(), entry.getKey());
        Integer mode = hardLinksMode.get(entry.getKey());
        if (mode != null) {
            chmod(entry.getKey(), mode.intValue());
        }
    }

    for (Map.Entry<File, String> entry : symLinks.entrySet()) {
        if (entry.getKey().exists() && overwrite) {
            entry.getKey().delete();
        }
        symlink(entry.getValue(), entry.getKey());
        entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey()).longValue());
    }

    // Set folders timestamps
    for (Map.Entry<File, Long> entry : foldersTimestamps.entrySet()) {
        entry.getKey().setLastModified(entry.getValue().longValue());
    }

    return Status.OK_STATUS;

}

From source file:com.puppetlabs.geppetto.forge.util.TarUtils.java

/**
 * Unpack the content read from <i>source</i> into <i>targetFolder</i>. If the
 * <i>skipTopFolder</i> is set, then don't assume that the archive contains one
 * single folder and unpack the content of that folder, not including the folder
 * itself./*w w  w.  j  av  a 2 s  . com*/
 * 
 * @param source
 *            The input source. Must be in <i>TAR</i> format.
 * @param targetFolder
 *            The destination folder for the unpack. Not used when a <tt>fileCatcher</tt> is provided
 * @param skipTopFolder
 *            Set to <code>true</code> to unpack beneath the top folder
 *            of the archive. The archive must consist of one single folder and nothing else
 *            in order for this to work.
 * @param fileCatcher
 *            Used when specific files should be picked from the archive without writing them to disk. Can be
 *            <tt>null</tt>.
 * @throws IOException
 */
public static void unpack(InputStream source, File targetFolder, boolean skipTopFolder, FileCatcher fileCatcher)
        throws IOException {
    String topFolderName = null;
    Map<File, Map<Integer, List<String>>> chmodMap = new HashMap<File, Map<Integer, List<String>>>();
    TarArchiveInputStream in = new TarArchiveInputStream(source);
    try {
        TarArchiveEntry te = in.getNextTarEntry();
        if (te == null) {
            throw new IOException("No entry in the tar file");
        }
        do {
            if (te.isGlobalPaxHeader())
                continue;

            String name = te.getName();
            if (skipTopFolder) {
                int firstSlash = name.indexOf('/');
                if (firstSlash < 0)
                    throw new IOException("Archive doesn't contain one single folder");

                String tfName = name.substring(0, firstSlash);
                if (topFolderName == null)
                    topFolderName = tfName;
                else if (!tfName.equals(topFolderName))
                    throw new IOException("Archive doesn't contain one single folder");
                name = name.substring(firstSlash + 1);
            }
            if (name.length() == 0)
                continue;

            String linkName = te.getLinkName();
            if (linkName != null) {
                if (linkName.trim().equals(""))
                    linkName = null;
            }

            if (fileCatcher != null) {
                if (linkName == null && !te.isDirectory() && fileCatcher.accept(name)) {
                    if (fileCatcher.catchData(name, in))
                        // We're done here
                        return;
                }
                continue;
            }

            File outFile = new File(targetFolder, name);
            if (linkName != null) {
                if (!OsUtil.link(targetFolder, name, te.getLinkName()))
                    throw new IOException("Archive contains links but they are not supported on this platform");
            } else {
                if (te.isDirectory()) {
                    outFile.mkdirs();
                } else {
                    outFile.getParentFile().mkdirs();
                    OutputStream target = new FileOutputStream(outFile);
                    StreamUtil.copy(in, target);
                    target.close();
                    outFile.setLastModified(te.getModTime().getTime());
                }
                registerChmodFile(chmodMap, targetFolder, Integer.valueOf(te.getMode()), name);
            }
        } while ((te = in.getNextTarEntry()) != null);
    } finally {
        StreamUtil.close(in);
    }
    chmod(chmodMap);
}

From source file:com.facebook.buck.util.unarchive.Untar.java

/** Writes out a symlink from an archive */
private void writeSymbolicLink(DirectoryCreator creator, Path target, TarArchiveEntry entry)
        throws IOException {
    prepareForFile(creator, target);// w  w w  .j av  a2 s  . com
    creator.getFilesystem().createSymLink(target, Paths.get(entry.getLinkName()), true);
}

From source file:com.facebook.buck.util.unarchive.Untar.java

/**
 * Cleans up the destination for the symlink, and symlink file -> symlink target is recorded in
 * {@code windowsSymlinkMap}//from   w w w  .jav  a  2 s  .  co m
 */
private void recordSymbolicLinkForWindows(DirectoryCreator creator, Path destPath, TarArchiveEntry entry,
        Map<Path, Path> windowsSymlinkMap) throws IOException {
    prepareForFile(creator, destPath);
    Path linkPath = Paths.get(entry.getLinkName());
    if (destPath.isAbsolute()) {
        windowsSymlinkMap.put(destPath, linkPath);
    } else {
        // Symlink might be '../foo/Bar', so make sure we resolve relative to the src file
        // We make them relative to the file again when we write them out
        Path destinationRelativeTargetPath = destPath.getParent().resolve(linkPath).normalize();
        windowsSymlinkMap.put(destPath, destinationRelativeTargetPath);
    }
}

From source file:cc.arduino.utils.ArchiveExtractor.java

public void extract(File archiveFile, File destFolder, int stripPath, boolean overwrite)
        throws IOException, InterruptedException {

    // Folders timestamps must be set at the end of archive extraction
    // (because creating a file in a folder alters the folder's timestamp)
    Map<File, Long> foldersTimestamps = new HashMap<>();

    ArchiveInputStream in = null;//  ww w. j  av  a2s.c  o  m
    try {

        // Create an ArchiveInputStream with the correct archiving algorithm
        if (archiveFile.getName().endsWith("tar.bz2")) {
            in = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(archiveFile)));
        } else if (archiveFile.getName().endsWith("zip")) {
            in = new ZipArchiveInputStream(new FileInputStream(archiveFile));
        } else if (archiveFile.getName().endsWith("tar.gz")) {
            in = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(archiveFile)));
        } else if (archiveFile.getName().endsWith("tar")) {
            in = new TarArchiveInputStream(new FileInputStream(archiveFile));
        } else {
            throw new IOException("Archive format not supported.");
        }

        String pathPrefix = "";

        Map<File, File> hardLinks = new HashMap<>();
        Map<File, Integer> hardLinksMode = new HashMap<>();
        Map<File, String> symLinks = new HashMap<>();
        Map<File, Long> symLinksModifiedTimes = new HashMap<>();

        // Cycle through all the archive entries
        while (true) {
            ArchiveEntry entry = in.getNextEntry();
            if (entry == null) {
                break;
            }

            // Extract entry info
            long size = entry.getSize();
            String name = entry.getName();
            boolean isDirectory = entry.isDirectory();
            boolean isLink = false;
            boolean isSymLink = false;
            String linkName = null;
            Integer mode = null;
            long modifiedTime = entry.getLastModifiedDate().getTime();

            {
                // Skip MacOSX metadata
                // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
                int slash = name.lastIndexOf('/');
                if (slash == -1) {
                    if (name.startsWith("._")) {
                        continue;
                    }
                } else {
                    if (name.substring(slash + 1).startsWith("._")) {
                        continue;
                    }
                }
            }

            // Skip git metadata
            // http://www.unix.com/unix-for-dummies-questions-and-answers/124958-file-pax_global_header-means-what.html
            if (name.contains("pax_global_header")) {
                continue;
            }

            if (entry instanceof TarArchiveEntry) {
                TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
                mode = tarEntry.getMode();
                isLink = tarEntry.isLink();
                isSymLink = tarEntry.isSymbolicLink();
                linkName = tarEntry.getLinkName();
            }

            // On the first archive entry, if requested, detect the common path
            // prefix to be stripped from filenames
            if (stripPath > 0 && pathPrefix.isEmpty()) {
                int slash = 0;
                while (stripPath > 0) {
                    slash = name.indexOf("/", slash);
                    if (slash == -1) {
                        throw new IOException("Invalid archive: it must contain a single root folder");
                    }
                    slash++;
                    stripPath--;
                }
                pathPrefix = name.substring(0, slash);
            }

            // Strip the common path prefix when requested
            if (!name.startsWith(pathPrefix)) {
                throw new IOException("Invalid archive: it must contain a single root folder while file " + name
                        + " is outside " + pathPrefix);
            }
            name = name.substring(pathPrefix.length());
            if (name.isEmpty()) {
                continue;
            }
            File outputFile = new File(destFolder, name);

            File outputLinkedFile = null;
            if (isLink) {
                if (!linkName.startsWith(pathPrefix)) {
                    throw new IOException("Invalid archive: it must contain a single root folder while file "
                            + linkName + " is outside " + pathPrefix);
                }
                linkName = linkName.substring(pathPrefix.length());
                outputLinkedFile = new File(destFolder, linkName);
            }
            if (isSymLink) {
                // Symbolic links are referenced with relative paths
                outputLinkedFile = new File(linkName);
                if (outputLinkedFile.isAbsolute()) {
                    System.err.println(I18n.format(tr("Warning: file {0} links to an absolute path {1}"),
                            outputFile, outputLinkedFile));
                    System.err.println();
                }
            }

            // Safety check
            if (isDirectory) {
                if (outputFile.isFile() && !overwrite) {
                    throw new IOException(
                            "Can't create folder " + outputFile + ", a file with the same name exists!");
                }
            } else {
                // - isLink
                // - isSymLink
                // - anything else
                if (outputFile.exists() && !overwrite) {
                    throw new IOException("Can't extract file " + outputFile + ", file already exists!");
                }
            }

            // Extract the entry
            if (isDirectory) {
                if (!outputFile.exists() && !outputFile.mkdirs()) {
                    throw new IOException("Could not create folder: " + outputFile);
                }
                foldersTimestamps.put(outputFile, modifiedTime);
            } else if (isLink) {
                hardLinks.put(outputFile, outputLinkedFile);
                hardLinksMode.put(outputFile, mode);
            } else if (isSymLink) {
                symLinks.put(outputFile, linkName);
                symLinksModifiedTimes.put(outputFile, modifiedTime);
            } else {
                // Create the containing folder if not exists
                if (!outputFile.getParentFile().isDirectory()) {
                    outputFile.getParentFile().mkdirs();
                }
                copyStreamToFile(in, size, outputFile);
                outputFile.setLastModified(modifiedTime);
            }

            // Set file/folder permission
            if (mode != null && !isSymLink && outputFile.exists()) {
                platform.chmod(outputFile, mode);
            }
        }

        for (Map.Entry<File, File> entry : hardLinks.entrySet()) {
            if (entry.getKey().exists() && overwrite) {
                entry.getKey().delete();
            }
            platform.link(entry.getValue(), entry.getKey());
            Integer mode = hardLinksMode.get(entry.getKey());
            if (mode != null) {
                platform.chmod(entry.getKey(), mode);
            }
        }

        for (Map.Entry<File, String> entry : symLinks.entrySet()) {
            if (entry.getKey().exists() && overwrite) {
                entry.getKey().delete();
            }
            platform.symlink(entry.getValue(), entry.getKey());
            entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey()));
        }

    } finally {
        IOUtils.closeQuietly(in);
    }

    // Set folders timestamps
    for (File folder : foldersTimestamps.keySet()) {
        folder.setLastModified(foldersTimestamps.get(folder));
    }
}

From source file:com.google.devtools.build.lib.bazel.repository.TarGzFunction.java

@Nullable
@Override/*from w  w w .j  ava 2  s . co m*/
public SkyValue compute(SkyKey skyKey, Environment env) throws RepositoryFunctionException {
    DecompressorDescriptor descriptor = (DecompressorDescriptor) skyKey.argument();
    Optional<String> prefix = descriptor.prefix();
    boolean foundPrefix = false;

    try (GZIPInputStream gzipStream = new GZIPInputStream(
            new FileInputStream(descriptor.archivePath().getPathFile()))) {
        TarArchiveInputStream tarStream = new TarArchiveInputStream(gzipStream);
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix);
            foundPrefix = foundPrefix || entryPath.foundPrefix();
            if (entryPath.skip()) {
                continue;
            }

            Path filename = descriptor.repositoryPath().getRelative(entryPath.getPathFragment());
            FileSystemUtils.createDirectoryAndParents(filename.getParentDirectory());
            if (entry.isDirectory()) {
                FileSystemUtils.createDirectoryAndParents(filename);
            } else {
                if (entry.isSymbolicLink()) {
                    PathFragment linkName = new PathFragment(entry.getLinkName());
                    if (linkName.isAbsolute()) {
                        linkName = linkName.relativeTo(PathFragment.ROOT_DIR);
                        linkName = descriptor.repositoryPath().getRelative(linkName).asFragment();
                    }
                    FileSystemUtils.ensureSymbolicLink(filename, linkName);
                } else {
                    Files.copy(tarStream, filename.getPathFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
                    filename.chmod(entry.getMode());
                }
            }
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }

    if (prefix.isPresent() && !foundPrefix) {
        throw new RepositoryFunctionException(
                new IOException("Prefix " + prefix.get() + " was given, but not found in the archive"),
                Transience.PERSISTENT);
    }

    return new DecompressorValue(descriptor.repositoryPath());
}

From source file:com.google.devtools.build.lib.bazel.repository.CompressedTarFunction.java

@Override
public Path decompress(DecompressorDescriptor descriptor) throws RepositoryFunctionException {
    Optional<String> prefix = descriptor.prefix();
    boolean foundPrefix = false;

    try (InputStream decompressorStream = getDecompressorStream(descriptor)) {
        TarArchiveInputStream tarStream = new TarArchiveInputStream(decompressorStream);
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix);
            foundPrefix = foundPrefix || entryPath.foundPrefix();
            if (entryPath.skip()) {
                continue;
            }/*from  w  w  w  . j a  va2 s  .  c  o m*/

            Path filename = descriptor.repositoryPath().getRelative(entryPath.getPathFragment());
            FileSystemUtils.createDirectoryAndParents(filename.getParentDirectory());
            if (entry.isDirectory()) {
                FileSystemUtils.createDirectoryAndParents(filename);
            } else {
                if (entry.isSymbolicLink() || entry.isLink()) {
                    PathFragment linkName = new PathFragment(entry.getLinkName());
                    boolean wasAbsolute = linkName.isAbsolute();
                    // Strip the prefix from the link path if set.
                    linkName = StripPrefixedPath.maybeDeprefix(linkName.getPathString(), prefix)
                            .getPathFragment();
                    if (wasAbsolute) {
                        // Recover the path to an absolute path as maybeDeprefix() relativize the path
                        // even if the prefix is not set
                        linkName = descriptor.repositoryPath().getRelative(linkName).asFragment();
                    }
                    if (entry.isSymbolicLink()) {
                        FileSystemUtils.ensureSymbolicLink(filename, linkName);
                    } else {
                        FileSystemUtils.createHardLink(filename,
                                descriptor.repositoryPath().getRelative(linkName));
                    }
                } else {
                    Files.copy(tarStream, filename.getPathFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
                    filename.chmod(entry.getMode());

                    // This can only be done on real files, not links, or it will skip the reader to
                    // the next "real" file to try to find the mod time info.
                    Date lastModified = entry.getLastModifiedDate();
                    filename.setLastModifiedTime(lastModified.getTime());
                }
            }
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }

    if (prefix.isPresent() && !foundPrefix) {
        throw new RepositoryFunctionException(
                new IOException("Prefix " + prefix.get() + " was given, but not found in the archive"),
                Transience.PERSISTENT);
    }

    return descriptor.repositoryPath();
}

From source file:com.playonlinux.core.utils.archive.Tar.java

/**
 * Uncompress a tar/*from   w  w w .  java2  s .c o m*/
 *
 * @param countingInputStream
 *            to count the number of byte extracted
 * @param outputDir
 *            The directory where files should be extracted
 * @return A list of extracted files
 * @throws ArchiveException
 *             if the process fails
 */
private List<File> uncompress(final InputStream inputStream, CountingInputStream countingInputStream,
        final File outputDir, long finalSize, Consumer<ProgressEntity> stateCallback) {
    final List<File> uncompressedFiles = new LinkedList<>();
    try (ArchiveInputStream debInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar",
            inputStream)) {
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                LOGGER.info(String.format("Attempting to write output directory %s.",
                        outputFile.getAbsolutePath()));

                if (!outputFile.exists()) {
                    LOGGER.info(String.format("Attempting to createPrefix output directory %s.",
                            outputFile.getAbsolutePath()));
                    Files.createDirectories(outputFile.toPath());
                }
            } else {
                LOGGER.info(String.format("Creating output file %s (%s).", outputFile.getAbsolutePath(),
                        entry.getMode()));

                if (entry.isSymbolicLink()) {
                    Files.createSymbolicLink(Paths.get(outputFile.getAbsolutePath()),
                            Paths.get(entry.getLinkName()));
                } else {
                    try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) {
                        IOUtils.copy(debInputStream, outputFileStream);

                        Files.setPosixFilePermissions(Paths.get(outputFile.getPath()),
                                com.playonlinux.core.utils.Files.octToPosixFilePermission(entry.getMode()));
                    }
                }

            }
            uncompressedFiles.add(outputFile);

            stateCallback.accept(new ProgressEntity.Builder()
                    .withPercent((double) countingInputStream.getCount() / (double) finalSize * (double) 100)
                    .withProgressText("Extracting " + outputFile.getName()).build());

        }
        return uncompressedFiles;
    } catch (IOException | org.apache.commons.compress.archivers.ArchiveException e) {
        throw new ArchiveException("Unable to extract the file", e);
    }
}