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.vamonossoftware.core.ZipUtil.java

public static int countFiles(File zip) {
    try {/*from   w w w  .jav a  2s .  c  o m*/
        ZipFile zf = new ZipFile(zip);
        int count = 0;
        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            final ZipEntry nextElement = (ZipEntry) entries.nextElement();
            if (!nextElement.isDirectory()) {
                count++;
            }
        }
        return count;
    } catch (IOException e) {
        return -1;
    }
}

From source file:Main.java

/**
 * Uncompresses zipped files/*w ww . ja  va 2s.  com*/
 * @param zippedFile The file to uncompress
 * @param destinationDir Where to put the files
 * @return  list of unzipped files
 * @throws java.io.IOException thrown if there is a problem finding or writing the files
 */
public static List<File> unzip(File zippedFile, File destinationDir) throws IOException {
    int buffer = 2048;

    List<File> unzippedFiles = new ArrayList<File>();

    BufferedOutputStream dest;
    BufferedInputStream is;
    ZipEntry entry;
    ZipFile zipfile = new ZipFile(zippedFile);
    Enumeration e = zipfile.entries();
    while (e.hasMoreElements()) {
        entry = (ZipEntry) e.nextElement();

        is = new BufferedInputStream(zipfile.getInputStream(entry));
        int count;
        byte data[] = new byte[buffer];

        File destFile;

        if (destinationDir != null) {
            destFile = new File(destinationDir, entry.getName());
        } else {
            destFile = new File(entry.getName());
        }

        FileOutputStream fos = new FileOutputStream(destFile);
        dest = new BufferedOutputStream(fos, buffer);
        try {
            while ((count = is.read(data, 0, buffer)) != -1) {
                dest.write(data, 0, count);
            }

            unzippedFiles.add(destFile);
        } finally {
            dest.flush();
            dest.close();
            is.close();
        }
    }

    return unzippedFiles;
}

From source file:com.opoopress.maven.plugins.plugin.zip.ZipUtils.java

public static void unzipFileToDirectory(File file, File destDir, boolean keepTimestamp) throws IOException {
    // create output directory if it doesn't exist
    if (!destDir.exists())
        destDir.mkdirs();//from  w  ww .  j  a va2 s .c  om

    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            new File(destDir, entry.getName()).mkdirs();
            continue;
        }

        InputStream inputStream = zipFile.getInputStream(entry);
        File destFile = new File(destDir, entry.getName());

        System.out.println("Unzipping to " + destFile.getAbsolutePath());
        FileUtils.copyInputStreamToFile(inputStream, destFile);
        IOUtils.closeQuietly(inputStream);

        if (keepTimestamp) {
            long time = entry.getTime();
            if (time > 0) {
                destFile.setLastModified(time);
            }
        }
    }

    zipFile.close();
}

From source file:com.vamonossoftware.core.ZipUtil.java

public static int unzip(File zip, File dest) {
    try {//ww w.  java 2  s .c  om
        ZipFile zf = new ZipFile(zip);
        int count = 0;
        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            File destfile = new File(dest, entry.getName());
            if (entry.isDirectory()) {
                destfile.mkdirs();
            } else {
                IOUtils.copy(zf.getInputStream(entry), new FileOutputStream(destfile));
            }
        }
        return count;
    } catch (IOException e) {
        return -1;
    }
}

From source file:Main.java

public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration<? extends ZipEntry> zList = zfile.entries();
    ZipEntry ze = null;//from  w  ww .  j ava  2  s. c o  m
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        Log.d(TAG, "ze.getName() = " + ze.getName());
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(getRealFileName(folderPath, ze.getName())));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
    }
    zfile.close();
    return true;
}

From source file:net.bpelunit.util.ZipUtil.java

public static void unzipFile(File zip, File dir) throws IOException {
    InputStream in = null;/*  ww  w.j  a  va 2 s . c o m*/
    OutputStream out = null;
    ZipFile zipFile = new ZipFile(zip);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (!entry.getName().endsWith("/")) {
            File unzippedFile = new File(dir, entry.getName());
            try {
                in = zipFile.getInputStream(entry);
                unzippedFile.getParentFile().mkdirs();

                out = new FileOutputStream(unzippedFile);

                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }
    }
}

From source file:com.splout.db.common.CompressorUtil.java

public static void uncompress(File file, File dest) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File entryDestination = new File(dest, entry.getName());
        entryDestination.getParentFile().mkdirs();
        InputStream in = zipFile.getInputStream(entry);
        OutputStream out = new FileOutputStream(entryDestination);
        IOUtils.copy(in, out);/*ww  w  .j av a  2  s.  c  o m*/
        in.close();
        out.close();
    }
}

From source file:Main.java

/**
 * extracts a zip file to the given dir/*www  .j a va  2s  .  c om*/
 * @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:local.edg.ClassDB.java

/**
 * Generates a data base with class informations about a given application
 * //from  w w  w  .  j a  v  a 2 s  .c  o m
 * @param appClasses
 *            The classes of the application as directories to class-files or as path to jar-files.
 *            
 * @return Data base with class information.
 */
public static Map<String, Class> create(String[] appClasses) {
    ClassDbVisitor cv = new ClassDbVisitor();
    for (String s : appClasses) {
        File f = new File(s);
        if (f.isDirectory()) {
            Collection<File> files = FileUtils.listFiles(f, new String[] { "class" }, true);
            for (File cf : files) {
                try {
                    ClassReader cr = new ClassReader(new FileInputStream(cf));
                    cr.accept(cv, 0);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else if (f.isFile() && s.toLowerCase().endsWith(".jar")) {
            try {
                ZipFile zf = new ZipFile(f);
                Enumeration<? extends ZipEntry> en = zf.entries();
                while (en.hasMoreElements()) {
                    ZipEntry e = en.nextElement();
                    String name = e.getName();
                    if (name.endsWith(".class")) {
                        new ClassReader(zf.getInputStream(e)).accept(cv, 0);
                    }
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return cv.getClassDb();
}

From source file:edu.umd.cs.guitar.testcase.plugin.edg.ClassDB.java

/**
 * Generates a database containing class information about given application classes
 * @param appClasses The classes of an application as directories to class-files or as path to jar-files
 * @return Database containing class information
 *//*from  w ww  . j a v a2 s. co m*/
public static Map<String, Class> create(String[] appClasses) {
    ClassDBVisitor cv = new ClassDBVisitor();
    for (String s : appClasses) {
        File f = new File(s);
        if (f.isDirectory()) {
            Collection<File> files = FileUtils.listFiles(f, new String[] { "class" }, true);
            for (File cf : files) {
                try {
                    ClassReader cr = new ClassReader(new FileInputStream(cf));
                    cr.accept(cv, 0);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else if (f.isFile() && s.toLowerCase().endsWith(".jar")) {
            try {
                ZipFile zf = new ZipFile(f);
                Enumeration<? extends ZipEntry> en = zf.entries();
                while (en.hasMoreElements()) {
                    ZipEntry e = en.nextElement();
                    String name = e.getName();
                    if (name.endsWith(".class")) {
                        new ClassReader(zf.getInputStream(e)).accept(cv, 0);
                    }
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return cv.getClassDb();
}