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:net.chris54721.infinitycubed.utils.Utils.java

public static void unzip(File zipFile, File outputFolder) {
    InputStream in = null;/*from   w  w w.jav a  2  s  . c  om*/
    OutputStream out = null;
    try {
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        if (!outputFolder.isDirectory())
            outputFolder.mkdirs();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryTarget = new File(outputFolder, entry.getName());
            entryTarget.getParentFile().mkdirs();
            if (entry.isDirectory())
                entryTarget.mkdirs();
            else {
                in = zip.getInputStream(entry);
                out = new FileOutputStream(entryTarget);
                IOUtils.copy(in, out);
            }
        }
    } catch (Exception e) {
        LogHelper.error("Failed extracting " + zipFile.getName(), e);
    } finally {
        if (in != null)
            IOUtils.closeQuietly(in);
        if (out != null)
            IOUtils.closeQuietly(out);
    }
}

From source file:Main.java

public static void unzip(String strZipFile) {

    try {// ww w  .j  a  v a 2  s . c o  m
        /*
         * STEP 1 : Create directory with the name of the zip file
         * 
         * For e.g. if we are going to extract c:/demo.zip create c:/demo directory where we can extract all the zip entries
         */
        File fSourceZip = new File(strZipFile);
        String zipPath = strZipFile.substring(0, strZipFile.length() - 4);
        File temp = new File(zipPath);
        temp.mkdir();
        System.out.println(zipPath + " created");

        /*
         * STEP 2 : Extract entries while creating required sub-directories
         */
        ZipFile zipFile = new ZipFile(fSourceZip);
        Enumeration<? extends ZipEntry> e = zipFile.entries();

        while (e.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            File destinationFilePath = new File(zipPath, entry.getName());

            // create directories if required.
            destinationFilePath.getParentFile().mkdirs();

            // if the entry is directory, leave it. Otherwise extract it.
            if (entry.isDirectory()) {
                continue;
            } else {
                // System.out.println("Extracting " + destinationFilePath);

                /*
                 * Get the InputStream for current entry of the zip file using
                 * 
                 * InputStream getInputStream(Entry entry) method.
                 */
                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

                int b;
                byte buffer[] = new byte[1024];

                /*
                 * read the current entry from the zip file, extract it and write the extracted file.
                 */
                FileOutputStream fos = new FileOutputStream(destinationFilePath);
                BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);

                while ((b = bis.read(buffer, 0, 1024)) != -1) {
                    bos.write(buffer, 0, b);
                }

                // flush the output stream and close it.
                bos.flush();
                bos.close();

                // close the input stream.
                bis.close();
            }

        }
        zipFile.close();
    } catch (IOException ioe) {
        System.out.println("IOError :" + ioe);
    }

}

From source file:org.paxml.bean.UnzipTag.java

public static void unzip(File file, File dir) {
    dir.mkdirs();/*w  w w . ja  v a  2  s .  co  m*/
    ZipFile zipFile = null;

    try {
        zipFile = new ZipFile(file);

        Enumeration entries = zipFile.entries();

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

            if (entry.isDirectory()) {

                new File(dir, entry.getName()).mkdirs();
                continue;
            }

            InputStream in = null;
            OutputStream out = null;
            try {
                zipFile.getInputStream(entry);
                out = new BufferedOutputStream(new FileOutputStream(entry.getName()));
                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }

    } catch (IOException ioe) {

        throw new PaxmlRuntimeException(
                "Cannot unzip file: " + file.getAbsolutePath() + " under dir: " + dir.getAbsolutePath(), ioe);

    } finally {

        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
}

From source file:org.cloudfoundry.client.lib.SampleProjects.java

private static void unpackZip(ZipFile zipFile, File unpackDir) throws IOException {
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File destination = new File(unpackDir.getAbsolutePath() + "/" + entry.getName());
        if (entry.isDirectory()) {
            destination.mkdirs();/* w w w  .  ja v a 2  s. com*/
        } else {
            destination.getParentFile().mkdirs();
            FileCopyUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(destination));
        }
        if (entry.getTime() != -1) {
            destination.setLastModified(entry.getTime());
        }
    }
}

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

public static void extract(final AbstractVertxMojo mojo, File in, File out, boolean stripVersion)
        throws IOException {
    ZipFile file = new ZipFile(in);
    try {/*from www.ja va 2s. co m*/
        Enumeration<? extends ZipEntry> entries = file.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.getName().startsWith(WEBJAR_LOCATION) && !entry.isDirectory()) {
                // Compute destination.
                File output = new File(out, entry.getName().substring(WEBJAR_LOCATION.length()));
                if (stripVersion) {
                    String path = entry.getName().substring(WEBJAR_LOCATION.length());
                    Matcher matcher = WEBJAR_INTERNAL_PATH_REGEX.matcher(path);
                    if (matcher.matches()) {
                        output = new File(out, matcher.group(1) + "/" + matcher.group(3));
                    } else {
                        mojo.getLog().warn(path
                                + " does not match the regex - did not strip the version for this" + " file");
                    }
                }
                InputStream stream = null;
                try {
                    stream = file.getInputStream(entry);
                    output.getParentFile().mkdirs();
                    org.apache.commons.io.FileUtils.copyInputStreamToFile(stream, output);
                } catch (IOException e) {
                    mojo.getLog().error("Cannot unpack " + entry.getName() + " from " + file.getName(), e);
                    throw e;
                } finally {
                    IOUtils.closeQuietly(stream);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(file);
    }
}

From source file:org.atomserver.utils.io.JarUtils.java

/**
 * Copies an entire folder out of a jar to a physical location (i.e. to a directory).
 * @param cl The ClassLoader to use when pulling resources from the Jar
 * @param jar  The jar opened as a File//from   w  w  w  . java  2 s .c o m
 * @param folderName  The folder name in the jar (e.g. test-resources/var)
 * @param destDir The root directory to copy into (will be created if req'd)
 * @throws IOException
 */
static public void copyJarFolder(ClassLoader cl, File jar, String folderName, File destDir) throws IOException {

    log.debug("jar= " + jar + " destDir= " + destDir + " folderName= " + folderName);

    FileUtils.forceMkdir(destDir);

    ZipFile zipfile = new ZipFile(jar);

    Enumeration entries = zipfile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        log.trace("examining ENTRY = " + entry.getName());

        if (entry.getName().contains(folderName)) {
            File fileDest = new File(destDir, entry.getName());
            if (!fileDest.exists()) {
                if (entry.isDirectory()) {
                    log.trace("Creating Directory :: " + fileDest);
                    FileUtils.forceMkdir(fileDest);
                } else {
                    copyFromJar(cl, entry.getName(), fileDest);
                }
            } else {
                log.trace("file (" + fileDest + ") already exists");
            }

        }
    }
}

From source file:net.sf.sripathi.ws.mock.util.FileUtil.java

public static void createFilesAndFolder(String root, ZipFile zip) {

    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
    Set<String> files = new TreeSet<String>();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        if (entry.getName().toLowerCase().endsWith(".wsdl") || entry.getName().toLowerCase().endsWith(".xsd"))
            files.add(entry.getName());// ww  w .j av a 2 s.  c  om
    }

    File rootFolder = new File(root);
    if (!rootFolder.exists()) {
        throw new MockException("Unable to create file - " + root);
    }

    for (String fileStr : files) {

        String folder = root;

        String[] split = fileStr.split("/");

        if (split.length > 1) {

            for (int i = 0; i < split.length - 1; i++) {

                folder = folder + "/" + split[i];
                File f = new File(folder);
                if (!f.exists()) {
                    f.mkdir();
                }
            }
        }

        File file = new File(folder + "/" + split[split.length - 1]);
        FileOutputStream fos = null;
        InputStream zipStream = null;
        try {
            fos = new FileOutputStream(file);
            zipStream = zip.getInputStream(zip.getEntry(fileStr));
            fos.write(IOUtils.toByteArray(zipStream));
        } catch (Exception e) {
            throw new MockException("Unable to create file - " + fileStr);
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
            }
            try {
                zipStream.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:org.apache.geode.management.internal.configuration.utils.ZipUtils.java

public static void unzip(String zipFilePath, String outputDirectoryPath) throws IOException {
    ZipFile zipFile = new ZipFile(zipFilePath);
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> zipEntries = (Enumeration<ZipEntry>) zipFile.entries();

    try {/*  w  ww .j a v a  2  s.  co m*/
        while (zipEntries.hasMoreElements()) {
            ZipEntry zipEntry = zipEntries.nextElement();
            String fileName = outputDirectoryPath + File.separator + zipEntry.getName();

            if (zipEntry.isDirectory()) {
                FileUtils.forceMkdir(new File(fileName));
                continue;
            }
            File entryDestination = new File(fileName);
            File parent = entryDestination.getParentFile();
            if (parent != null) {
                FileUtils.forceMkdir(parent);
            }
            if (entryDestination.createNewFile()) {

                InputStream in = zipFile.getInputStream(zipEntry);
                OutputStream out = new FileOutputStream(entryDestination);
                try {
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(in);
                    IOUtils.closeQuietly(out);
                }
            } else {
                throw new IOException("Cannot create file :" + entryDestination.getCanonicalPath());
            }
        }
    } finally {
        zipFile.close();
    }
}

From source file:com.chinamobile.bcbsp.fault.tools.Zip.java

/**
 * Uncompress *.zip files./*ww  w .j  a  v  a  2 s.  c o m*/
 * @param fileName
 *        : file to be uncompress
 */
@SuppressWarnings("unchecked")
public static void decompress(String fileName) {
    File sourceFile = new File(fileName);
    String filePath = sourceFile.getParent() + "/";
    try {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration en = zipFile.entries();
        byte[] data = new byte[BUFFER];
        while (en.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) en.nextElement();
            if (entry.isDirectory()) {
                new File(filePath + entry.getName()).mkdirs();
                continue;
            }
            bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + entry.getName());
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            bos = new BufferedOutputStream(new FileOutputStream(file));
            int count;
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                bos.write(data, 0, count);
            }
            bis.close();
            bos.close();
        }
        zipFile.close();
    } catch (IOException e) {
        //LOG.error("[compress]", e);
        throw new RuntimeException("[compress]", e);
    }
}

From source file:com.dustindoloff.s3websitedeploy.Main.java

private static boolean upload(final AmazonS3 s3Client, final String bucket, final ZipFile zipFile) {
    boolean failed = false;
    final ObjectMetadata data = new ObjectMetadata();
    final Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = entries.nextElement();
        data.setContentLength(entry.getSize());
        try {/*from   w ww . j ava2  s . co m*/
            s3Client.putObject(bucket, entry.getName(), zipFile.getInputStream(entry), data);
        } catch (final AmazonClientException | IOException e) {
            failed = true;
        }
    }
    return !failed;
}