Example usage for org.apache.commons.compress.archivers ArchiveEntry getName

List of usage examples for org.apache.commons.compress.archivers ArchiveEntry getName

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers ArchiveEntry getName.

Prototype

public String getName();

Source Link

Document

The name of the entry in the archive.

Usage

From source file:de.dentrassi.eclipse.rpm.editor.EditorImpl.java

private RpmInformation load(final InputStream stream) {
    try (RpmInputStream in = new RpmInputStream(stream)) {
        final RpmLead lead = in.getLead();

        final InputHeader<RpmTag> header = in.getPayloadHeader();
        final InputHeader<RpmSignatureTag> sigHeader = in.getSignatureHeader();

        final CpioArchiveInputStream cpio = in.getCpioStream();

        ArchiveEntry entry;

        final List<FileEntry> files = new ArrayList<>();
        while ((entry = cpio.getNextEntry()) != null) {
            final FileEntry fe = new FileEntry(entry.getName(), entry.getSize(),
                    entry.getLastModifiedDate().toInstant());
            files.add(fe);/* w w w .ja  va2 s.  c  om*/
        }

        return new RpmInformation(lead, header, sigHeader, files);
    } catch (final IOException e) {
        return null;
    }
}

From source file:ezbake.deployer.publishers.artifact.ArtifactContentsPublisher.java

protected boolean doesResourceAlreadyExistInArtifact(java.nio.file.Path artifactPath,
        java.nio.file.Path artifactBasePath, byte[] artifact) throws DeploymentException {

    try (org.apache.commons.compress.archivers.tar.TarArchiveInputStream tais = new org.apache.commons.compress.archivers.tar.TarArchiveInputStream(
            new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(
                    new java.io.ByteArrayInputStream(artifact)))) {

        org.apache.commons.compress.archivers.ArchiveEntry nextEntry;
        while ((nextEntry = tais.getNextEntry()) != null) {

            if (nextEntry.isDirectory())
                continue;

            java.nio.file.Path entryPath = java.nio.file.Paths.get(nextEntry.getName());
            if (!entryPath.startsWith(artifactBasePath))
                continue;

            if (entryPath.compareTo(artifactPath) == 0) {
                return true;
            }/*from w ww .  j av a 2 s  .  co  m*/
        }
    } catch (java.io.IOException e) {
        throw new DeploymentException(e.getMessage());
    }
    return false;
}

From source file:net.codestory.simplelenium.driver.Downloader.java

protected void untar(File tar, File toDir) throws IOException {
    try (FileInputStream fin = new FileInputStream(tar);
            BufferedInputStream bin = new BufferedInputStream(fin);
            TarArchiveInputStream tarInput = new TarArchiveInputStream(bin)) {
        ArchiveEntry entry;
        while (null != (entry = tarInput.getNextTarEntry())) {
            if (entry.isDirectory()) {
                continue;
            }/*  ww w  . j a  va 2  s .  c o  m*/

            File to = new File(toDir, entry.getName());

            File parent = to.getParentFile();
            if (!parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("Unable to create folder " + parent);
                }
            }

            Files.copy(tarInput, to.toPath(), REPLACE_EXISTING);
        }
    }
}

From source file:com.fizzed.stork.deploy.Archive.java

public Path unpack(Path unpackDir) throws IOException {
    Files.createDirectories(unpackDir);

    log.info("Unpacking {} to {}", file, unpackDir);

    // we need to know the top-level dir(s) created by unpack
    final Set<Path> firstLevelPaths = new LinkedHashSet<>();
    final AtomicInteger count = new AtomicInteger();

    try (ArchiveInputStream ais = newArchiveInputStream(file)) {
        ArchiveEntry entry;
        while ((entry = ais.getNextEntry()) != null) {
            try {
                Path entryFile = Paths.get(entry.getName());
                Path resolvedFile = unpackDir.resolve(entryFile);

                firstLevelPaths.add(entryFile.getName(0));

                log.debug("{}", resolvedFile);

                if (entry.isDirectory()) {
                    Files.createDirectories(resolvedFile);
                } else {
                    unpackEntry(ais, resolvedFile);
                    count.incrementAndGet();
                }//  w  w w.  jav a2s  . c om
            } catch (IOException | IllegalStateException | IllegalArgumentException e) {
                log.error("", e);
                throw new RuntimeException(e);
            }
        }
    }

    if (firstLevelPaths.size() != 1) {
        throw new IOException("Only archives with a single top-level directory are supported!");
    }

    Path assemblyDir = unpackDir.resolve(firstLevelPaths.iterator().next());

    log.info("Unpacked {} files to {}", count, assemblyDir);

    return assemblyDir;
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.datasets.internal.actions.Explode.java

private void extract(ActionDescription aAction, Path aArchive, ArchiveInputStream aAStream, Path aTarget)
        throws IOException {
    // We always extract archives into a subfolder. Figure out the name of the folder.
    String base = getBase(aArchive.getFileName().toString());

    Map<String, Object> cfg = aAction.getConfiguration();
    int strip = cfg.containsKey("strip") ? (int) cfg.get("strip") : 0;

    AntFileFilter filter = new AntFileFilter(coerceToList(cfg.get("includes")),
            coerceToList(cfg.get("excludes")));

    ArchiveEntry entry = null;
    while ((entry = aAStream.getNextEntry()) != null) {
        String name = stripLeadingFolders(entry.getName(), strip);

        if (name == null) {
            // Stripped to null - nothing left to extract - continue;
            continue;
        }//from  w  w  w. j av a 2s.  co  m

        if (filter.accept(name)) {
            Path out = aTarget.resolve(base).resolve(name);
            if (entry.isDirectory()) {
                Files.createDirectories(out);
            } else {
                Files.createDirectories(out.getParent());
                Files.copy(aAStream, out);
            }
        }
    }
}

From source file:com.github.jarlakxen.embedphantomjs.PhantomJSReference.java

private void downloadPhantomJS(File binaryFile) throws IOException {
    Properties properties = new Properties();
    properties.load(this.getClass().getClassLoader().getResourceAsStream(PHANTOMJS_DATA_FILE));

    String name = properties.getProperty(this.getVersion().getDescription() + "." + this.getHostOs() + ".name");

    String architecture = this.getArchitecture().indexOf("64") >= 0 ? "x86_64" : "i686";

    LOGGER.debug("System Data: Arch [" + architecture + "] - OS [" + this.getHostOs() + "]");

    if (this.getHostOs().equals("linux")) {
        name = String.format(name, architecture);
    }/*from   w  w w .j a  va 2  s  .  com*/

    // Download PhantomJS
    URL downloadPath = new URL(this.getDownloadUrl() + name);
    File phantomJsCompressedFile = new File(System.getProperty("java.io.tmpdir") + "/" + name);

    LOGGER.info("Downloading " + downloadPath.getPath() + " ...");

    FileUtils.copyURLToFile(downloadPath, phantomJsCompressedFile);

    ArchiveInputStream archiveInputStream = null;

    if (phantomJsCompressedFile.getName().endsWith(".zip")) {

        archiveInputStream = new ZipArchiveInputStream(new FileInputStream(phantomJsCompressedFile));

    } else if (phantomJsCompressedFile.getName().endsWith(".bz2")) {

        archiveInputStream = new TarArchiveInputStream(
                new BZip2CompressorInputStream(new FileInputStream(phantomJsCompressedFile)));

    } else if (phantomJsCompressedFile.getName().endsWith(".gz")) {

        archiveInputStream = new TarArchiveInputStream(
                new GzipCompressorInputStream(new FileInputStream(phantomJsCompressedFile)));

    }

    ArchiveEntry entry;
    while ((entry = archiveInputStream.getNextEntry()) != null) {
        if (entry.getName().endsWith(PHANTOMJS_DOWNLOAD_BINARY_PATH)
                || entry.getName().toLowerCase().endsWith("phantomjs.exe")) {

            // Create target folder
            new File(this.getTargetInstallationFolder() + "/" + this.getVersion().getDescription()).mkdirs();

            FileUtils.forceMkdir(new File(binaryFile.getParent()));

            if (!binaryFile.exists()) {
                binaryFile.createNewFile();
            }

            binaryFile.setExecutable(true);
            binaryFile.setReadable(true);

            // Untar the binary file
            FileOutputStream outputBinary = new FileOutputStream(binaryFile);

            LOGGER.info("Un-compress download to " + downloadPath.getPath() + " ...");
            IOUtils.copy(archiveInputStream, outputBinary);

            outputBinary.close();
        }
    }

    archiveInputStream.close();
}

From source file:com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.RegistryBackedArchiveProfileBuilder.java

public List<Profile> build(DeploymentConfiguration deploymentConfiguration, String baseOutputPath,
        SpinnakerArtifact artifact, String archiveName) {
    String version = artifactService.getArtifactVersion(deploymentConfiguration.getName(), artifact);
    String archiveObjectName = ProfileRegistry.profilePath(artifact.getName(), version,
            archiveName + ".tar.gz");

    InputStream is;//from ww w .  j  av  a 2 s  . c  o m
    try {
        is = profileRegistry.getObjectContents(archiveObjectName);
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL,
                "Error retrieving contents of archive " + archiveObjectName, e);
    }

    TarArchiveInputStream tis;
    try {
        tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    } catch (ArchiveException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to unpack tar archive", e);
    }

    try {
        List<Profile> result = new ArrayList<>();

        ArchiveEntry profileEntry = tis.getNextEntry();
        while (profileEntry != null) {
            if (profileEntry.isDirectory()) {
                profileEntry = tis.getNextEntry();
                continue;
            }

            String entryName = profileEntry.getName();
            String profileName = String.join("/", artifact.getName(), archiveName, entryName);
            String outputPath = Paths.get(baseOutputPath, archiveName, entryName).toString();
            String contents = IOUtils.toString(tis);

            result.add((new ProfileFactory() {
                @Override
                protected void setProfile(Profile profile, DeploymentConfiguration deploymentConfiguration,
                        SpinnakerRuntimeSettings endpoints) {
                    profile.setContents(profile.getBaseContents());
                }

                @Override
                protected Profile getBaseProfile(String name, String version, String outputFile) {
                    return new Profile(name, version, outputFile, contents);
                }

                @Override
                protected boolean showEditWarning() {
                    return false;
                }

                @Override
                protected ArtifactService getArtifactService() {
                    return artifactService;
                }

                @Override
                public SpinnakerArtifact getArtifact() {
                    return artifact;
                }

                @Override
                protected String commentPrefix() {
                    return null;
                }
            }).getProfile(profileName, outputPath, deploymentConfiguration, null));

            profileEntry = tis.getNextEntry();
        }

        return result;
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to read profile entry", e);
    }

}

From source file:com.github.nethad.clustermeister.integration.JPPFTestNode.java

private void untar(File tarFile) throws IOException {
    logger.info("Untar {}.", tarFile.getAbsolutePath());
    TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new FileInputStream(tarFile));
    try {/*ww w  .  jav a  2 s. c  o  m*/
        ArchiveEntry tarEntry = tarArchiveInputStream.getNextEntry();
        while (tarEntry != null) {
            File destPath = new File(libDir, tarEntry.getName());
            logger.info("Unpacking {}.", destPath.getAbsoluteFile());
            if (!tarEntry.isDirectory()) {
                FileOutputStream fout = new FileOutputStream(destPath);
                final byte[] buffer = new byte[8192];
                int n = 0;
                while (-1 != (n = tarArchiveInputStream.read(buffer))) {
                    fout.write(buffer, 0, n);
                }
                fout.close();

            } else {
                destPath.mkdir();
            }
            tarEntry = tarArchiveInputStream.getNextEntry();
        }
    } finally {
        tarArchiveInputStream.close();
    }
}

From source file:br.com.thiaguten.archive.AbstractArchive.java

/**
 * Generic decompress implemetation//from  w w  w  .  java  2 s.co m
 */
@Override
public Path decompress(Path path) throws IOException {
    Path decompressDir = removeExtension(path);

    logger.debug("reading archive file " + path);

    try (ArchiveInputStream archiveInputStream = createArchiveInputStream(
            new BufferedInputStream(newInputStream(path)))) {

        // creates a new decompress folder to not override if already exists
        // if you do not want this behavior, just comment this line
        decompressDir = createFile(ArchiveAction.DECOMPRESS, decompressDir.getParent(), decompressDir);

        createDirectories(decompressDir);

        logger.debug("creating the decompress destination directory " + decompressDir);

        ArchiveEntry entry;
        while ((entry = archiveInputStream.getNextEntry()) != null) {
            if (archiveInputStream.canReadEntryData(entry)) {

                final String entryName = entry.getName();
                final Path target = Paths.get(decompressDir.toString(), entryName);
                final Path parent = target.getParent();

                if (parent != null && !exists(parent)) {
                    createDirectories(parent);
                }

                logger.debug("reading compressed path " + entryName);

                if (!entry.isDirectory()) {
                    try (OutputStream outputStream = new BufferedOutputStream(newOutputStream(target))) {

                        logger.debug("writting compressed " + entryName + " file in the decompress directory");

                        //                            byte[] content = new byte[(int) entry.getSize()];
                        //                            outputStream.write(content);
                        IOUtils.copy(archiveInputStream, outputStream);
                    }
                }
            }
        }

        logger.debug("finishing the decompress in the directory: " + decompressDir);

    }

    return decompressDir;
}

From source file:consulo.cold.runner.execute.target.artifacts.Generator.java

public void buildDistributionInArchive(String distZip, @Nullable String jdkArchivePath, String path,
        String archiveOutType) throws Exception {
    myListener.info("Build: " + path);

    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    final File fileZip = new File(myDistPath, distZip);

    final List<String> executables = Arrays.asList(ourExecutable);

    try (OutputStream pathStream = createOutputStream(archiveOutType, path)) {
        ArchiveOutputStream archiveOutputStream = factory.createArchiveOutputStream(archiveOutType, pathStream);
        if (archiveOutputStream instanceof TarArchiveOutputStream) {
            ((TarArchiveOutputStream) archiveOutputStream).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        }/*from   w  ww .  j  a v a2s .  c  o  m*/

        // move Consulo to archive, and change permissions
        try (InputStream is = new FileInputStream(fileZip)) {
            try (ArchiveInputStream ais = factory.createArchiveInputStream(ArchiveStreamFactory.ZIP, is)) {
                ArchiveEntry tempEntry = ais.getNextEntry();
                while (tempEntry != null) {
                    final ArchiveEntryWrapper newEntry = createEntry(archiveOutType, tempEntry.getName(),
                            tempEntry);

                    newEntry.setMode(extractMode(tempEntry));
                    newEntry.setTime(tempEntry.getLastModifiedDate().getTime());

                    if (executables.contains(tempEntry.getName())) {
                        newEntry.setMode(0b111_101_101);
                    }

                    copyEntry(archiveOutputStream, ais, tempEntry, newEntry);

                    tempEntry = ais.getNextEntry();
                }
            }
        }

        boolean mac = distZip.contains("mac");

        // jdk check
        if (jdkArchivePath != null) {
            try (InputStream is = new FileInputStream(jdkArchivePath)) {
                try (GzipCompressorInputStream gz = new GzipCompressorInputStream(is)) {
                    try (ArchiveInputStream ais = factory.createArchiveInputStream(ArchiveStreamFactory.TAR,
                            gz)) {
                        ArchiveEntry tempEntry = ais.getNextEntry();
                        while (tempEntry != null) {
                            final String name = tempEntry.getName();

                            // is our path
                            if (!mac && name.startsWith("jre/")) {
                                final ArchiveEntryWrapper jdkEntry = createEntry(archiveOutType,
                                        "Consulo/platform/buildSNAPSHOT/" + name, tempEntry);
                                jdkEntry.setMode(extractMode(tempEntry));
                                jdkEntry.setTime(tempEntry.getLastModifiedDate().getTime());

                                copyEntry(archiveOutputStream, ais, tempEntry, jdkEntry);
                            } else if (mac && name.startsWith("jdk")) {
                                boolean needAddToArchive = true;
                                for (String prefix : ourMacSkipJdkList) {
                                    if (name.startsWith(prefix)) {
                                        needAddToArchive = false;
                                    }
                                }

                                if (needAddToArchive) {
                                    final ArchiveEntryWrapper jdkEntry = createEntry(archiveOutType,
                                            "Consulo.app/Contents/platform/buildSNAPSHOT/jre/" + name,
                                            tempEntry);
                                    jdkEntry.setMode(extractMode(tempEntry));
                                    jdkEntry.setTime(tempEntry.getLastModifiedDate().getTime());

                                    copyEntry(archiveOutputStream, ais, tempEntry, jdkEntry);
                                }
                            }

                            tempEntry = ais.getNextEntry();
                        }
                    }
                }
            }
        }

        archiveOutputStream.finish();
    }
}