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.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();/*  w  ww .j av  a  2s .c o  m*/
                } 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:org.apache.ignite.testsuites.IgniteHadoopTestSuite.java

/**
 *  Downloads and extracts an Apache product.
 *
 * @param appName Name of application for log messages.
 * @param homeVariable Pointer to home directory of the component.
 * @param downloadPath Relative download path of tar package.
 * @param destName Local directory name to install component.
 * @throws Exception If failed./*from w w w .  ja v a 2  s  . co m*/
 */
private static void download(String appName, String homeVariable, String downloadPath, String destName)
        throws Exception {
    String homeVal = IgniteSystemProperties.getString(homeVariable);

    if (!F.isEmpty(homeVal) && new File(homeVal).isDirectory()) {
        X.println(homeVariable + " is set to: " + homeVal);

        return;
    }

    List<String> urls = F.asList("http://archive.apache.org/dist/", "http://apache-mirror.rbc.ru/pub/apache/",
            "http://www.eu.apache.org/dist/", "http://www.us.apache.org/dist/");

    String tmpPath = System.getProperty("java.io.tmpdir");

    X.println("tmp: " + tmpPath);

    final File install = new File(tmpPath + File.separatorChar + "__hadoop");

    final File home = new File(install, destName);

    X.println("Setting " + homeVariable + " to " + home.getAbsolutePath());

    System.setProperty(homeVariable, home.getAbsolutePath());

    final File successFile = new File(home, "__success");

    if (home.exists()) {
        if (successFile.exists()) {
            X.println(appName + " distribution already exists.");

            return;
        }

        X.println(appName + " distribution is invalid and it will be deleted.");

        if (!U.delete(home))
            throw new IOException("Failed to delete directory: " + home.getAbsolutePath());
    }

    for (String url : urls) {
        if (!(install.exists() || install.mkdirs()))
            throw new IOException("Failed to create directory: " + install.getAbsolutePath());

        URL u = new URL(url + downloadPath);

        X.println("Attempting to download from: " + u);

        try {
            URLConnection c = u.openConnection();

            c.connect();

            try (TarArchiveInputStream in = new TarArchiveInputStream(
                    new GzipCompressorInputStream(new BufferedInputStream(c.getInputStream(), 32 * 1024)))) {

                TarArchiveEntry entry;

                while ((entry = in.getNextTarEntry()) != null) {
                    File dest = new File(install, entry.getName());

                    if (entry.isDirectory()) {
                        if (!dest.mkdirs())
                            throw new IllegalStateException();
                    } else if (entry.isSymbolicLink()) {
                        // Important: in Hadoop installation there are symlinks, we need to create them:
                        Path theLinkItself = Paths.get(install.getAbsolutePath(), entry.getName());

                        Path linkTarget = Paths.get(entry.getLinkName());

                        Files.createSymbolicLink(theLinkItself, linkTarget);
                    } else {
                        File parent = dest.getParentFile();

                        if (!(parent.exists() || parent.mkdirs()))
                            throw new IllegalStateException();

                        X.print(" [" + dest);

                        try (BufferedOutputStream out = new BufferedOutputStream(
                                new FileOutputStream(dest, false), 128 * 1024)) {
                            U.copy(in, out);

                            out.flush();
                        }

                        Files.setPosixFilePermissions(dest.toPath(), modeToPermissionSet(entry.getMode()));

                        X.println("]");
                    }
                }
            }

            if (successFile.createNewFile())
                return;
        } catch (Exception e) {
            e.printStackTrace();

            U.delete(home);
        }
    }

    throw new IllegalStateException("Failed to install " + appName + ".");
}

From source file:org.codehaus.mojo.unix.deb.DpkgDebTool.java

private static List<UnixFsObject> process(InputStream is) throws IOException {
    TarArchiveInputStream tarInputStream = new TarArchiveInputStream(is);

    List<UnixFsObject> objects = new ArrayList<UnixFsObject>();

    TarArchiveEntry entry = (TarArchiveEntry) tarInputStream.getNextEntry();

    while (entry != null) {
        Option<UnixFileMode> mode = some(UnixFileMode.fromInt(entry.getMode()));
        FileAttributes attributes = new FileAttributes(some(entry.getUserName()), some(entry.getGroupName()),
                mode);//w  w  w.java 2s.  c o m
        RelativePath path = relativePath(entry.getName());
        LocalDateTime lastModified = LocalDateTime.fromDateFields(entry.getModTime());

        UnixFsObject object;

        if (entry.isDirectory()) {
            object = directory(path, lastModified, attributes);
        } else if (entry.isSymbolicLink()) {
            object = symlink(path, lastModified, some(entry.getUserName()), some(entry.getGroupName()),
                    entry.getLinkName());
        } else if (entry.isFile()) {
            object = regularFile(path, lastModified, entry.getSize(), attributes);
        } else {
            throw new IOException("Unsupported link type: name=" + entry.getName());
        }

        objects.add(object);

        entry = (TarArchiveEntry) tarInputStream.getNextEntry();
    }

    return objects;
}

From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java

public void createArchive(final int directoryMode, final int fileModes[]) throws Exception {
    int defaultFileMode = fileModes[0];
    int oneFileMode = fileModes[1];
    int twoFileMode = fileModes[2];

    TarArchiver archiver = getPosixTarArchiver();

    archiver.setDirectoryMode(directoryMode);

    archiver.setFileMode(defaultFileMode);

    archiver.addDirectory(getTestFile("src/main"));
    archiver.setFileMode(oneFileMode);/*from  w  w  w.  ja v a  2s. c  o m*/

    archiver.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt");
    archiver.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt", twoFileMode);
    archiver.setDestFile(getTestFile("target/output/archive.tar"));

    archiver.addSymlink("link_to_test_destinaton", "../test_destination/");

    archiver.createArchive();

    TarArchiveInputStream tis;

    tis = new TarArchiveInputStream(bufferedInputStream(new FileInputStream(archiver.getDestFile())));
    TarArchiveEntry te;

    while ((te = tis.getNextTarEntry()) != null) {
        if (te.isDirectory()) {
            assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", directoryMode,
                    te.getMode() & UnixStat.PERM_MASK);
        } else if (te.isSymbolicLink()) {
            assertEquals("../test_destination/", te.getLinkName());
            assertEquals("link_to_test_destinaton", te.getName());
            assertEquals(0640, te.getMode() & UnixStat.PERM_MASK);
        } else {
            if (te.getName().equals("one.txt")) {
                assertEquals(oneFileMode, te.getMode() & UnixStat.PERM_MASK);
            } else if (te.getName().equals("two.txt")) {
                assertEquals(twoFileMode, te.getMode() & UnixStat.PERM_MASK);
            } else {
                assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", defaultFileMode,
                        te.getMode() & UnixStat.PERM_MASK);
            }

        }
    }
    IOUtil.close(tis);

}

From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java

public void testCreateArchiveWithJiustASymlink() throws Exception {
    TarArchiver archiver = getPosixTarArchiver();

    archiver.setDirectoryMode(0500);/*from   w  w  w  .  ja  va 2  s .  c  om*/

    archiver.setFileMode(0400);

    archiver.setFileMode(0640);

    archiver.setDestFile(getTestFile("target/output/symlinkarchive.tar"));

    archiver.addSymlink("link_to_test_destinaton", "../test_destination/");

    archiver.createArchive();

    TarArchiveInputStream tis;

    tis = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(archiver.getDestFile())));
    TarArchiveEntry te;

    while ((te = tis.getNextTarEntry()) != null) {
        if (te.isDirectory()) {
            assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", 0500,
                    te.getMode() & UnixStat.PERM_MASK);
        } else if (te.isSymbolicLink()) {
            assertEquals("../test_destination/", te.getLinkName());
            assertEquals("link_to_test_destinaton", te.getName());
            assertEquals(0640, te.getMode() & UnixStat.PERM_MASK);
        } else {
            assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", 0400,
                    te.getMode() & UnixStat.PERM_MASK);
        }
    }
    tis.close();

}

From source file:org.codehaus.plexus.archiver.tar.TarSymlinkResource.java

public TarSymlinkResource(TarFile tarFile, TarArchiveEntry entry) {
    super(tarFile, entry);
    symlinkDestination = entry.getLinkName();
}

From source file:org.codehaus.plexus.archiver.tar.TarUnArchiver.java

protected void execute(File sourceFile, File destDirectory) throws ArchiverException {
    TarArchiveInputStream tis = null;/*from www  .j  av a  2  s  .c  o m*/
    try {
        getLogger().info("Expanding: " + sourceFile + " into " + destDirectory);
        TarFile tarFile = new TarFile(sourceFile);
        tis = new TarArchiveInputStream(
                decompress(compression, sourceFile, new BufferedInputStream(new FileInputStream(sourceFile))));
        TarArchiveEntry te;
        while ((te = tis.getNextTarEntry()) != null) {
            TarResource fileInfo = new TarResource(tarFile, te);
            if (isSelected(te.getName(), fileInfo)) {
                final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null;
                extractFile(sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(),
                        te.getMode() != 0 ? te.getMode() : null, symlinkDestination);
            }

        }
        getLogger().debug("expand complete");

    } catch (IOException ioe) {
        throw new ArchiverException("Error while expanding " + sourceFile.getAbsolutePath(), ioe);
    } finally {
        IOUtil.close(tis);
    }
}

From source file:org.eclipse.cdt.arduino.core.internal.board.ArduinoManager.java

public static void downloadAndInstall(String url, String archiveFileName, Path installPath,
        IProgressMonitor monitor) throws IOException {
    Exception error = null;//from   ww  w  .ja va 2  s.c  om
    for (int retries = 3; retries > 0 && !monitor.isCanceled(); --retries) {
        try {
            URL dl = new URL(url);
            Path dlDir = ArduinoPreferences.getArduinoHome().resolve("downloads"); //$NON-NLS-1$
            Files.createDirectories(dlDir);
            Path archivePath = dlDir.resolve(archiveFileName);
            URLConnection conn = dl.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            Files.copy(conn.getInputStream(), archivePath, StandardCopyOption.REPLACE_EXISTING);

            boolean isWin = Platform.getOS().equals(Platform.OS_WIN32);

            // extract
            ArchiveInputStream archiveIn = null;
            try {
                String compressor = null;
                String archiver = null;
                if (archiveFileName.endsWith("tar.bz2")) { //$NON-NLS-1$
                    compressor = CompressorStreamFactory.BZIP2;
                    archiver = ArchiveStreamFactory.TAR;
                } else if (archiveFileName.endsWith(".tar.gz") || archiveFileName.endsWith(".tgz")) { //$NON-NLS-1$ //$NON-NLS-2$
                    compressor = CompressorStreamFactory.GZIP;
                    archiver = ArchiveStreamFactory.TAR;
                } else if (archiveFileName.endsWith(".tar.xz")) { //$NON-NLS-1$
                    compressor = CompressorStreamFactory.XZ;
                    archiver = ArchiveStreamFactory.TAR;
                } else if (archiveFileName.endsWith(".zip")) { //$NON-NLS-1$
                    archiver = ArchiveStreamFactory.ZIP;
                }

                InputStream in = new BufferedInputStream(new FileInputStream(archivePath.toFile()));
                if (compressor != null) {
                    in = new CompressorStreamFactory().createCompressorInputStream(compressor, in);
                }
                archiveIn = new ArchiveStreamFactory().createArchiveInputStream(archiver, in);

                for (ArchiveEntry entry = archiveIn.getNextEntry(); entry != null; entry = archiveIn
                        .getNextEntry()) {
                    if (entry.isDirectory()) {
                        continue;
                    }

                    // Magic file for git tarballs
                    Path path = Paths.get(entry.getName());
                    if (path.endsWith("pax_global_header")) { //$NON-NLS-1$
                        continue;
                    }

                    // Strip the first directory of the path
                    Path entryPath;
                    switch (path.getName(0).toString()) {
                    case "i586":
                    case "i686":
                        // Cheat for Intel
                        entryPath = installPath.resolve(path);
                        break;
                    default:
                        entryPath = installPath.resolve(path.subpath(1, path.getNameCount()));
                    }

                    Files.createDirectories(entryPath.getParent());

                    if (entry instanceof TarArchiveEntry) {
                        TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
                        if (tarEntry.isLink()) {
                            Path linkPath = Paths.get(tarEntry.getLinkName());
                            linkPath = installPath.resolve(linkPath.subpath(1, linkPath.getNameCount()));
                            Files.deleteIfExists(entryPath);
                            Files.createSymbolicLink(entryPath, entryPath.getParent().relativize(linkPath));
                        } else if (tarEntry.isSymbolicLink()) {
                            Path linkPath = Paths.get(tarEntry.getLinkName());
                            Files.deleteIfExists(entryPath);
                            Files.createSymbolicLink(entryPath, linkPath);
                        } else {
                            Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
                        }
                        if (!isWin && !tarEntry.isSymbolicLink()) {
                            int mode = tarEntry.getMode();
                            Files.setPosixFilePermissions(entryPath, toPerms(mode));
                        }
                    } else {
                        Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
                    }
                }
            } finally {
                if (archiveIn != null) {
                    archiveIn.close();
                }
            }
            return;
        } catch (IOException | CompressorException | ArchiveException e) {
            error = e;
            // retry
        }
    }

    // out of retries
    if (error instanceof IOException) {
        throw (IOException) error;
    } else {
        throw new IOException(error);
    }
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java

@Test
public void testSymbolicLinkWithinArchivePreserved() throws Exception {
    createSymbolicLink(new File(archiveRoot, "testSymLink"), Paths.get("dir2/dir3", "test.sh"));
    archiver.createArchive();/* w  w  w . ja  va  2s .  co  m*/
    TarArchiveEntry symLinkEntry = getTarEntries().get("testSymLink");
    assertTrue(symLinkEntry.isSymbolicLink());
    assertEquals("dir2/dir3/test.sh", symLinkEntry.getLinkName());
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java

@Test
public void testRelativeSymbolicLinkWithinArchivePreserved() throws Exception {
    createSymbolicLink(new File(archiveRoot, "dir2/testSymLink"), Paths.get("../", "test.sh"));
    archiver.createArchive();/*from   ww  w .j  a  va2  s . c  o  m*/
    TarArchiveEntry symLinkEntry = getTarEntries().get("dir2/testSymLink");
    assertTrue(symLinkEntry.isSymbolicLink());
    assertEquals("../test.sh", symLinkEntry.getLinkName());
}