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.android.tradefed.util.FileUtil.java

/**
 * Utility method to extract entire contents of zip file into given
 * directory/*from  w  w  w. j a v a2 s  .co  m*/
 *
 * @param zipFile
 *            the {@link ZipFile} to extract
 * @param destDir
 *            the local dir to extract file to
 * @throws IOException
 *             if failed to extract file
 */
public static void extractZip(ZipFile zipFile, File destDir) throws IOException {
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {

        ZipEntry entry = entries.nextElement();
        File childFile = new File(destDir, entry.getName());
        childFile.getParentFile().mkdirs();
        if (entry.isDirectory()) {
            continue;
        } else {
            FileUtil.writeToFile(zipFile.getInputStream(entry), childFile);
        }
    }
}

From source file:JarResources.java

public JarResources(String jarFileName) throws Exception {
    this.jarFileName = jarFileName;
    ZipFile zf = new ZipFile(jarFileName);
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();

        htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
    }/* w  ww.j  a v  a2 s.c  o m*/
    zf.close();

    // extract resources and put them into the hashtable.
    FileInputStream fis = new FileInputStream(jarFileName);
    BufferedInputStream bis = new BufferedInputStream(fis);
    ZipInputStream zis = new ZipInputStream(bis);
    ZipEntry ze = null;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            continue;
        }

        int size = (int) ze.getSize();
        // -1 means unknown size.
        if (size == -1) {
            size = ((Integer) htSizes.get(ze.getName())).intValue();
        }

        byte[] b = new byte[(int) size];
        int rb = 0;
        int chunk = 0;
        while (((int) size - rb) > 0) {
            chunk = zis.read(b, rb, (int) size - rb);
            if (chunk == -1) {
                break;
            }
            rb += chunk;
        }

        htJarContents.put(ze.getName(), b);
    }
}

From source file:edu.stanford.epadd.launcher.Splash.java

private static void copyResourcesRecursively(String sourceDirectory, String writeDirectory) throws IOException {
    final URL dirURL = ePADD.class.getClassLoader().getResource(sourceDirectory);
    //final String path = sourceDirectory.substring( 1 );

    if ((dirURL != null) && dirURL.getProtocol().equals("jar")) {
        final JarURLConnection jarConnection = (JarURLConnection) dirURL.openConnection();
        //System.out.println( "jarConnection is " + jarConnection );

        final ZipFile jar = jarConnection.getJarFile();

        final Enumeration<? extends ZipEntry> entries = jar.entries(); // gives ALL entries in jar

        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final String name = entry.getName();
            // System.out.println( name );
            if (!name.startsWith(sourceDirectory)) {
                // entry in wrong subdir -- don't copy
                continue;
            }//from  w w w  .ja va 2 s . c  om
            final String entryTail = name.substring(sourceDirectory.length());

            final File f = new File(writeDirectory + File.separator + entryTail);
            if (entry.isDirectory()) {
                // if its a directory, create it
                final boolean bMade = f.mkdir();
                System.out.println((bMade ? "  creating " : "  unable to create ") + name);
            } else {
                System.out.println("  writing  " + name);
                final InputStream is = jar.getInputStream(entry);
                final OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
                final byte buffer[] = new byte[4096];
                int readCount;
                // write contents of 'is' to 'os'
                while ((readCount = is.read(buffer)) > 0) {
                    os.write(buffer, 0, readCount);
                }
                os.close();
                is.close();
            }
        }

    } else if (dirURL == null) {
        throw new IllegalStateException("can't find " + sourceDirectory + " on the classpath");
    } else {
        // not a "jar" protocol URL
        throw new IllegalStateException("don't know how to handle extracting from " + dirURL);
    }
}

From source file:JarResource.java

private List<String> load(File jarFile) throws IOException {
    List<String> jarContents = new ArrayList<String>();
    try {/*  w  w  w .  ja  v a  2  s.  c o  m*/
        ZipFile zf = new ZipFile(jarFile);
        for (Enumeration e = zf.entries(); e.hasMoreElements();) {
            ZipEntry ze = (ZipEntry) e.nextElement();
            if (ze.isDirectory()) {
                continue;
            }
            jarContents.add(ze.getName());
        }
    } catch (NullPointerException e) {
        System.out.println("done.");
    } catch (ZipException ze) {
        ze.printStackTrace();
    }
    return jarContents;
}

From source file:com.l2jfree.gameserver.script.ScriptPackage.java

/**
 * @param scriptFiles The scriptFiles to set.
 *///from  ww  w  .j  a v a  2s.  c o m
private void addFiles(ZipFile pack) {
    for (Enumeration<? extends ZipEntry> e = pack.entries(); e.hasMoreElements();) {
        ZipEntry entry = e.nextElement();
        if (entry.getName().endsWith(".xml")) {
            try {
                ScriptDocument newScript = new ScriptDocument(entry.getName(), pack.getInputStream(entry));
                _scriptFiles.add(newScript);
            } catch (IOException e1) {
                _log.error(e1.getMessage(), e1);
            }
        } else if (!entry.isDirectory()) {
            // ignore it
        }
    }
}

From source file:dk.deck.resolver.util.ZipUtils.java

public void unzipArchive(File archive, File outputDir) {
    try {/*  w w  w.j  a  v a 2  s .  c  om*/
        ZipFile zipfile = new ZipFile(archive);
        for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            unzipEntry(zipfile, entry, outputDir);
        }
        zipfile.close();
    } catch (Exception e) {
        log.error("Error while extracting file " + archive, e);
    }
}

From source file:dk.deck.resolver.util.ZipUtils.java

public List<String> listArchive(File archive) {
    List<String> paths = new ArrayList<String>();
    try {/*w w w.  j  a va2s .  c o m*/
        ZipFile zipfile = new ZipFile(archive);
        for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            paths.add(entry.getName());
        }
        zipfile.close();
    } catch (Exception e) {
        log.error("Error while extracting file " + archive, e);
    }

    return paths;
}

From source file:com.s2g.pst.resume.importer.UnZipHelper.java

/**
 * Unzip it/*from  w ww. ja  v  a 2  s  . c  o  m*/
 *
 * @param fileName
 * @param outputFolder
 *
 * @throws java.io.FileNotFoundException
 */
public void unZipFolder(String fileName, String outputFolder) throws IOException {

    ZipFile zipFile = new ZipFile(fileName);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File entryDestination = new File(outputFolder, entry.getName());
        entryDestination.getParentFile().mkdirs();
        if (entry.isDirectory()) {
            //FileHelper fileHelper = new FileHelper();
            //String filename = fileHelper.getUniqueFileName(outputFolder, FilenameUtils.removeExtension(entry.getName()));
            //System.out.println("zip :" + filename);
            //new File(filename).mkdirs();

            entryDestination.mkdirs();

        } else {
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }

    zipFile.close();
    this.deleteZipFile(fileName);
    System.out.println("Done");

}

From source file:org.openmrs.module.clinicalsummary.rule.EvaluableSummaryRuleProvider.java

/**
 * @see org.openmrs.logic.rule.provider.RuleProvider#afterStartup()
 *///  w  w  w  .ja v  a2s  .  com
@Override
public void afterStartup() {

    if (log.isDebugEnabled())
        log.debug("Registering clinical summary rules ...");

    try {
        ModuleClassLoader moduleClassLoader = ModuleFactory.getModuleClassLoader(CLINICAL_SUMMARY_MODULE);
        for (URL url : moduleClassLoader.getURLs()) {
            String filename = url.getFile();
            if (StringUtils.contains(filename, CLINICAL_SUMMARY_MODULE_API)) {
                ZipFile zipFile = new ZipFile(filename);
                for (Enumeration list = zipFile.entries(); list.hasMoreElements();) {
                    ZipEntry entry = (ZipEntry) list.nextElement();
                    String zipFileName = FilenameUtils.getBaseName(entry.getName());
                    String zipFileExtension = FilenameUtils.getExtension(entry.getName());
                    if (StringUtils.endsWith(zipFileName, RULE_CLASS_SUFFIX)
                            && StringUtils.equals(zipFileExtension, CLASS_SUFFIX)) {
                        EvaluableRuleVisitor visitor = new EvaluableRuleVisitor();
                        ClassReader classReader = new ClassReader(zipFile.getInputStream(entry));
                        classReader.accept(visitor, false);
                    }
                }
                zipFile.close();
            }
        }
    } catch (IOException e) {
        log.error("Processing rule classes failed. Rule might not get registered.");
    }
}

From source file:org.openmrs.module.amrsreports.rule.MohEvaluableRuleProvider.java

/**
 * @see org.openmrs.logic.rule.provider.RuleProvider#afterStartup()
 *///from ww w .ja v  a 2  s  . c  o m
@Override
public void afterStartup() {

    if (log.isDebugEnabled())
        log.debug("Registering AMRS reports summary rules ...");

    try {
        ModuleClassLoader moduleClassLoader = ModuleFactory.getModuleClassLoader(AMRS_REPORT_MODULE);
        for (URL url : moduleClassLoader.getURLs()) {
            String filename = url.getFile();
            if (StringUtils.contains(filename, AMRS_REPORT_MODULE_API)) {
                ZipFile zipFile = new ZipFile(filename);
                for (Enumeration list = zipFile.entries(); list.hasMoreElements();) {
                    ZipEntry entry = (ZipEntry) list.nextElement();
                    String zipFileName = FilenameUtils.getBaseName(entry.getName());
                    String zipFileExtension = FilenameUtils.getExtension(entry.getName());
                    if (StringUtils.endsWith(zipFileName, RULE_CLASS_SUFFIX)
                            && StringUtils.equals(zipFileExtension, CLASS_SUFFIX)) {
                        MohEvaluableRuleVisitor visitor = new MohEvaluableRuleVisitor();
                        ClassReader classReader = new ClassReader(zipFile.getInputStream(entry));
                        classReader.accept(visitor, false);
                    }
                }
                zipFile.close();
            }
        }
    } catch (IOException e) {
        log.error("Processing rule classes failed. Rule might not get registered.");
    }
}