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:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java

@Test
public void testSymbolicLinkAbsoluteTargetConvertedToRelative() throws Exception {
    // use absolute path as symlink target
    Path absoluteLinkTarget = new File(archiveRoot, "dir2/dir3/test.sh").getAbsoluteFile().toPath();
    createSymbolicLink(new File(archiveRoot, "dir2/testSymLink"), absoluteLinkTarget);
    archiver.createArchive();//from  w  ww . j  a  v a  2s  .c o m
    TarArchiveEntry symLinkEntry = getTarEntries().get("dir2/testSymLink");
    assertTrue(symLinkEntry.isSymbolicLink());
    final String relativeLinkTarget = "dir3/test.sh";
    assertEquals(relativeLinkTarget, symLinkEntry.getLinkName());
}

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

@Test
public void testSymbolicLinkOutsideArchiveInlined() throws Exception {
    File linkTargetFile = tempFolder.newFile("linkTargetOutsideArchiveRoot");
    FileUtils.fileWrite(linkTargetFile, "testContent");
    createSymbolicLink(new File(archiveRoot, "testSymLink"), linkTargetFile.toPath());
    archiver.createArchive();// ww w  . ja v  a  2  s.co  m
    TarArchiveEntry inlinedSymLinkEntry = getTarEntries().get("testSymLink");
    assertFalse(inlinedSymLinkEntry.isSymbolicLink());
    assertTrue(inlinedSymLinkEntry.isFile());
    String content = new String(getTarEntry("testSymLink"), "UTF-8");
    assertEquals("testContent", content);
}

From source file:org.moe.cli.utils.ArchiveUtils.java

public static void untarArchive(ArchiveInputStream archStrim, File destination) throws IOException {
    TarArchiveEntry entry;

    while ((entry = (TarArchiveEntry) archStrim.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            String dest = entry.getName();
            File destFolder = new File(destination, dest);
            if (!destFolder.exists()) {
                destFolder.mkdirs();//from w w w. ja v a  2 s.  com
            }
        } else {
            int count;
            byte[] data = new byte[2048];
            File d = new File(destination, entry.getName());

            if (!d.getParentFile().exists()) {
                d.getParentFile().mkdirs();
            }

            if (entry.isSymbolicLink()) {
                String link = entry.getLinkName();

                String entryName = entry.getName();
                int parentIdx = entryName.lastIndexOf("/");

                String newLink = entryName.substring(0, parentIdx) + "/" + link;
                File destFile = new File(destination, newLink);
                File linkFile = new File(destination, entryName);

                Files.createSymbolicLink(Paths.get(linkFile.getPath()), Paths.get(destFile.getPath()));

            } else {
                FileOutputStream fos = new FileOutputStream(d);
                BufferedOutputStream dest = new BufferedOutputStream(fos, 2048);
                while ((count = archStrim.read(data, 0, 2048)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }
        }
    }
}

From source file:org.phoenicis.tools.archive.Tar.java

/**
 * Uncompress a tar/*from   www  .  ja va  2s .  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()),
                                fileUtilities.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:org.xenmaster.setup.debian.Bootstrapper.java

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

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

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

    lastChecked = System.currentTimeMillis();

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

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

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

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

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

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

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

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

    return true;
}