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:org.robovm.gradle.tasks.AbstractRoboVMTask.java

private static void extractTarGz(File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;/*from   w  w  w .j  a v  a2  s.c  om*/
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            f.setLastModified(entry.getLastModifiedDate().getTime());
            if (entry instanceof TarArchiveEntry) {
                int mode = ((TarArchiveEntry) entry).getMode();
                if ((mode & 00100) > 0) {
                    // Preserve execute permissions
                    f.setExecutable(true, (mode & 00001) == 0);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.robovm.idea.RoboVmPlugin.java

private static void extractArchive(String archive, File dest) {
    archive = "/" + archive;
    TarArchiveInputStream in = null;/*from  w ww.j  a v a  2 s . c om*/
    boolean isSnapshot = Version.getVersion().toLowerCase().contains("snapshot");
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(RoboVmPlugin.class.getResourceAsStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(dest, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                if (!isSnapshot && f.exists()) {
                    continue;
                }
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
        }
        logInfo(null, "Installed RoboVM SDK %s to %s", Version.getVersion(), dest.getAbsolutePath());

        // make all files in bin executable
        for (File file : new File(getSdkHome(), "bin").listFiles()) {
            file.setExecutable(true);
        }
    } catch (Throwable t) {
        logError(null, "Couldn't extract SDK to %s", dest.getAbsolutePath());
        throw new RuntimeException("Couldn't extract SDK to " + dest.getAbsolutePath(), t);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.robovm.maven.resolver.Archiver.java

public static void unarchive(Logger logger, File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;//from   w  w w  . ja  va  2s  . co m
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                logger.debug(f.getAbsolutePath());
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            f.setLastModified(entry.getLastModifiedDate().getTime());
            if (entry instanceof TarArchiveEntry) {
                int mode = ((TarArchiveEntry) entry).getMode();
                if ((mode & 00100) > 0) {
                    // Preserve execute permissions
                    f.setExecutable(true, (mode & 00001) == 0);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.robovm.templater.Templater.java

private void extractArchive(File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;/*from   w  w w .  j a va 2 s  .c o m*/
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, substitutePlaceholdersInFileName(entry.getName()));
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
                substitutePlaceholdersInFile(f);
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.silverpeas.core.util.ZipUtil.java

/**
 * Extract the content of an archive into a directory.
 *
 * @param source the archive. Must be non null.
 * @param dest the destination directory. Must be non null.
 *//*from  w  w  w  .jav  a2 s.c  o m*/
public static void extract(File source, File dest) {
    Objects.requireNonNull(source);
    Objects.requireNonNull(dest);
    try (final InputStream in = new BufferedInputStream(new FileInputStream(source));
            final ArchiveInputStream archiveStream = openArchive(source.getName(), in)) {
        ArchiveEntry archiveEntry;
        while ((archiveEntry = archiveStream.getNextEntry()) != null) {
            File currentFile = new File(dest, archiveEntry.getName());
            FileUtil.validateFilename(currentFile.getCanonicalPath(), dest.getCanonicalPath());
            createPath(archiveStream, archiveEntry, currentFile);
        }
    } catch (IOException ioe) {
        SilverLogger.getLogger(ZipUtil.class).error("Cannot extract archive " + source.getPath(), ioe);
    }
}

From source file:org.slc.sli.ingestion.landingzone.validation.ZipFileValidator.java

private static boolean isDirectory(ArchiveEntry zipEntry) {

    if (zipEntry.isDirectory()) {
        return true;
    }//from   w w  w  . ja  v  a  2 s.c  om

    // UN: This check is to ensure that any zipping utility which does not pack a directory
    // entry
    // is verified by checking for a filename with '/'. Example: Windows Zipping Tool.
    if (zipEntry.getName().contains("/")) {
        return true;
    }

    return false;
}

From source file:org.slc.sli.ingestion.landingzone.ZipFileUtilTest.java

private void assertTargetMatchesZip(File targetDir, File zipFile) throws IOException {
    InputStream zipStream = null;
    ZipArchiveInputStream zipFileStrm = null;
    try {//ww  w.  j a v  a  2s.  co  m
        zipStream = new BufferedInputStream(new FileInputStream(zipFile));
        zipFileStrm = new ZipArchiveInputStream(zipStream);

        ArchiveEntry entry;
        ArrayList<String> zipFileSet = new ArrayList<String>();
        while ((entry = zipFileStrm.getNextEntry()) != null) {
            zipFileSet.add(File.separator + entry.getName().replace('/', File.separatorChar));
        }

        ArrayList<String> extractedFileSet = new ArrayList<String>();
        addExtractedFiles(targetDir, File.separator, extractedFileSet);
        Collections.sort(zipFileSet);
        Collections.sort(extractedFileSet);

        Assert.assertEquals(extractedFileSet, zipFileSet);
    } finally {
        IOUtils.closeQuietly(zipStream);
        IOUtils.closeQuietly(zipFileStrm);
    }
}

From source file:org.sonatype.nexus.repository.r.internal.RDescriptionUtils.java

private static Map<String, String> extractMetadataFromArchive(final String archiveType, final InputStream is) {
    final ArchiveStreamFactory archiveFactory = new ArchiveStreamFactory();
    try (ArchiveInputStream ais = archiveFactory.createArchiveInputStream(archiveType, is)) {
        ArchiveEntry entry = ais.getNextEntry();
        while (entry != null) {
            if (!entry.isDirectory() && DESCRIPTION_FILE_PATTERN.matcher(entry.getName()).matches()) {
                return parseDescriptionFile(ais);
            }/*  ww  w .  j a v a 2s .  c  o m*/
            entry = ais.getNextEntry();
        }
    } catch (ArchiveException | IOException e) {
        throw new RException(null, e);
    }
    throw new IllegalStateException("No metadata file found");
}

From source file:org.sourcepit.tools.shared.resources.internal.harness.SharedResourcesUtils.java

private static void importArchive(ZipArchiveInputStream zipIn, String archiveEntry, File outDir,
        boolean keepArchivePaths, String encoding, IFilteredCopier copier, IFilterStrategy strategy)
        throws IOException {
    boolean found = false;

    ArchiveEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        final String entryName = entry.getName();

        if (archiveEntry == null || entryName.startsWith(archiveEntry + "/")
                || entryName.equals(archiveEntry)) {
            found = true;//from   ww  w .j a va  2  s. c o  m

            boolean isDir = entry.isDirectory();

            final String fileName;
            if (archiveEntry == null || keepArchivePaths) {
                fileName = entryName;
            } else {
                if (entryName.startsWith(archiveEntry + "/")) {
                    fileName = entryName.substring(archiveEntry.length() + 1);
                } else if (!isDir && entryName.equals(archiveEntry)) {
                    fileName = new File(entryName).getName();
                } else {
                    throw new IllegalStateException();
                }
            }

            final File file = new File(outDir, fileName);
            if (entry.isDirectory()) {
                file.mkdir();
            } else {
                file.createNewFile();
                OutputStream out = new FileOutputStream(file);
                try {
                    if (copier != null && strategy.filter(fileName)) {
                        copy(zipIn, out, encoding, copier, file);
                    } else {
                        IOUtils.copy(zipIn, out);
                    }
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
        }
        entry = zipIn.getNextEntry();
    }

    if (!found) {
        throw new FileNotFoundException(archiveEntry);
    }
}

From source file:org.springframework.cloud.stream.app.tensorflow.util.ModelExtractor.java

/**
 * Traverses the Archive to find either an entry that matches the modelFileNameInArchive name (if not empty) or
 * and entry that ends in .pb if the modelFileNameInArchive is empty.
 *
 * @param modelFileNameInArchive Optional name of the archive entry that represents the frozen model file. If empty
 *                               the archive will be searched for the first entry that ends in .pb
 * @param archive Archive stream to be traversed
 * @return/*from  w  w  w . j ava2 s.  c  o  m*/
 * @throws IOException
 */
private byte[] findInArchiveStream(String modelFileNameInArchive, ArchiveInputStream archive)
        throws IOException {
    ArchiveEntry entry;
    while ((entry = archive.getNextEntry()) != null) {
        //System.out.println(entry.getName() + " : " + entry.isDirectory());

        if (archive.canReadEntryData(entry) && !entry.isDirectory()) {
            if ((StringUtils.hasText(modelFileNameInArchive)
                    && entry.getName().endsWith(modelFileNameInArchive))
                    || (!StringUtils.hasText(modelFileNameInArchive)
                            && entry.getName().endsWith(this.frozenGraphFileExtension))) {
                return StreamUtils.copyToByteArray(archive);
            }
        }
    }
    throw new IllegalArgumentException("No model is found in the archive");
}