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:org.eclipse.mylyn.commons.sdk.util.CommonTestUtil.java

private static void unzip(ZipFile zipFile, File rootDstDir, File dstDir, int depth) throws IOException {

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

    try {//  w  w w .j  a  v  a  2 s  . c  o m
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                continue;
            }
            String entryName = entry.getName();
            File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
            file.getParentFile().mkdirs();
            InputStream src = null;
            OutputStream dst = null;
            try {
                src = zipFile.getInputStream(entry);
                dst = new FileOutputStream(file);
                transferData(src, dst);
            } finally {
                if (dst != null) {
                    try {
                        dst.close();
                    } catch (IOException e) {
                        // don't need to catch this
                    }
                }
                if (src != null) {
                    try {
                        src.close();
                    } catch (IOException e) {
                        // don't need to catch this
                    }
                }
            }
        }
    } finally {
        try {
            zipFile.close();
        } catch (IOException e) {
            // don't need to catch this
        }
    }
}

From source file:org.apache.kudu.mapreduce.KuduTableMapReduceUtil.java

/**
 * Add entries to <code>packagedClasses</code> corresponding to class files
 * contained in <code>jar</code>.
 * @param jar The jar who's content to list.
 * @param packagedClasses map[class -> jar]
 *//* w ww. j av a  2  s  . c om*/
private 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:apim.restful.importexport.utils.APIImportUtil.java

/**
 * This method decompresses API the archive
 *
 * @param sourceFile  The archive containing the API
 * @param destination location of the archive to be extracted
 * @return Name of the extracted directory
 * @throws APIImportException If the decompressing fails
 */// w  w w. j  a  v a2  s. c o m
public static String extractArchive(File sourceFile, String destination) throws APIImportException {

    BufferedInputStream inputStream = null;
    InputStream zipInputStream = null;
    FileOutputStream outputStream = null;
    ZipFile zip = null;
    String archiveName = null;

    try {
        zip = new ZipFile(sourceFile);
        Enumeration zipFileEntries = zip.entries();
        int index = 0;

        // Process each entry
        while (zipFileEntries.hasMoreElements()) {

            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();

            //This index variable is used to get the extracted folder name; that is root directory
            if (index == 0) {
                archiveName = currentEntry.substring(0,
                        currentEntry.indexOf(APIImportExportConstants.ARCHIVE_PATH_SEPARATOR));
                --index;
            }

            File destinationFile = new File(destination, currentEntry);
            File destinationParent = destinationFile.getParentFile();

            // create the parent directory structure
            if (destinationParent.mkdirs()) {
                log.info("Creation of folder is successful. Directory Name : " + destinationParent.getName());
            }

            if (!entry.isDirectory()) {
                zipInputStream = zip.getInputStream(entry);
                inputStream = new BufferedInputStream(zipInputStream);

                // write the current file to the destination
                outputStream = new FileOutputStream(destinationFile);
                IOUtils.copy(inputStream, outputStream);
            }
        }
        return archiveName;
    } catch (IOException e) {
        log.error("Failed to extract archive file ", e);
        throw new APIImportException("Failed to extract archive file. " + e.getMessage());
    } finally {
        IOUtils.closeQuietly(zipInputStream);
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:org.cloudifysource.shell.commands.TestRecipe.java

/**
 * Unzips a given file.//from   ww  w  .j  a  v  a  2s  .  com
 *
 * @param inputFile
 *            The zip file to extract
 * @return The new folder, containing the extracted content of the zip file
 * @throws IOException
 *             Reporting a failure to extract the zipped file or close it afterwards
 */
private static File unzipFile(final File inputFile) throws IOException {

    ZipFile zipFile = null;
    try {
        final File baseDir = TestRecipe.createTempDir();
        zipFile = new ZipFile(inputFile);
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();

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

            if (entry.isDirectory()) {

                logger.fine("Extracting directory: " + entry.getName());
                final File dir = new File(baseDir, entry.getName());
                dir.mkdir();
                continue;
            }

            logger.finer("Extracting file: " + entry.getName());
            final File file = new File(baseDir, entry.getName());
            file.getParentFile().mkdirs();
            ServiceReader.copyInputStream(zipFile.getInputStream(entry),
                    new BufferedOutputStream(new FileOutputStream(file)));
        }
        return baseDir;

    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (final IOException e) {
                logger.log(Level.SEVERE, "Failed to close zip file after unzipping zip contents", e);
            }
        }
    }

}

From source file:fridgegameinstaller.installation.java

public static void unzip(ZipFile zipFile, File jiniHomeParentDir) {

    Enumeration files = zipFile.entries();
    File f = null;//w  w w .j  ava2  s.co m
    FileOutputStream fos = null;

    while (files.hasMoreElements()) {
        try {
            ZipEntry entry = (ZipEntry) files.nextElement();
            InputStream eis = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            f = new File(jiniHomeParentDir.getAbsolutePath() + File.separator + entry.getName());

            if (entry.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }

            fos = new FileOutputStream(f);

            while ((bytesRead = eis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            continue;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

From source file:marytts.util.io.FileUtils.java

/**
 * Unzip a zip archive into a directory on the file system.
 * Thanks to Piotr Gabryanczyk for making this code available at http://piotrga.wordpress.com/2008/05/07/how-to-unzip-archive-in-java/
 * @param archive the zip file to extract
 * @param outputDir the directory below which to extract the contents of the zip file. If this does not exist, it is created.
 * @throws IOException if any part of the process fails.
 *//* ww  w.j a  v a  2s . com*/
public static void unzipArchive(File archive, File outputDir) throws IOException {
    ZipFile zipfile = new ZipFile(archive);
    for (Enumeration<? extends ZipEntry> e = zipfile.entries(); e.hasMoreElements();) {
        ZipEntry entry = e.nextElement();
        unzipEntry(zipfile, entry, outputDir);
    }
}

From source file:org.apache.sysml.validation.ValidateLicAndNotice.java

/**
 * This will return the list of files from zip file.
 *
 * @param   zipFileName is the name of zip file from which list of files with specified file extension will be returned.
 * @param   fileExt is the file extension to be used to get list of files to be returned.
 * @return   Returns list of files having specified extension from zip file.
 *//*from ww w. jav a 2  s  .com*/
public static List<String> getFilesFromZip(String zipFileName, String fileExt) {
    List<String> files = new ArrayList<String>();
    try {
        ZipEntry entry;
        ZipFile zipfile = new ZipFile(zipFileName);
        Enumeration e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = (ZipEntry) e.nextElement();
            if (entry.getName().endsWith("." + fileExt)) {
                int iPos = entry.getName().lastIndexOf("/");
                if (iPos == 0)
                    --iPos;
                String strFileName = entry.getName().substring(iPos + 1);
                files.add(strFileName);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (files);
}

From source file:org.apache.sysml.validation.ValidateLicAndNotice.java

/**
 * This will return the set of packages from zip file.
 *
 * @param   zipFileName is the name of zip file from which set of class packages will be returned.
 * @return   Returns set of packages for classes included in the zip file .
 *//*from ww  w  . ja v a  2  s.c om*/
public static HashMap<String, Boolean> getPackagesFromZip(String zipFileName) throws Exception {
    HashMap<String, Boolean> packages = new HashMap<String, Boolean>();
    try {
        ZipEntry entry;
        ZipFile zipfile = new ZipFile(zipFileName);
        Enumeration e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = (ZipEntry) e.nextElement();
            if (!entry.getName().startsWith(Constants.SYSTEMML_PACKAGE)
                    && entry.getName().endsWith("." + Constants.CLASS)) {
                int iPos = entry.getName().lastIndexOf("/");
                if (iPos > 0) {
                    String strPackageName = entry.getName().substring(0, iPos);
                    packages.put(strPackageName, Boolean.FALSE);
                    Utility.debugPrint(Constants.DEBUG_CODE, "Package found : " + strPackageName);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return packages;
}

From source file:org.apache.sysml.validation.ValidateLicAndNotice.java

/**
 * This will return the file from zip file and store it in specified location.
 *
 * @param   zipFileName is the name of zip file from which file to be extracted.
 * @param   fileName is the name of the file to be extracted.
 * @param   strDestLoc is the location where file will be extracted.
 * @param    bFirstDirLevel to indicate to get file from first directory level.
 * @return  Sucess or Failure/*ww w .ja  v a 2 s .  c  o  m*/
 */
public static boolean extractFileFromZip(String zipFileName, String fileName, String strDestLoc,
        boolean bFirstDirLevel) {
    boolean bRetCode = Constants.bFAILURE;
    try {
        BufferedOutputStream bufOut = null;
        BufferedInputStream bufIn = null;
        ZipEntry entry;
        ZipFile zipfile = new ZipFile(zipFileName);
        Enumeration e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = (ZipEntry) e.nextElement();
            if (!entry.getName().endsWith(fileName))
                continue;
            //Get file at root (in single directory) level. This is for License in root location.
            if (bFirstDirLevel && (entry.getName().indexOf('/') != entry.getName().lastIndexOf('/')))
                continue;
            bufIn = new BufferedInputStream(zipfile.getInputStream(entry));
            int count;
            byte data[] = new byte[Constants.BUFFER];
            String strOutFileName = strDestLoc == null ? entry.getName() : strDestLoc + "/" + fileName;
            FileOutputStream fos = new FileOutputStream(strOutFileName);
            bufOut = new BufferedOutputStream(fos, Constants.BUFFER);
            while ((count = bufIn.read(data, 0, Constants.BUFFER)) != -1) {
                bufOut.write(data, 0, count);
            }
            bufOut.flush();
            bufOut.close();
            bufIn.close();
            bRetCode = Constants.bSUCCESS;
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bRetCode;
}

From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java

public static void extractZipFile(String folder, String zipName) throws Exception {

    ZipFile zipFile = new ZipFile(new File(folder + "/" + zipName));

    Enumeration entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        Object obj = entries.nextElement();
        if (obj instanceof ZipEntry) {
            ZipEntry entry = (ZipEntry) obj;

            InputStream eis = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            File f = new File(folder + "/" + entry.getName());

            if (entry.isDirectory()) {
                f.mkdirs();//from ww w. ja v a 2s  .co m
                eis.close();
                continue;
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(f);

            while ((bytesRead = eis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }

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

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

    zipFile.close();
}