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.yunrang.hadoop.app.utils.CustomizedUtil.java

/**
 * Add entries to <code>packagedClasses</code> corresponding to class files
 * contained in <code>jar</code>.
 * //  w  w  w  .  jav  a 2  s  . c  o m
 * @param jar
 *            The jar who's content to list.
 * @param packagedClasses
 *            map[class -> jar]
 */
public static void updateMap(String jar, Map<String, String> packagedClasses) throws IOException {
    if (null == jar || jar.isEmpty()) {
        return;
    }
    ZipFile zip = null;
    try {
        zip = new ZipFile(jar);
        for (Enumeration<? extends ZipEntry> iter = zip.entries(); iter.hasMoreElements();) {
            ZipEntry entry = iter.nextElement();
            if (entry.getName().endsWith("class")) {
                packagedClasses.put(entry.getName(), jar);
            }
        }
    } finally {
        if (null != zip)
            zip.close();
    }
}

From source file:com.ibm.util.merge.CompareArchives.java

/**
 * @param archive/*from  w  w w. java 2 s  .  co  m*/
 * @return
 * @throws IOException
 */
private static final HashMap<String, ZipEntry> getMembers(ZipFile archive) throws IOException {
    HashMap<String, ZipEntry> map = new HashMap<>();
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) archive.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        map.put(entry.getName(), entry);
    }
    return map;
}

From source file:nl.strohalm.cyclos.themes.ThemeHelper.java

/**
 * Selects the given theme for use//from  ww w .  j  a  va 2 s  .co  m
 */
public static void select(final ServletContext context, final String fileName) throws ThemeException {
    try {
        final File file = realFile(context, fileName);
        if (!file.exists()) {
            throw new ThemeNotFoundException(fileName);
        }
        final ZipFile zipFile = new ZipFile(file);

        // Ensure the properties entry exists
        properties(zipFile);

        // Read the files
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            if (entry.getName().startsWith("/images")) {

            }
        }
    } catch (final ThemeException e) {
        throw e;
    } catch (final Exception e) {
        throw new ThemeException(e);
    }
}

From source file:com.googlecode.dex2jar.test.TestUtils.java

public static void checkZipFile(File zip) throws ZipException, Exception {
    ZipFile zipFile = new ZipFile(zip);
    for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) {
        ZipEntry entry = e.nextElement();
        if (entry.getName().endsWith(".class")) {
            StringWriter sw = new StringWriter();
            // PrintWriter pw = new PrintWriter(sw);
            InputStream is = zipFile.getInputStream(entry);
            try {
                verify(new ClassReader(IOUtils.toByteArray(is)));
            } finally {
                IOUtils.closeQuietly(is);
            }/*from  w  w w  . j  a  v  a2  s .  com*/
            Assert.assertTrue(sw.toString(), sw.toString().length() == 0);
        }
    }
}

From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java

public static void unZipAll(File source, File destination) throws IOException {
    System.out.println("Unzipping - " + source.getName());
    int BUFFER = 2048;

    ZipFile zip = new ZipFile(source);
    try {//  w  w  w. j  a  v  a 2 s .  c  o  m
        destination.getParentFile().mkdirs();
        Enumeration zipFileEntries = zip.entries();

        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            File destFile = new File(destination, currentEntry);
            //destFile = new File(newPath, destFile.getName());
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

            if (!entry.isDirectory()) {
                BufferedInputStream is = null;
                FileOutputStream fos = null;
                BufferedOutputStream dest = null;
                try {
                    is = new BufferedInputStream(zip.getInputStream(entry));
                    int currentByte;
                    // establish buffer for writing file
                    byte data[] = new byte[BUFFER];

                    // write the current file to disk
                    fos = new FileOutputStream(destFile);
                    dest = new BufferedOutputStream(fos, BUFFER);

                    // read and write until last byte is encountered
                    while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, currentByte);
                    }
                } catch (Exception e) {
                    System.out.println("unable to extract entry:" + entry.getName());
                    throw e;
                } finally {
                    if (dest != null) {
                        dest.close();
                    }
                    if (fos != null) {
                        fos.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                }
            } else {
                //Create directory
                destFile.mkdirs();
            }

            if (currentEntry.endsWith(".zip")) {
                // found a zip file, try to extract
                unZipAll(destFile, destinationParent);
                if (!destFile.delete()) {
                    System.out.println("Could not delete zip");
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Failed to successfully unzip:" + source.getName());
    } finally {
        zip.close();
    }
    System.out.println("Done Unzipping:" + source.getName());
}

From source file:com.netflix.nicobar.core.utils.ClassPathUtils.java

/**
 * Get all of the directory paths in a zip/jar file
 * @param pathToJarFile location of the jarfile. can also be a zipfile
 * @return set of directory paths relative to the root of the jar
 *///from w w  w  .ja v  a 2s.c o  m
public static Set<Path> getDirectoriesFromJar(Path pathToJarFile) throws IOException {
    Set<Path> result = new HashSet<Path>();
    ZipFile jarfile = new ZipFile(pathToJarFile.toFile());
    try {
        final Enumeration<? extends ZipEntry> entries = jarfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                result.add(Paths.get(entry.getName()));
            }
        }
        jarfile.close();
    } finally {
        IOUtils.closeQuietly(jarfile);
    }
    return result;
}

From source file:com.thruzero.common.core.utils.FileUtilsExt.java

public static final boolean unzipArchive(final File fromFile, final File toDir) throws IOException {
    boolean result = false; // assumes error

    logHelper.logBeginUnzip(fromFile, toDir);

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

    if (!toDir.exists()) {
        toDir.mkdirs();/*from w  ww .j  a  v  a2  s  . c  o  m*/
        logHelper.logProgressCreatedToDir(toDir);
    }
    logHelper.logProgressToDirIsWritable(toDir);

    if (toDir.canWrite()) {
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();

            if (entry.isDirectory()) {
                File dir = new File(
                        toDir.getAbsolutePath() + EnvironmentHelper.FILE_PATH_SEPARATOR + entry.getName());

                if (!dir.exists()) {
                    dir.mkdirs();
                    logHelper.logProgressFilePathCreated(dir);
                }
            } else {
                File fosz = new File(
                        toDir.getAbsolutePath() + EnvironmentHelper.FILE_PATH_SEPARATOR + entry.getName());
                logHelper.logProgressCopyEntry(fosz);

                File parent = fosz.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }

                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fosz));
                IOUtils.copy(zipFile.getInputStream(entry), bos);
                bos.flush();
            }
        }
        zipFile.close();
        result = true; // success
    } else {
        logHelper.logFileWriteError(fromFile, null);
        zipFile.close();
    }

    return result;
}

From source file:com.gelakinetic.mtgfam.helpers.ZipUtils.java

/**
 * Unzip a file directly into getFilesDir()
 *
 * @param zipFile The zip file to unzip//  ww w . j  a v  a  2  s .c  o m
 * @param context The application context, for getting files and the like
 * @throws IOException Thrown if something goes wrong with unzipping and writing
 */
private static void unZipIt(ZipFile zipFile, Context context) throws IOException {
    Enumeration<? extends ZipEntry> entries;

    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. */
            if (!(new File(entry.getName())).mkdir()) {
                return;
            }
            continue;
        }
        String[] path = entry.getName().split("/");
        String pathCat = "";
        if (path.length > 1) {
            for (int i = 0; i < path.length - 1; i++) {
                pathCat += path[i] + "/";
                File tmp = new File(context.getFilesDir(), pathCat);
                if (!tmp.exists()) {
                    if (!tmp.mkdir()) {
                        return;
                    }
                }
            }
        }

        InputStream in = zipFile.getInputStream(entry);
        OutputStream out;
        if (entry.getName().contains("_preferences.xml")) {
            String sharedPrefsDir = context.getFilesDir().getPath();
            sharedPrefsDir = sharedPrefsDir.substring(0, sharedPrefsDir.lastIndexOf("/")) + "/shared_prefs/";

            out = new BufferedOutputStream(new FileOutputStream(new File(sharedPrefsDir, entry.getName())));
        } else {
            out = new BufferedOutputStream(
                    new FileOutputStream(new File(context.getFilesDir(), entry.getName())));
        }
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }

    zipFile.close();
}

From source file:org.elasticwarehouse.core.ResourceTools.java

private static Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern) {
    final ArrayList<String> retval = new ArrayList<String>();
    ZipFile zf;
    try {// ww  w  .  j av  a 2 s .c o  m
        zf = new ZipFile(file);
    } catch (final ZipException e) {
        throw new Error(e);
    } catch (final IOException e) {
        throw new Error(e);
    }
    final Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        final ZipEntry ze = (ZipEntry) e.nextElement();
        final String fileName = ze.getName();
        final boolean accept = pattern.matcher(fileName).matches();
        if (accept) {
            retval.add(fileName);
        }
    }
    try {
        zf.close();
    } catch (final IOException e1) {
        throw new Error(e1);
    }
    return retval;
}

From source file:de.xwic.appkit.core.util.ZipUtil.java

/**
 * Unzips the files from the zipped file into the destination folder.
 * /*from   w w  w. ja v  a2  s . c  o  m*/
 * @param zippedFile
 *            the files array
 * @param destinationFolder
 *            the folder in which the zip archive will be unzipped
 * @return the file array which consists into the files which were zipped in
 *         the zippedFile
 * @throws IOException
 */
public static File[] unzip(File zippedFile, String destinationFolder) throws IOException {

    ZipFile zipFile = null;
    List<File> files = new ArrayList<File>();

    try {

        zipFile = new ZipFile(zippedFile);
        Enumeration<?> entries = zipFile.entries();

        while (entries.hasMoreElements()) {

            ZipEntry entry = (ZipEntry) entries.nextElement();

            if (!entry.isDirectory()) {

                String filePath = destinationFolder + System.getProperty("file.separator") + entry.getName();
                FileOutputStream stream = new FileOutputStream(filePath);

                InputStream is = zipFile.getInputStream(entry);

                log.info("Unzipping " + entry.getName());

                int n = 0;
                while ((n = is.read(BUFFER)) > 0) {
                    stream.write(BUFFER, 0, n);
                }

                is.close();
                stream.close();

                files.add(new File(filePath));

            }

        }

        zipFile.close();
    } catch (IOException e) {

        log.error("Error: " + e.getMessage(), e);
        throw e;

    } finally {

        try {

            if (null != zipFile) {
                zipFile.close();
            }

        } catch (IOException e) {
            log.error("Error: " + e.getMessage(), e);
            throw e;
        }

    }

    File[] array = files.toArray(new File[files.size()]);
    return array;
}