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.codice.ddf.admin.application.service.impl.ApplicationFileInstaller.java

/**
 * Verifies that the file is a file Karaf ARchive is a valid file.
 *
 * @param appZip//  ww  w . j  a v a2 s . com
 *            Zip file that should be checked.
 * @return true if the file is a valid kar, false if not.
 */
private static boolean isFileValid(ZipFile appZip) {
    Enumeration<?> entries = appZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry curEntry = (ZipEntry) entries.nextElement();
        if (!curEntry.isDirectory()) {
            if (isFeatureFile(curEntry)) {
                LOGGER.info("Found a feature in the application: {} which verifies this is a Karaf ARchive.",
                        curEntry.getName());
                return true;
            }
        }
    }
    return false;
}

From source file:org.eclipse.jubula.tools.internal.utils.ZipUtil.java

/**
 * Unzips the contents of the given zip file into the given directory.
 * /*from  w w w  . j a  v  a 2s .co  m*/
 * @param srcZip The zip file to extract.
 * @param targetDir The base directory for extracted files.
 * @param filter Only files accepted by this filter will be extracted.
 * @return all extracted files.
 * @throws IOException
 */
public static File[] unzipFiles(File srcZip, File targetDir, IZipEntryFilter filter) throws IOException {

    ZipFile archive = new ZipFile(srcZip);
    Enumeration e = archive.entries();
    List<File> extractedFiles = new ArrayList<File>();
    while (e.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        if (filter.accept(entry)) {
            File file = new File(targetDir, entry.getName());
            if (entry.isDirectory() && !file.exists()) {
                file.mkdirs();
            } else {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }

                extractedFiles.add(file);
                unzipFile(archive, file, entry);
            }
        }
    }

    return (File[]) extractedFiles.toArray();
}

From source file:org.eclipse.jubula.tools.utils.ZipUtil.java

/**
 * Unzips the contents of the given zip file into the given directory.
 * /* w  w  w .  j a v  a  2 s. c om*/
 * @param srcZip The zip file to extract.
 * @param targetDir The base directory for extracted files.
 * @param filter Only files accepted by this filter will be extracted.
 * @return all extracted files.
 * @throws IOException
 */
public static File[] unzipFiles(File srcZip, File targetDir, IZipEntryFilter filter) throws IOException {

    ZipFile archive = new ZipFile(srcZip);
    Enumeration e = archive.entries();
    List extractedFiles = new ArrayList();
    while (e.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        if (filter.accept(entry)) {
            File file = new File(targetDir, entry.getName());
            if (entry.isDirectory() && !file.exists()) {
                file.mkdirs();
            } else {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }

                extractedFiles.add(file);
                unzipFile(archive, file, entry);
            }
        }
    }

    return (File[]) extractedFiles.toArray();
}

From source file:de.langmi.spring.batch.examples.readers.file.zip.ZipMultiResourceItemReader.java

/**
 * Extract only files from the zip archive.
 *
 * @param currentZipFile// w w  w .j  av a 2 s. com
 * @param extractedResources
 * @throws IOException 
 */
private static void extractFiles(final ZipFile currentZipFile, final List<Resource> extractedResources)
        throws IOException {
    Enumeration<? extends ZipEntry> zipEntryEnum = currentZipFile.entries();
    while (zipEntryEnum.hasMoreElements()) {
        ZipEntry zipEntry = zipEntryEnum.nextElement();
        LOG.info("extracting:" + zipEntry.getName());
        // traverse directories
        if (!zipEntry.isDirectory()) {
            // add inputStream
            extractedResources
                    .add(new InputStreamResource(currentZipFile.getInputStream(zipEntry), zipEntry.getName()));
            LOG.info("using extracted file:" + zipEntry.getName());
        }
    }
}

From source file:org.openo.nfvo.jujuvnfmadapter.common.DownloadCsarManager.java

/**
 * unzip CSAR packge/*from  w ww .ja  v  a  2s .c o m*/
 * @param fileName filePath
 * @return
 */
public static int unzipCSAR(String fileName, String filePath) {
    final int BUFFER = 2048;
    int status = 0;

    try {
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration emu = zipFile.entries();
        int i = 0;
        while (emu.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) emu.nextElement();
            //read directory as file first,so only need to create directory
            if (entry.isDirectory()) {
                new File(filePath + entry.getName()).mkdirs();
                continue;
            }
            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + entry.getName());
            //Because that is random to read zipfile,maybe the file is read first
            //before the directory is read,so we need to create directory first.
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

            int count;
            byte data[] = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                bos.write(data, 0, count);
            }
            bos.flush();
            bos.close();
            bis.close();

            if (entry.getName().endsWith(".zip")) {
                File subFile = new File(filePath + entry.getName());
                if (subFile.exists()) {
                    int subStatus = unzipCSAR(filePath + entry.getName(), subFile.getParent() + "/");
                    if (subStatus != 0) {
                        LOG.error("sub file unzip fail!" + subFile.getName());
                        status = Constant.UNZIP_FAIL;
                        return status;
                    }
                }
            }
        }
        status = Constant.UNZIP_SUCCESS;
        zipFile.close();
    } catch (Exception e) {
        status = Constant.UNZIP_FAIL;
        e.printStackTrace();
    }
    return status;
}

From source file:com.twosigma.beakerx.kernel.magic.command.ClasspathAddMvnDepsCellMagicCommandTest.java

private static void unzipRepo() {
    try {//w  w w .  j ava 2 s .  c o  m
        ZipFile zipFile = new ZipFile(BUILD_PATH + "/testMvnCache.zip");
        Enumeration<?> enu = zipFile.entries();
        while (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();
            String name = BUILD_PATH + "/" + zipEntry.getName();
            File file = new File(name);
            if (name.endsWith("/")) {
                file.mkdirs();
                continue;
            }

            File parent = file.getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }

            InputStream is = zipFile.getInputStream(zipEntry);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = is.read(bytes)) >= 0) {
                fos.write(bytes, 0, length);
            }
            is.close();
            fos.close();

        }
        zipFile.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.jubula.tools.internal.utils.ZipUtil.java

/**
 * Extracts all JAR files from the given ZIP file into temporary JAR files. 
 * The directory structure of the extracted contents is not maintained.
 * A mapping from ZIP file to extracted JARs is maintained by this class,
 * so multiple calls to this method for a single ZIP file will extract JAR 
 * files once and return references to those files for each subsequent call.
 * The extracted JAR files are deleted on VM exit.
 * @param srcZip The ZIP file to extract.
 * @return all extracted files.//w  ww  .  j a v a  2  s . c  om
 * @throws IOException
 */
public static File[] unzipTempJars(File srcZip) throws IOException {

    if (zipToTempJars.containsKey(srcZip)) {
        return zipToTempJars.get(srcZip);
    }

    IZipEntryFilter filter = new IZipEntryFilter() {
        public boolean accept(ZipEntry entry) {
            return entry.getName().toLowerCase().endsWith(JAR_FILE_EXT);
        }
    };
    ZipFile archive = new ZipFile(srcZip);
    Enumeration e = archive.entries();
    List<File> extractedFiles = new ArrayList<File>();
    while (e.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        if (filter.accept(entry)) {
            if (!entry.isDirectory()) {
                String prefix = entry.getName().substring(entry.getName().lastIndexOf("/") + 1, //$NON-NLS-1$
                        entry.getName().toLowerCase().lastIndexOf(JAR_FILE_EXT));
                File file = File.createTempFile(StringUtils.rightPad(prefix, 3), JAR_FILE_EXT);
                extractedFiles.add(file);
                file.deleteOnExit();
                unzipFile(archive, file, entry);
            }
        }
    }

    File[] files = extractedFiles.toArray(new File[extractedFiles.size()]);
    zipToTempJars.put(srcZip, files);
    return files;
}

From source file:org.eclipse.jubula.tools.utils.ZipUtil.java

/**
 * Extracts all JAR files from the given ZIP file into temporary JAR files. 
 * The directory structure of the extracted contents is not maintained.
 * A mapping from ZIP file to extracted JARs is maintained by this class,
 * so multiple calls to this method for a single ZIP file will extract JAR 
 * files once and return references to those files for each subsequent call.
 * The extracted JAR files are deleted on VM exit.
 * @param srcZip The ZIP file to extract.
 * @return all extracted files.//from www.j a v a 2s  . c o  m
 * @throws IOException
 */
public static File[] unzipTempJars(File srcZip) throws IOException {

    if (zipToTempJars.containsKey(srcZip)) {
        return (File[]) zipToTempJars.get(srcZip);
    }

    IZipEntryFilter filter = new IZipEntryFilter() {
        public boolean accept(ZipEntry entry) {
            return entry.getName().toLowerCase().endsWith(JAR_FILE_EXT);
        }
    };
    ZipFile archive = new ZipFile(srcZip);
    Enumeration e = archive.entries();
    List extractedFiles = new ArrayList();
    while (e.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        if (filter.accept(entry)) {
            if (!entry.isDirectory()) {
                String prefix = entry.getName().substring(entry.getName().lastIndexOf("/") + 1, //$NON-NLS-1$
                        entry.getName().toLowerCase().lastIndexOf(JAR_FILE_EXT));
                File file = File.createTempFile(StringUtils.rightPad(prefix, 3), JAR_FILE_EXT);
                extractedFiles.add(file);
                file.deleteOnExit();
                unzipFile(archive, file, entry);
            }
        }
    }

    File[] files = (File[]) extractedFiles.toArray(new File[extractedFiles.size()]);
    zipToTempJars.put(srcZip, files);
    return files;
}

From source file:com.mc.printer.model.utils.ZipHelper.java

public static void unZip(String sourceZip, String outDirName) throws IOException {
    log.info("unzip source:" + sourceZip);
    log.info("unzip to :" + outDirName);
    ZipFile zfile = new ZipFile(sourceZip);
    System.out.println(zfile.getName());
    Enumeration zList = zfile.entries();
    ZipEntry ze = null;// ww  w  . ja v  a2s  .co  m
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        //ZipFileZipEntry
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        //ZipEntry?InputStreamOutputStream
        File fil = getRealFileName(outDirName, ze.getName());

        OutputStream os = new BufferedOutputStream(new FileOutputStream(fil));
        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();
        log.debug("Extracted: " + ze.getName());
    }
    zfile.close();

}

From source file:com.ts.db.connector.ConnectorDriverManager.java

private static Driver getDriver(File jar, String url, ClassLoader cl) throws IOException {
    ZipFile zipFile = new ZipFile(jar);
    try {//w  w  w.  j  a v  a  2  s . c o m
        for (ZipEntry entry : Iteration.asIterable(zipFile.entries())) {
            final String name = entry.getName();
            if (name.endsWith(".class")) {
                final String fqcn = name.replaceFirst("\\.class", "").replace('/', '.');
                try {
                    Class<?> c = DynamicLoader.loadClass(fqcn, cl);
                    if (Driver.class.isAssignableFrom(c)) {
                        Driver driver = (Driver) c.newInstance();
                        if (driver.acceptsURL(url)) {
                            return driver;
                        }
                    }
                } catch (Exception ex) {
                    if (log.isTraceEnabled()) {
                        log.trace(ex.toString());
                    }
                }
            }
        }
    } finally {
        zipFile.close();
    }
    return null;
}