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.kie.guvnor.m2repo.backend.server.GuvnorM2Repository.java

public File appendPOMToJar(String pom, String jarPath, GAV gav) {
    File originalJarFile = new File(jarPath);
    File appendedJarFile = new File(jarPath + ".tmp");

    try {/*from  w  w  w. ja  va  2  s  .co  m*/
        ZipFile war = new ZipFile(originalJarFile);

        ZipOutputStream append = new ZipOutputStream(new FileOutputStream(appendedJarFile));

        // first, copy contents from existing war
        Enumeration<? extends ZipEntry> entries = war.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            // System.out.println("copy: " + e.getName());
            append.putNextEntry(e);
            if (!e.isDirectory()) {
                IOUtil.copy(war.getInputStream(e), append);
            }
            append.closeEntry();
        }

        // append pom.
        ZipEntry e = new ZipEntry(getPomXmlPath(gav));
        // System.out.println("append: " + e.getName());
        append.putNextEntry(e);
        append.write(pom.getBytes());
        append.closeEntry();

        // close
        war.close();
        append.close();
    } catch (ZipException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //originalJarFile.delete();
    //appendedJarFile.renameTo(originalJarFile);
    return appendedJarFile;
}

From source file:org.commonjava.maven.galley.filearc.internal.ZipPublish.java

private Boolean rewriteArchive(final File dest, final String path) {
    final boolean isJar = isJarOperation();
    final File src = new File(dest.getParentFile(), dest.getName() + ".old");
    dest.renameTo(src);//from  w w w . java  2s . com

    InputStream zin = null;
    ZipFile zfIn = null;

    FileOutputStream fos = null;
    ZipOutputStream zos = null;
    ZipFile zfOut = null;
    try {
        fos = new FileOutputStream(dest);
        zos = isJar ? new JarOutputStream(fos) : new ZipOutputStream(fos);

        zfOut = isJar ? new JarFile(dest) : new ZipFile(dest);
        zfIn = isJar ? new JarFile(src) : new ZipFile(src);

        for (final Enumeration<? extends ZipEntry> en = zfIn.entries(); en.hasMoreElements();) {
            final ZipEntry inEntry = en.nextElement();
            final String inPath = inEntry.getName();
            try {
                if (inPath.equals(path)) {
                    zin = stream;
                } else {
                    zin = zfIn.getInputStream(inEntry);
                }

                final ZipEntry entry = zfOut.getEntry(inPath);
                zos.putNextEntry(entry);
                copy(stream, zos);
            } finally {
                closeQuietly(zin);
            }
        }

        return true;
    } catch (final IOException e) {
        error = new TransferException("Failed to write path: %s to EXISTING archive: %s. Reason: %s", e, path,
                dest, e.getMessage());
    } finally {
        closeQuietly(zos);
        closeQuietly(fos);
        if (zfOut != null) {
            //noinspection EmptyCatchBlock
            try {
                zfOut.close();
            } catch (final IOException e) {
            }
        }

        if (zfIn != null) {
            //noinspection EmptyCatchBlock
            try {
                zfIn.close();
            } catch (final IOException e) {
            }
        }

        closeQuietly(stream);
    }

    return false;
}

From source file:com.github.promeg.configchecker.Main.java

/**
 * Tries to open an input file as a Zip archive (jar/apk) with a
 * "classes.dex" inside./*from   www.j a  v  a 2s. co m*/
 */
void openInputFileAsZip(String fileName, List<String> dexFiles) throws IOException {
    ZipFile zipFile;

    // Try it as a zip file.
    try {
        zipFile = new ZipFile(fileName);
    } catch (FileNotFoundException fnfe) {
        // not found, no point in retrying as non-zip.
        System.err.println("Unable to open '" + fileName + "': " + fnfe.getMessage());
        throw fnfe;
    } catch (ZipException ze) {
        // not a zip
        return;
    }

    // Open and add all files matching "classes.*\.dex" in the zip file.
    for (ZipEntry entry : Collections.list(zipFile.entries())) {
        if (entry.getName().matches("classes.*\\.dex")) {
            dexFiles.add(openDexFile(zipFile, entry).getAbsolutePath());
        }
    }

    zipFile.close();
}

From source file:com.openAtlas.bundleInfo.maker.PackageLite.java

private List<String> getFileList(ZipFile zipFile, String mPref, String mSuffix) {
    List<String> arrayList = new ArrayList<String>();
    try {/*from   w  ww  .  jav a 2 s.  co m*/
        Enumeration<?> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            String name = ((ZipEntry) entries.nextElement()).getName();
            if (name.startsWith(mPref) && name.endsWith(mSuffix)) {
                arrayList.add(name);
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return arrayList;
}

From source file:com.cisco.ca.cstg.pdi.services.ConfigurationServiceImpl.java

private boolean validateZipFile(File file) throws IOException {
    boolean isMetaDataPresent = STATUS_FALSE;
    boolean isConfigFilePresent = STATUS_FALSE;
    ZipFile zipFile = null;

    try {//from  w w  w. ja  v a  2s .  c  o m
        zipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();

            if (zipEntry.getName().equals(Constants.PDI_CONFIG_FILE_NAME)) {
                isConfigFilePresent = STATUS_TRUE;
            } else if (zipEntry.getName().equals(Constants.PDI_META_DATA_FILE_NAME)) {
                isMetaDataPresent = STATUS_TRUE;
            }

        }
    } catch (IOException e) {
        LOGGER.error("Error occured while trying to validate zip files.", e);
        throw e;
    } finally {
        if (zipFile != null) {
            zipFile.close();
        }
    }

    if (isMetaDataPresent && isConfigFilePresent) {
        return STATUS_TRUE;
    }

    return STATUS_FALSE;
}

From source file:maltcms.ui.nb.pipelineRunner.options.LocalMaltcmsExecutionPanel.java

public void unzipArchive(File archive, File outputDir) {
    try {/*from  ww w.  j a v a  2  s  . c o  m*/
        ZipFile zipfile = new ZipFile(archive);
        for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            unzipEntry(zipfile, entry, outputDir);
        }
    } catch (Exception e) {
        Exceptions.printStackTrace(e);
    }
}

From source file:org.adempiere.webui.install.WTranslationDialog.java

private void unZipAndProcess(InputStream istream) throws AdempiereException {
    FileOutputStream ostream = null;
    File file = null;//  w w w  .  j  a v a  2  s . c  om
    try {
        file = File.createTempFile("trlImport", ".zip");
        ostream = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = istream.read(buffer)) != -1) {
            ostream.write(buffer, 0, read);
        }
    } catch (Throwable e) {
        throw new AdempiereException("Copy zip failed", e);
    } finally {
        if (ostream != null) {
            try {
                ostream.close();
            } catch (Exception e2) {
            }
        }
    }

    // create temp folder
    File tempfolder;
    try {
        tempfolder = File.createTempFile(m_AD_Language.getValue(), ".trl");
        tempfolder.delete();
        tempfolder.mkdir();
    } catch (IOException e1) {
        throw new AdempiereException("Problem creating temp folder", e1);
    }

    String suffix = "_" + m_AD_Language.getValue() + ".xml";
    ZipFile zipFile = null;
    boolean validfile = false;
    try {
        zipFile = new ZipFile(file);

        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();

            if (entry.isDirectory()) {
                // ignore folders
                log.warning("Imported zip must not contain folders, ignored folder" + entry.getName());
                continue;
            }

            if (!entry.getName().endsWith(suffix)) {
                // not valid file
                log.warning("Ignored file " + entry.getName());
                continue;
            }

            if (log.isLoggable(Level.INFO))
                log.info("Extracting file: " + entry.getName());
            copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(
                    new FileOutputStream(tempfolder.getPath() + File.separator + entry.getName())));
            validfile = true;
        }
    } catch (Throwable e) {
        throw new AdempiereException("Uncompress zip failed", e);
    } finally {
        if (zipFile != null)
            try {
                zipFile.close();
            } catch (IOException e) {
            }
    }

    if (!validfile) {
        throw new AdempiereException("ZIP file invalid, doesn't contain *" + suffix + " files");
    }

    callImportProcess(tempfolder.getPath());

    file.delete();
    try {
        FileUtils.deleteDirectory(tempfolder);
    } catch (IOException e) {
    }
}

From source file:maltcms.ui.nb.pipelineRunner.options.LocalMaltcmsExecutionPanel.java

public String getTopLevelArchiveDirectoryName(File archive) {
    try {/*from ww  w  . j  a v a 2  s  .  c o m*/
        ZipFile zipfile = new ZipFile(archive);
        for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            if (entry.isDirectory()) {
                return entry.getName();
            }
        }
    } catch (Exception e) {
        Exceptions.printStackTrace(e);
    }
    return null;
}

From source file:com.dsh105.commodus.data.Updater.java

/**
 * Part of Zip-File-Extractor, modified by Gravity for use with Bukkit
 *///from   ww  w  . j av a2 s  . c o m
private void unzip(String file) {
    try {
        final File fSourceZip = new File(file);
        final String zipPath = file.substring(0, file.length() - 4);
        ZipFile zipFile = new ZipFile(fSourceZip);
        Enumeration<? extends ZipEntry> e = zipFile.entries();
        while (e.hasMoreElements()) {
            ZipEntry entry = e.nextElement();
            File destinationFilePath = new File(zipPath, entry.getName());
            destinationFilePath.getParentFile().mkdirs();
            if (entry.isDirectory()) {
                continue;
            } else {
                final BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
                int b;
                final byte buffer[] = new byte[Updater.BYTE_SIZE];
                final FileOutputStream fos = new FileOutputStream(destinationFilePath);
                final BufferedOutputStream bos = new BufferedOutputStream(fos, Updater.BYTE_SIZE);
                while ((b = bis.read(buffer, 0, Updater.BYTE_SIZE)) != -1) {
                    bos.write(buffer, 0, b);
                }
                bos.flush();
                bos.close();
                bis.close();
                final String name = destinationFilePath.getName();
                if (name.endsWith(".jar") && this.pluginFile(name)) {
                    destinationFilePath.renameTo(
                            new File(this.plugin.getDataFolder().getParent(), this.updateFolder + "/" + name));
                }
            }
            entry = null;
            destinationFilePath = null;
        }
        e = null;
        zipFile.close();
        zipFile = null;

        // Move any plugin data folders that were included to the right place, Bukkit won't do this for us.
        for (final File dFile : new File(zipPath).listFiles()) {
            if (dFile.isDirectory()) {
                if (this.pluginFile(dFile.getName())) {
                    final File oFile = new File(this.plugin.getDataFolder().getParent(), dFile.getName()); // Get current dir
                    final File[] contents = oFile.listFiles(); // List of existing files in the current dir
                    for (final File cFile : dFile.listFiles()) // Loop through all the files in the new dir
                    {
                        boolean found = false;
                        for (final File xFile : contents) // Loop through contents to see if it exists
                        {
                            if (xFile.getName().equals(cFile.getName())) {
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            // Move the new file into the current dir
                            cFile.renameTo(new File(oFile.getCanonicalFile() + "/" + cFile.getName()));
                        } else {
                            // This file already exists, so we don't need it anymore.
                            cFile.delete();
                        }
                    }
                }
            }
            dFile.delete();
        }
        new File(zipPath).delete();
        fSourceZip.delete();
    } catch (final IOException ex) {
        this.plugin.getLogger()
                .warning("The auto-updater tried to unzip a new update file, but was unsuccessful.");
        this.result = Updater.UpdateResult.FAIL_DOWNLOAD;
        ex.printStackTrace();
    }
    new File(file).delete();
}

From source file:com.pari.mw.api.execute.reports.template.ReportTemplateRunner.java

private InputStream getInputStream(ZipFile zipFile, String entryFileName) throws Exception {
    ZipEntry topLevelFolder = null;
    ZipEntry specificEntry = null;
    InputStream ipStream = null;//from w w  w . j  av a 2s. c o  m

    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        topLevelFolder = entries.nextElement();
        if (topLevelFolder.isDirectory()) {
            break;
        }
    }

    if (topLevelFolder != null) {
        String lookupFile = topLevelFolder + entryFileName;
        specificEntry = zipFile.getEntry(lookupFile);
    }

    if (specificEntry != null) {
        ipStream = zipFile.getInputStream(specificEntry);
    }

    return ipStream;
}