Example usage for java.util Enumeration hasMoreElements

List of usage examples for java.util Enumeration hasMoreElements

Introduction

In this page you can find the example usage for java.util Enumeration hasMoreElements.

Prototype

boolean hasMoreElements();

Source Link

Document

Tests if this enumeration contains more elements.

Usage

From source file:gov.va.oia.terminology.converters.sharedUtils.Unzip.java

public final static void unzip(File zipFile, File rootDir) throws IOException {
    ZipFile zip = new ZipFile(zipFile);
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        java.io.File f = new java.io.File(rootDir, entry.getName());
        if (entry.isDirectory()) {
            f.mkdirs();//from   w  w w  .  j  av  a  2s  .  co m
            continue;
        } else {
            f.createNewFile();
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            is = zip.getInputStream(entry);
            os = new FileOutputStream(f);
            IOUtils.copy(is, os);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    // noop
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    // noop
                }
            }
        }
    }
    zip.close();
}

From source file:io.reactiverse.vertx.maven.plugin.utils.WebJars.java

/**
 * Checks whether the given file is a WebJar or not (http://www.webjars.org/documentation).
 * The check is based on the presence of {@literal META-INF/resources/webjars/} directory in the jar file.
 *
 * @param file the file.// w  w w .  j  av a2 s  . com
 * @return {@literal true} if it's a bundle, {@literal false} otherwise.
 */
public static boolean isWebJar(Log log, File file) {
    if (file == null) {
        return false;
    }
    Set<String> found = new LinkedHashSet<>();
    if (file.isFile() && file.getName().endsWith(".jar")) {
        try (JarFile jar = new JarFile(file)) {

            // Fast return if the base structure is not there
            if (jar.getEntry(WEBJAR_LOCATION) == null) {
                return false;
            }

            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                Matcher matcher = WEBJAR_REGEX.matcher(entry.getName());
                if (matcher.matches()) {
                    found.add(matcher.group(1) + "-" + matcher.group(2));
                }
            }
        } catch (IOException e) {
            log.error("Cannot check if the file " + file.getName() + " is a webjar, cannot open it", e);
            return false;
        }

        for (String lib : found) {
            log.info("Web Library found in " + file.getName() + " : " + lib);
        }

        return !found.isEmpty();
    }

    return false;
}

From source file:Main.java

/**
 * extracts a zip file to the given dir/*from w  ww .ja v a2 s  .  c o  m*/
 * @param archive
 * @param destDir
 * @throws IOException 
 * @throws ZipException 
 * @throws Exception
 */
public static void extractZipArchive(File archive, File destDir) throws ZipException, IOException {
    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(destDir, entryFileName)));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
}

From source file:b2s.idea.mavenize.JarCombiner.java

private static void copyContentsOf(File input, ZipOutputStream output, JarContext context) throws IOException {
    ZipFile zip = null;//  w  w  w. j a va2 s.  com
    try {
        zip = new ZipFile(input);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (fileCanBeIgnored(entry, context)) {
                continue;
            }

            if (isServiceEntry(entry)) {
                context.addService(entry.getName(), contentsOf(entry, zip));
                continue;
            }

            output.putNextEntry(new ZipEntry(entry.getName()));
            output.write(contentsOf(entry, zip));
            output.flush();
            output.closeEntry();

            context.addVisitedEntry(entry);
        }
    } finally {
        close(zip);
    }
}

From source file:jp.co.golorp.emarf.sql.DataSources.java

/**
 * ?/*from   w  w w .j  a v a  2  s . co  m*/
 *
 * @return DataSource
 */
public static DataSource get() {

    if (ds != null) {
        return ds;
    }

    /*
     * JNDI??
     */

    String name = BUNDLE.getString(DATA_SOURCE_NAME);
    try {
        Context context = new InitialContext();
        ds = (DataSource) context.lookup(name);
        return ds;
    } catch (NamingException e) {
        LOG.warn(e.getMessage());
    }

    /*
     * DBCP??
     */

    Properties properties = new Properties();
    Enumeration<String> keys = BUNDLE.getKeys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        String value = BUNDLE.getString(key);
        properties.put(key, value);
        // if (value.contains("mysql")) {
        // DataSources.isMySQL = true;
        // } else if (value.contains("oracle")) {
        // DataSources.isOracle = true;
        // }
    }

    try {
        ds = BasicDataSourceFactory.createDataSource(properties);
        return ds;
    } catch (Exception e) {
        throw new SystemError(e);
    }
}

From source file:de.langmi.spring.batch.examples.readers.file.zip.ZipMultiResourceItemReader.java

/**
 * Extract only files from the zip archive.
 *
 * @param currentZipFile// w  w w.  j  a  v a  2  s  .co m
 * @param extractedResources
 * @throws IOException 
 */
private static void extractFiles(final ZipFile currentZipFile, final List<Resource> extractedResources)
        throws IOException {
    Enumeration<? extends ZipEntry> zipEntryEnum = currentZipFile.entries();
    while (zipEntryEnum.hasMoreElements()) {
        ZipEntry zipEntry = zipEntryEnum.nextElement();
        LOG.info("extracting:" + zipEntry.getName());
        // traverse directories
        if (!zipEntry.isDirectory()) {
            // add inputStream
            extractedResources
                    .add(new InputStreamResource(currentZipFile.getInputStream(zipEntry), zipEntry.getName()));
            LOG.info("using extracted file:" + zipEntry.getName());
        }
    }
}

From source file:com.itude.mobile.mobbl.server.util.HeaderUtil.java

public static void printRequestHeaders(Logger logger, HttpServletRequest request) {
    if (request == null)
        throw new IllegalArgumentException("Request can't be null");
    if (logger == null)
        throw new IllegalArgumentException("Logger can't be null");

    logger.debug("List of request parameters:");

    @SuppressWarnings("unchecked")
    Enumeration<String> requestHeaders = request.getHeaderNames();
    if (requestHeaders != null) {
        while (requestHeaders.hasMoreElements()) {
            String header = requestHeaders.nextElement();

            @SuppressWarnings("unchecked")
            Enumeration<String> headers = request.getHeaders(header);
            if (headers != null) {
                while (headers.hasMoreElements())
                    logger.debug(header + ":" + headers.nextElement());
            }//from  w w w.  ja va2s.  com
        }
    }
}

From source file:com.enonic.esl.io.FileUtil.java

/**
 * Inflates a zip into a directory. Creates the directory and/or its parents if they don't exists.
 *
 * @param zipFile ZipFile zip file to inflate
 * @param dir     File destination directory
 */// w w  w .j a  v  a  2  s.c  om
public static void inflateZipFile(ZipFile zipFile, File dir) throws IOException {
    Enumeration entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        inflateFile(dir, zipFile, entry);
    }
}

From source file:ste.web.beanshell.jetty.BeanShellUtils.java

/**
 * Cleans up request variables so that they won't be set in next invocations
 *
 * @param interpreter the beanshell interpreter
 * @param request the request/*from  w  w  w .ja  va 2  s  .  c om*/
 *
 */
public static void cleanup(final Interpreter interpreter, final HttpServletRequest request) throws EvalError {
    Enumeration<String> params = request.getParameterNames();
    while (params.hasMoreElements()) {
        interpreter.unset(params.nextElement());
    }
}

From source file:com.predic8.membrane.examples.DistributionExtractingTestcase.java

public static final void unzip(File zip, File target) throws IOException {
    ZipFile zipFile = new ZipFile(zip);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            // Assume directories are stored parents first then children.
            // This is not robust, just for demonstration purposes.
            new File(target, entry.getName()).mkdir();
        } else {//from  w ww. ja v  a 2 s .co m
            FileOutputStream fos = new FileOutputStream(new File(target, entry.getName()));
            try {
                copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(fos));
            } finally {
                fos.close();
            }
        }
    }
    zipFile.close();
}