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

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

Introduction

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

Prototype

public boolean isSymbolicLink() 

Source Link

Document

Check if this is a symbolic link entry.

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//  w  w w .  java  2 s .co m
 *        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:com.sshtools.common.vomanagementtool.common.VOHelper.java

private static void untar(InputStream in, String untarDir) throws IOException {

    TarArchiveInputStream tin = new TarArchiveInputStream(in);
    TarArchiveEntry entry = tin.getNextTarEntry();

    //TarInputStream tin = new TarInputStream(in);
    //TarEntry tarEntry = tin.getNextEntry();
    if (new File(untarDir).exists()) {
        //To handle the normal files (not symbolic files)
        while (entry != null) {
            if (!entry.isSymbolicLink()) {
                String filename = untarDir + File.separatorChar + entry.getName();
                File destPath = new File(filename);

                if (!entry.isDirectory()) {
                    FileOutputStream fout = new FileOutputStream(destPath);
                    IOUtils.copy(tin, fout);
                    //tin.copyEntryContents(fout);
                    fout.close();//from   ww w.  j  a va  2  s  .  c om
                } else {
                    if (!destPath.exists()) {
                        boolean success = destPath.mkdir();
                        if (!success) {
                            throw new IOException("Couldn't create directory for VOMS support: "
                                    + destPath.getAbsolutePath());
                        }
                    }
                }
            }
            entry = tin.getNextTarEntry();
            //tarEntry = tin.getNextEntry();
        }
        //tin.close();

        //To handle the symbolic link files
        tin = new TarArchiveInputStream(in);
        entry = tin.getNextTarEntry();
        while (entry != null) {

            if (entry.isSymbolicLink()) {
                File srcPath = new File(untarDir + File.separatorChar + entry.getLinkName());
                File destPath = new File(untarDir + File.separatorChar + entry.getName());
                copyFile(srcPath, destPath);

                //tarEntry = tin.getNextEntry();
            }
            entry = tin.getNextTarEntry();
        }
        tin.close();
    } else {
        throw new IOException("Couldn't find directory: " + untarDir);
    }

}

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;
        }// www  .  java  2s .co  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;
        }/*from www  .j a  v a  2 s  . co 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: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;/*from  w ww.  j av a2  s.  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.facebook.buck.util.unarchive.Untar.java

/** Sets the modification time and the execution bit on a file */
private void setAttributes(ProjectFilesystem filesystem, Path path, TarArchiveEntry entry) throws IOException {
    Path filePath = filesystem.getRootPath().resolve(path);
    File file = filePath.toFile();
    file.setLastModified(entry.getModTime().getTime());
    Set<PosixFilePermission> posixPermissions = MorePosixFilePermissions.fromMode(entry.getMode());
    if (posixPermissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
        // setting posix file permissions on a symlink does not work, so use File API instead
        if (entry.isSymbolicLink()) {
            file.setExecutable(true, true);
        } else {/*from   w  w w  .j  a  va 2 s  . c  o  m*/
            MostFiles.makeExecutable(filePath);
        }
    }
}

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

@Nullable
@Override/*from   w  w w . j  a  va2s  .c  o 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;
            }// www. j  a  v a  2s .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/*  w ww. j ava  2 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);
    }
}

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

@VisibleForTesting
ImmutableSet<Path> extractArchive(Path archiveFile, ProjectFilesystem filesystem, Path filesystemRelativePath,
        Optional<Path> stripPath, ExistingFileMode existingFileMode, PatternsMatcher entriesToExclude,
        boolean writeSymlinksAfterCreatingFiles) throws IOException {

    ImmutableSet.Builder<Path> paths = ImmutableSet.builder();
    HashSet<Path> dirsToTidy = new HashSet<>();
    TreeMap<Path, Long> dirCreationTimes = new TreeMap<>();
    DirectoryCreator creator = new DirectoryCreator(filesystem);

    // On windows, we create hard links instead of symlinks. This is fine, but the
    // destination file may not exist yet, which is an error. So, just hold onto the paths until
    // all files are extracted, and /then/ try to do the links
    Map<Path, Path> windowsSymlinkMap = new HashMap<>();

    try (TarArchiveInputStream archiveStream = getArchiveInputStream(archiveFile)) {
        TarArchiveEntry entry;
        while ((entry = archiveStream.getNextTarEntry()) != null) {
            String entryName = entry.getName();
            if (entriesToExclude.matchesAny(entryName)) {
                continue;
            }//w ww . j  a v  a 2 s .  c  o m
            Path destFile = Paths.get(entryName);
            Path destPath;
            if (stripPath.isPresent()) {
                if (!destFile.startsWith(stripPath.get())) {
                    continue;
                }
                destPath = filesystemRelativePath.resolve(stripPath.get().relativize(destFile)).normalize();
            } else {
                destPath = filesystemRelativePath.resolve(destFile).normalize();
            }

            if (entry.isDirectory()) {
                dirsToTidy.add(destPath);
                mkdirs(creator, destPath);
                dirCreationTimes.put(destPath, entry.getModTime().getTime());
            } else if (entry.isSymbolicLink()) {
                if (writeSymlinksAfterCreatingFiles) {
                    recordSymbolicLinkForWindows(creator, destPath, entry, windowsSymlinkMap);
                } else {
                    writeSymbolicLink(creator, destPath, entry);
                }
                paths.add(destPath);
                setAttributes(filesystem, destPath, entry);
            } else if (entry.isFile()) {
                writeFile(creator, archiveStream, destPath);
                paths.add(destPath);
                setAttributes(filesystem, destPath, entry);
            }
        }

        writeWindowsSymlinks(creator, windowsSymlinkMap);
    } catch (CompressorException e) {
        throw new IOException(String.format("Could not get decompressor for archive at %s", archiveFile), e);
    }

    setDirectoryModificationTimes(filesystem, dirCreationTimes);

    ImmutableSet<Path> filePaths = paths.build();
    if (existingFileMode == ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES) {
        // Clean out directories of files that were not in the archive
        tidyDirectories(filesystem, dirsToTidy, filePaths);
    }
    return filePaths;
}