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.gisgraphy.importer.ImporterHelper.java

/**
 * unzip a file in the same directory as the zipped file
 * /*from   w  ww.j  a  v a 2  s  .com*/
 * @param file
 *            The file to unzip
 */
public static void unzipFile(File file) {
    logger.info("will Extracting file: " + file.getName());
    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile;

    try {
        zipFile = new ZipFile(file);

        entries = zipFile.entries();

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

            if (entry.isDirectory()) {
                // Assume directories are stored parents first then
                // children.
                (new File(entry.getName())).mkdir();
                continue;
            }

            logger.info("Extracting file: " + entry.getName() + " to " + file.getParent() + File.separator
                    + entry.getName());
            copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(
                    new FileOutputStream(file.getParent() + File.separator + entry.getName())));
        }

        zipFile.close();
    } catch (IOException e) {
        logger.error("can not unzip " + file.getName() + " : " + e.getMessage(), e);
        throw new ImporterException(e);
    }
}

From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java

/**
 * returns the {@link InputStream} for the file with given name in the given zipped file
 *//*from w w  w .ja va  2s. c  o m*/
public static InputStream getInputStreamForSingleFile(final URL zipFileURL, final String zippedFile)
        throws IOException, URISyntaxException {
    final ZipFile zf = new ZipFile(new File(zipFileURL.toURI()));

    final Enumeration<?> entries = zf.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry ze = (ZipEntry) entries.nextElement();
        if (!ze.isDirectory()
                && (StringUtils.isBlank(zippedFile) || zippedFile.equalsIgnoreCase(ze.getName()))) {
            final long size = ze.getSize();
            // FIXME: why this size check: dubios!
            if (size > 0) {
                return zf.getInputStream(ze);
            }
        }
    }

    return null;
}

From source file:com.asakusafw.bulkloader.testutil.UnitTestUtil.java

/**
 * Create file list from Zip file.//from w  ww  . j  ava  2 s.c  om
 * @param originalZipFile source Zip file
 * @param targetFileList target file list file
 */
public static void createFileList(File originalZipFile, File targetFileList) throws IOException {
    ZipFile zip = new ZipFile(originalZipFile);
    try {
        FileOutputStream output = new FileOutputStream(targetFileList);
        try {
            FileList.Writer writer = FileList.createWriter(output, true);
            Enumeration<? extends ZipEntry> entries = zip.entries();
            while (entries.hasMoreElements()) {
                ZipEntry next = entries.nextElement();
                InputStream input = zip.getInputStream(next);
                try {
                    OutputStream target = writer.openNext(FileList.content(next.getName().replace('\\', '/')));
                    try {
                        IOUtils.pipingAndClose(input, target);
                    } finally {
                        target.close();
                    }
                } finally {
                    input.close();
                }
            }
            writer.close();
        } finally {
            output.close();
        }
    } finally {
        zip.close();
    }
}

From source file:com.microsoft.intellij.helpers.IDEHelperImpl.java

private static void copyJarFiles(@NotNull final Module module, @NotNull VirtualFile baseDir,
        @NotNull File zipFile, @NotNull String zipPath) throws IOException {
    if (baseDir.isDirectory()) {
        final ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();

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

            if (!zipEntry.isDirectory() && zipEntry.getName().startsWith(zipPath)
                    && zipEntry.getName().endsWith(".jar") && !(zipEntry.getName().endsWith("-sources.jar")
                            || zipEntry.getName().endsWith("-javadoc.jar"))) {
                VirtualFile libsVf = null;

                for (VirtualFile vf : baseDir.getChildren()) {
                    if (vf.getName().equals("libs")) {
                        libsVf = vf;//from www .j ava 2s  .co  m
                        break;
                    }
                }

                if (libsVf == null) {
                    libsVf = baseDir.createChildDirectory(module.getProject(), "libs");
                }

                final VirtualFile libs = libsVf;
                final String fileName = zipEntry.getName().split("/")[1];

                if (libs.findChild(fileName) == null) {
                    ApplicationManager.getApplication().invokeAndWait(new Runnable() {
                        @Override
                        public void run() {
                            ApplicationManager.getApplication().runWriteAction(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        InputStream mobileserviceInputStream = zip.getInputStream(zipEntry);
                                        VirtualFile msVF = libs.createChildData(module.getProject(), fileName);
                                        msVF.setBinaryContent(getArray(mobileserviceInputStream));
                                    } catch (Throwable ex) {
                                        DefaultLoader.getUIHelper().showException(
                                                "An error occurred while attempting "
                                                        + "to configure Azure Mobile Services.",
                                                ex,
                                                "Azure Services Explorer - Error Configuring Mobile Services",
                                                false, true);
                                    }
                                }
                            });
                        }
                    }, ModalityState.defaultModalityState());
                }
            }
        }
    }
}

From source file:org.eclipse.koneki.ldt.core.internal.buildpath.LuaExecutionEnvironmentManager.java

public static LuaExecutionEnvironment getExecutionEnvironmentFromCompressedFile(final String filePath)
        throws CoreException {
    /*//  w w  w .j  ava  2  s .  c om
     * Extract manifest file
     */
    ZipFile zipFile = null;
    String manifestString = null;
    try {
        zipFile = new ZipFile(filePath);
        final Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
        while (zipEntries.hasMoreElements()) {
            final ZipEntry zipEntry = zipEntries.nextElement();
            if ((!zipEntry.getName().contains("/")) //$NON-NLS-1$
                    && zipEntry.getName().endsWith(LuaExecutionEnvironmentConstants.MANIFEST_EXTENSION)) {

                // check there are only one manifest.
                if (manifestString != null) {
                    throwException(
                            MessageFormat.format("Invalid Execution Environment : more than one \"{0}\" file.", //$NON-NLS-1$
                                    LuaExecutionEnvironmentConstants.MANIFEST_EXTENSION),
                            null, IStatus.ERROR);
                }

                // read manifest
                final InputStream input = zipFile.getInputStream(zipEntry);
                manifestString = IOUtils.toString(input);
            }
        }
    } catch (IOException e) {
        throwException(MessageFormat.format("Unable to extract manifest from zip file {0}", filePath), e, //$NON-NLS-1$
                IStatus.ERROR);
    } finally {
        if (zipFile != null)
            try {
                zipFile.close();
            } catch (IOException e) {
                Activator.logWarning(MessageFormat.format("Unable to close zip file {0}", filePath), e); //$NON-NLS-1$
            }
    }

    // if no manifest extract
    if (manifestString == null) {
        throwException(MessageFormat.format("No manifest \"{0}\" file found", //$NON-NLS-1$
                LuaExecutionEnvironmentConstants.MANIFEST_EXTENSION), null, IStatus.ERROR);
    }

    return getLuaExecutionEnvironmentFromManifest(manifestString, null);
}

From source file:lucee.commons.io.compress.CompressUtil.java

private static void unzip2(File zipFile, Resource targetDir) throws IOException {
    ZipFile zf = null;
    try {//  w ww .j  ava 2s.  c  o m
        zf = new ZipFile(zipFile);

        ZipEntry entry;
        Enumeration en = zf.entries();
        while (en.hasMoreElements()) {
            entry = (ZipEntry) en.nextElement();
            Resource target = targetDir.getRealResource(entry.getName());
            if (entry.isDirectory()) {
                target.mkdirs();
            } else {
                Resource parent = target.getParentResource();
                if (!parent.exists())
                    parent.mkdirs();
                InputStream is = zf.getInputStream(entry);
                IOUtil.copy(is, target, true);
            }
            target.setLastModified(entry.getTime());
        }
    } finally {
        IOUtil.closeEL(zf);
    }
}

From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java

/**
 * returns the {@link InputStream} for the file with given name in the given zipped file
 *//*from   w  ww. jav  a  2s .  c om*/
public static List<String> getFilesNamesFromZip(final URL zipFileURL) throws IOException, URISyntaxException {
    final ZipFile zf = new ZipFile(new File(zipFileURL.toURI()));
    List<String> listRes = new ArrayList<>();
    final Enumeration<?> entries = zf.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry ze = (ZipEntry) entries.nextElement();
        if (!ze.isDirectory())
            listRes.add(ze.getName());
    }

    return listRes;
}

From source file:org.jaggeryjs.jaggery.core.manager.JaggeryDeployerManager.java

private static JSONObject readJaggeryConfig(Context context, Path appBase) {
    String content = null;//from w  ww  .j  a  v  a  2s.c o m
    String path = null;
    if (context.getDocBase().contains(WAR_EXTENSION)) {
        try {
            if (!appBase.endsWith("/")) {
                path = appBase + File.separator + context.getDocBase();
            } else {
                path = appBase + context.getDocBase();
            }
            ZipFile zip = new ZipFile(path);
            for (Enumeration e = zip.entries(); e.hasMoreElements();) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                if (entry.getName().toLowerCase().contains(JAGGERY_CONF)) {
                    InputStream inputStream = zip.getInputStream(entry);
                    content = IOUtils.toString(inputStream);
                }
            }
        } catch (IOException e) {
            log.error("Error occuered when the accessing the jaggery.conf file of "
                    + context.getPath().substring(1), e);
        }
    } else {
        File file = new File(appBase + context.getPath() + File.separator + JAGGERY_CONF);
        try {
            content = FileUtils.readFileToString(file);
        } catch (IOException e) {
            log.error("IOException is thrown when accessing the jaggery.conf file of "
                    + context.getPath().substring(1), e);
        }
    }
    JSONObject jaggeryConfig = null;
    try {
        JSONParser jp = new JSONParser();
        jaggeryConfig = (JSONObject) jp.parse(content);
    } catch (ParseException e) {
        log.error("Error in parsing the jaggery.conf file", e);
    }
    return jaggeryConfig;
}

From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java

public static void unpack(final ZipFile zf, final File targetDir) throws ZipException, IOException {
    targetDir.mkdir();//  w w w  . java 2  s .  co m

    for (final Enumeration<? extends ZipEntry> e = zf.entries(); e.hasMoreElements();) {
        final ZipEntry target = e.nextElement();
        System.out.print(target.getName() + " ."); //$NON-NLS-1$
        saveEntry(zf, targetDir, target);
        System.out.println(". unpacked"); //$NON-NLS-1$
    }
    zf.close();
}

From source file:net.rptools.lib.FileUtil.java

public static void unzipFile(File sourceFile, File destDir) throws IOException {
    if (!sourceFile.exists())
        throw new IOException("source file does not exist: " + sourceFile);

    ZipFile zipFile = null;
    InputStream is = null;//w  w w . j  a v  a2  s .c  o  m
    OutputStream os = null;
    try {
        zipFile = new ZipFile(sourceFile);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();

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

            File file = new File(destDir, entry.getName());
            String path = file.getAbsolutePath();
            file.getParentFile().mkdirs();

            //System.out.println("Writing file: " + path);
            is = zipFile.getInputStream(entry);
            os = new BufferedOutputStream(new FileOutputStream(path));
            copyWithClose(is, os);
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
        try {
            if (zipFile != null)
                zipFile.close();
        } catch (Exception e) {
        }
    }
}