Example usage for java.util.zip ZipFile entries

List of usage examples for java.util.zip ZipFile entries

Introduction

In this page you can find the example usage for java.util.zip ZipFile entries.

Prototype

public Enumeration<? extends ZipEntry> entries() 

Source Link

Document

Returns an enumeration of the ZIP file entries.

Usage

From source file:com.isomorphic.maven.util.ArchiveUtils.java

/**
 * Unzips the <code>source</code> file to the <code>target</code> directory.
 * /*from www .  j  a v  a  2  s  .co  m*/
 * @param source The file to be unzipped
 * @param target The directory to which the source file should be unzipped
 * @throws IOException
 */
public static void unzip(File source, File target) throws IOException {

    ZipFile zip = new ZipFile(source);
    Enumeration<? extends ZipEntry> entries = zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File file = new File(target, entry.getName());
        FileUtils.copyInputStreamToFile(zip.getInputStream(entry), file);
    }
}

From source file:com.mc.printer.model.utils.ZipHelper.java

/**
 * jar????/*from  w  w w . jav  a  2s .  c  om*/
 *
 * @param jar ????jar
 * @param subDir jar???????
 * @param loc ????
 * @param force ????
 * @return
 */
public static boolean unZip(String jar, String subDir, String loc, boolean force) {
    try {
        File base = new File(loc);
        if (!base.exists()) {
            base.mkdirs();
        }

        ZipFile zip = new ZipFile(new File(jar));
        Enumeration<? extends ZipEntry> entrys = zip.entries();
        while (entrys.hasMoreElements()) {
            ZipEntry entry = entrys.nextElement();
            String name = entry.getName();
            if (!name.startsWith(subDir)) {
                continue;
            }
            //subDir
            name = name.replace(subDir, "").trim();
            if (name.length() < 2) {
                log.debug(name + "  < 2");
                continue;
            }
            if (entry.isDirectory()) {
                File dir = new File(base, name);
                if (!dir.exists()) {
                    dir.mkdirs();
                    log.debug("create directory");
                } else {
                    log.debug("the directory is existing.");
                }
                log.debug(name + " is a directory");
            } else {
                File file = new File(base, name);
                if (file.exists() && force) {
                    file.delete();
                }
                if (!file.exists()) {
                    InputStream in = zip.getInputStream(entry);
                    FileUtils.copyInputStreamToFile(in, file);
                    log.debug("create file.");
                    in.close();
                } else {
                    log.debug("the file is existing");
                }
                log.debug(name + " is not a directory");
            }
        }
        zip.close();
    } catch (ZipException ex) {
        ex.printStackTrace();
        log.error("file unzip failed.", ex);
        return false;
    } catch (IOException ex) {
        ex.printStackTrace();
        log.error("file IO failed", ex);
        return false;
    }
    return true;
}

From source file:com.acciente.commons.loader.ClassFinder.java

private static Set findInJar(File oJarFile, String sPackageName, Pattern oClassNamePattern, Set oClassNameSet)
        throws IOException {
    ZipFile oZipFile;
    Enumeration oZipFileEntries;/* w  w  w .j ava 2 s. c o m*/

    oZipFile = new ZipFile(oJarFile);
    oZipFileEntries = oZipFile.entries();

    while (oZipFileEntries.hasMoreElements()) {
        ZipEntry oZipFileEntry = (ZipEntry) oZipFileEntries.nextElement();
        String sClassname = getFQClassname(oZipFileEntry);

        if (!oZipFileEntry.isDirectory() && sClassname.startsWith(sPackageName)) {
            if (oClassNamePattern.matcher(sClassname).matches()) {
                oClassNameSet.add(sClassname);
            }
        }
    }

    return oClassNameSet;
}

From source file:com.opengamma.util.ZipUtils.java

/**
 * Unzips a ZIP archive./*from w  w  w.j  a v a2 s .co m*/
 * 
 * @param zipFile  the archive file, not null
 * @param outputDir  the output directory, not null
 */
public static void unzipArchive(final ZipFile zipFile, final File outputDir) {
    ArgumentChecker.notNull(zipFile, "zipFile");
    ArgumentChecker.notNull(outputDir, "outputDir");

    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                FileUtils.forceMkdir(new File(outputDir, entry.getName()));
                continue;
            }
            File entryDestination = new File(outputDir, entry.getName());
            entryDestination.getParentFile().mkdirs();
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        zipFile.close();
    } catch (IOException ex) {
        throw new OpenGammaRuntimeException(
                "Error while extracting file: " + zipFile.getName() + " to: " + outputDir, ex);
    }
}

From source file:com.limegroup.gnutella.gui.LanguageUtils.java

/**
 * Returns the languages as found from the classpath in messages.jar
 *//*from   ww w.  ja v a  2 s  .  com*/
static void addLocalesFromJar(List<Locale> locales, File jar) {
    ZipFile zip = null;
    try {
        zip = new ZipFile(jar);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            String name = entries.nextElement().getName();
            if (!name.startsWith(BUNDLE_PREFIX) || !name.endsWith(BUNDLE_POSTFIX) || name.indexOf("$") != -1) {
                continue;
            }

            String iso = name.substring(BUNDLE_PREFIX.length(), name.length() - BUNDLE_POSTFIX.length());
            List<String> tokens = new ArrayList<String>(Arrays.asList(iso.split("_", 3)));
            if (tokens.size() < 1) {
                continue;
            }
            while (tokens.size() < 3) {
                tokens.add("");
            }

            Locale locale = new Locale(tokens.get(0), tokens.get(1), tokens.get(2));
            locales.add(locale);
        }
    } catch (IOException e) {
        LOG.warn("Could not determine locales", e);
    } finally {
        if (zip != null) {
            try {
                zip.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:org.codice.ddf.admin.application.service.impl.ApplicationFileInstaller.java

/**
 * Extracts all files/folders from the provided zip file to our location defined in
 * REPO_LOCATION./* www  . j  a  v  a  2  s  .  c o m*/
 *
 * @param appZip
 *            the ZipFile to extract.
 * @return the detected feature file location.
 */
private static String installToRepo(ZipFile appZip) {
    String featureLocation = null;
    Enumeration<?> entries = appZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry curEntry = (ZipEntry) entries.nextElement();
        if (!curEntry.isDirectory() && !curEntry.getName().startsWith("META-INF")) {
            try {
                InputStream is = appZip.getInputStream(curEntry);
                String outputName = curEntry.getName().substring("repository/".length());
                LOGGER.info("Writing out {}", curEntry.getName());
                org.apache.aries.util.io.IOUtils.writeOut(new File(REPO_LOCATION), outputName, is);
                if (isFeatureFile(curEntry)) {
                    featureLocation = outputName;
                }
            } catch (IOException e) {
                LOGGER.warn("Could not write out file.", e);
            }
        }
    }
    return featureLocation;
}

From source file:com.vmware.admiral.closures.util.ClosureUtils.java

private static void buildTarData(URL dirURL, String folderNameFilter, OutputStream outputStream)
        throws IOException {
    final JarURLConnection jarConnection = (JarURLConnection) dirURL.openConnection();
    final ZipFile jar = jarConnection.getJarFile();
    final Enumeration<? extends ZipEntry> entries = jar.entries();

    try (TarArchiveOutputStream tarArchiveOutputStream = buildTarStream(outputStream)) {
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final String name = entry.getName();
            if (!name.startsWith(folderNameFilter)) {
                // entry in wrong subdir -- don't copy
                continue;
            }//  w  w  w.  j  ava2s .c  o  m
            TarArchiveEntry tarEntry = new TarArchiveEntry(entry.getName().replaceAll(folderNameFilter, ""));
            try (InputStream is = jar.getInputStream(entry)) {
                putTarEntry(tarArchiveOutputStream, tarEntry, is, entry.getSize());
            }
        }

        tarArchiveOutputStream.flush();
        tarArchiveOutputStream.close();
    }
}

From source file:net.sourceforge.pmd.util.FileUtil.java

private static List<DataSource> collect(List<DataSource> dataSources, String fileLocation,
        FilenameFilter filenameFilter) {
    File file = new File(fileLocation);
    if (!file.exists()) {
        throw new RuntimeException("File " + file.getName() + " doesn't exist");
    }/*from   w w  w  .  java 2  s  .co  m*/
    if (!file.isDirectory()) {
        if (fileLocation.endsWith(".zip") || fileLocation.endsWith(".jar")) {
            ZipFile zipFile;
            try {
                zipFile = new ZipFile(fileLocation);
                Enumeration<? extends ZipEntry> e = zipFile.entries();
                while (e.hasMoreElements()) {
                    ZipEntry zipEntry = e.nextElement();
                    if (filenameFilter.accept(null, zipEntry.getName())) {
                        dataSources.add(new ZipDataSource(zipFile, zipEntry));
                    }
                }
            } catch (IOException ze) {
                throw new RuntimeException("Archive file " + file.getName() + " can't be opened");
            }
        } else {
            dataSources.add(new FileDataSource(file));
        }
    } else {
        // Match files, or directories which are not excluded.
        // FUTURE Make the excluded directories be some configurable option
        Filter<File> filter = new OrFilter<>(Filters.toFileFilter(filenameFilter),
                new AndFilter<>(Filters.getDirectoryFilter(), Filters.toNormalizedFileFilter(
                        Filters.buildRegexFilterExcludeOverInclude(null, Collections.singletonList("SCCS")))));
        FileFinder finder = new FileFinder();
        List<File> files = finder.findFilesFrom(file, Filters.toFilenameFilter(filter), true);
        for (File f : files) {
            dataSources.add(new FileDataSource(f));
        }
    }
    return dataSources;
}

From source file:nl.knaw.dans.common.lang.file.UnzipUtil.java

private static List<File> extract(final File zipFile, final String destPath, final UnzipListener unzipListener)
        throws FileNotFoundException, ZipException, IOException {
    final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));

    // get total uncompressed size of zip file
    final ZipFile zf = new ZipFile(zipFile);
    final Enumeration<? extends ZipEntry> e = zf.entries();
    long totSize = 0;
    int totFiles = 0;
    while (e.hasMoreElements()) {
        final ZipEntry ze = (ZipEntry) e.nextElement();
        totSize += ze.getSize();//from w  w  w.  ja  va 2s. c om
        totFiles++;
    }
    zf.close();

    return extract(zis, destPath, totFiles, unzipListener, totSize);
}

From source file:com.eviware.soapui.integration.exporter.ProjectExporter.java

public static List<String> getZipContents(String archive) {
    List<String> contents = new ArrayList<String>();

    try {/* w  w w.  j a  v  a2 s.c  o m*/
        ZipFile zipFile = new ZipFile(archive);
        for (Enumeration<? extends ZipEntry> em1 = zipFile.entries(); em1.hasMoreElements();) {
            contents.add(em1.nextElement().toString());
        }
    } catch (ZipException ze) {
        SoapUI.logError(ze);
    } catch (IOException e) {
        SoapUI.logError(e);
    }

    return contents;
}