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.geoserver.data.util.IOUtils.java

public static void decompress(final File inputFile, final File destDir) throws IOException {
    ZipFile zipFile = new ZipFile(inputFile);

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

    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        InputStream stream = zipFile.getInputStream(entry);

        if (entry.isDirectory()) {
            // Assume directories are stored parents first then children.
            (new File(destDir, entry.getName())).mkdir();
            continue;
        }/*w w  w  .  j ava2 s .co m*/

        File newFile = new File(destDir, entry.getName());
        FileOutputStream fos = new FileOutputStream(newFile);
        try {
            byte[] buf = new byte[1024];
            int len;

            while ((len = stream.read(buf)) >= 0)
                saveCompressedStream(buf, fos, len);

        } catch (IOException e) {
            zipFile.close();
            IOException ioe = new IOException("Not valid COAMPS archive file type.");
            ioe.initCause(e);
            throw ioe;
        } finally {
            fos.flush();
            fos.close();

            stream.close();
        }
    }
    zipFile.close();
}

From source file:nz.ac.otago.psyanlab.common.util.FileUtils.java

/**
 * Inflate a pale file to a given working directory.
 * /*  w ww .  jav  a 2  s .  co m*/
 * @param paleFile .pale file to inflate.
 * @param workingDir Path to extract to.
 * @return Path of the location the file was extracted to.
 * @throws FileNotFoundException
 * @throws IOException
 */
public static File decompress(File paleFile, File workingDir) throws FileNotFoundException, IOException {
    ZipFile archive = new ZipFile(paleFile);

    try {
        // Iterate over elements in zip file and extract them to working
        // directory.
        Enumeration<? extends ZipEntry> entries = archive.entries();
        while (entries.hasMoreElements()) {
            ZipEntry ze = entries.nextElement();
            File newFile = new File(workingDir, ze.getName());

            if (ze.isDirectory()) {
                if (!newFile.exists()) {
                    newFile.mkdirs();
                }
                continue;
            }

            if (!newFile.getParentFile().exists()) {
                // Dir tree missing for some reason so create it.
                newFile.getParentFile().mkdirs();
            }

            InputStream in = archive.getInputStream(ze);
            OutputStream out = new BufferedOutputStream(new FileOutputStream(newFile));

            try {
                copy(in, out);
            } finally {
                in.close();
                out.close();
            }
        }
    } finally {
        archive.close();
    }

    return workingDir;
}

From source file:com.cenrise.test.azkaban.Utils.java

public static void unzip(final ZipFile source, final File dest) throws IOException {
    final Enumeration<?> entries = source.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = (ZipEntry) entries.nextElement();
        final File newFile = new File(dest, entry.getName());
        if (entry.isDirectory()) {
            newFile.mkdirs();// ww w. j  a  v  a2s  . c o  m
        } else {
            newFile.getParentFile().mkdirs();
            final InputStream src = source.getInputStream(entry);
            try {
                final OutputStream output = new BufferedOutputStream(new FileOutputStream(newFile));
                try {
                    IOUtils.copy(src, output);
                } finally {
                    output.close();
                }
            } finally {
                src.close();
            }
        }
    }
}

From source file:it.geosolutions.tools.compress.file.Extractor.java

/**
 * Inflate the provided {@link ZipFile} in the provided output directory.
 * /*  ww  w .  j a va2s. com*/
 * @param archive
 *            the {@link ZipFile} to inflate.
 * @param outputDirectory
 *            the directory where to inflate the archive.
 * @throws IOException
 *             in case something bad happens.
 * @throws FileNotFoundException
 *             in case something bad happens.
 */
public static void inflate(ZipFile archive, File outputDirectory, String fileName)
        throws IOException, FileNotFoundException {

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

            if (!entry.isDirectory()) {
                final String name = entry.getName();
                final String ext = FilenameUtils.getExtension(name);
                final InputStream in = new BufferedInputStream(archive.getInputStream(entry));
                final File outFile = new File(outputDirectory,
                        fileName != null ? new StringBuilder(fileName).append(".").append(ext).toString()
                                : name);
                final OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));

                IOUtils.copyStream(in, out, true, true);
            }
        }
    } finally {
        try {
            archive.close();
        } catch (Throwable e) {
            if (LOGGER.isTraceEnabled())
                LOGGER.error("unable to close archive.\nMessage is: " + e.getMessage(), e);
        }
    }

}

From source file:com.fluidops.iwb.luxid.LuxidExtractor.java

/**
 * extracts a zip-file and returns references to the unziped files. If the file passed to this method
 * is not a zip-file, a reference to the file is returned.
 * // ww w.  j a v a  2 s  .c  o  m
 * @param fileName
 * @return
 * @throws Exception
 */
public static Set<File> extractZip(String fileName) throws Exception {
    File zipf = new File((new StringBuilder("luxid/")).append(fileName).toString());

    Set<File> toBeUploaded = new HashSet<File>();
    if (zipf.getName().endsWith(".zip")) {
        ZipFile zip = new ZipFile(zipf);
        for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            if (entry.isDirectory()) {
                logger.info((new StringBuilder("Extracting directory: ")).append(entry.getName()).toString());
                GenUtil.mkdir(new File(entry.getName()));
            } else {
                logger.info((new StringBuilder("Extracting file: ")).append(entry.getName()).toString());
                String entryPath = "luxid/" + entry.getName();
                FileOutputStream fileOutputStream = null;
                InputStream zipEntryStream = zip.getInputStream(entry);
                try {
                    fileOutputStream = new FileOutputStream(entryPath);
                    IOUtils.copy(zipEntryStream, fileOutputStream);
                } finally {
                    closeQuietly(zipEntryStream);
                    closeQuietly(fileOutputStream);
                }
                toBeUploaded.add(new File(entryPath));
            }
        }

        zip.close();
    } else {
        toBeUploaded.add(zipf);
    }

    return toBeUploaded;
}

From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java

/**
 * Extracts the packageFile and writes its content in temporary directory
 *
 * @param packageFile the packageFile (zip format)
 * @param tempDir the tempDir directory/*ww  w .j a v a 2s . c  o  m*/
 */
public static void extractPackage(File packageFile, File tempDir) throws SoundPackageException {

    if (packageFile == null) {
        throw new SoundPackageException(new IllegalArgumentException("missing package file"));
    }

    ZipFile packageZip = null;
    try {
        packageZip = new ZipFile(packageFile);
        Enumeration<?> entries = packageZip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String entryName = entry.getName();
            if (log.isDebugEnabled()) {
                log.debug("ZipEntry name: " + entryName);
            }
            if (entry.isDirectory()) {
                File dir = new File(tempDir, entryName);
                if (dir.mkdirs())
                    log.info("successfully created dir: " + dir.getAbsolutePath());
            } else {
                FileUtil.createFileFromInputStream(getPackageEntryStream(packageFile, entryName),
                        tempDir + File.separator + entryName);
            }
        }
    } catch (FileNotFoundException ex) {
        throw new SoundPackageException(ex);
    } catch (IOException ex) {
        throw new SoundPackageException(ex);
    } finally {
        try {
            if (packageZip != null)
                packageZip.close();
        } catch (IOException ex) {
            log.error(ex.getMessage(), ex);
        }
    }
}

From source file:org.eclipse.thym.core.internal.util.FileUtils.java

private static void copyFromZip(ZipFile zipFile, String locationInBundle, File destination) throws IOException {

    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        if (zipEntry.getName().startsWith(locationInBundle)) {

            String path = zipEntry.getName().substring(locationInBundle.length());
            File file = new File(destination, path);

            if (!zipEntry.isDirectory()) {
                createFileFromZipFile(file, zipFile, zipEntry);
            } else {
                if (!file.exists()) {
                    file.mkdir();/*from  ww w .j a  v a  2s .c o m*/
                }
            }
        }
    }

}

From source file:azkaban.common.utils.Utils.java

public static void unzip(ZipFile source, File dest) throws IOException {
    Enumeration<?> entries = source.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        File newFile = new File(dest, entry.getName());
        if (entry.isDirectory()) {
            newFile.mkdirs();/*  w w  w .ja  va  2 s  .  c o m*/
        } else {
            newFile.getParentFile().mkdirs();
            InputStream src = source.getInputStream(entry);
            OutputStream output = new BufferedOutputStream(new FileOutputStream(newFile));
            IOUtils.copy(src, output);
            src.close();
            output.close();
        }
    }
}

From source file:gdt.data.entity.facet.ExtensionHandler.java

public static InputStream getResourceStream(String jar$, String resource$) {
    try {//from ww  w. ja  v  a2s.  c om
        //      System.out.println("ExtensionHandler:loadIcon:jar="+jar$);
        ZipFile zf = new ZipFile(jar$);
        Enumeration<? extends ZipEntry> entries = zf.entries();
        ZipEntry ze;
        String[] sa;
        while (entries.hasMoreElements()) {
            try {
                ze = entries.nextElement();
                sa = ze.getName().split("/");
                //     System.out.println("ExtensionHandler:loadIcon:zip entry="+sa[sa.length-1]);
                if (resource$.equals(sa[sa.length - 1])) {
                    InputStream is = zf.getInputStream(ze);
                    if (is != null)
                        return is;

                }
            } catch (Exception e) {

            }
        }
        return null;
    } catch (Exception e) {
        Logger.getLogger(ExtensionHandler.class.getName()).severe(e.toString());
        return null;
    }

}

From source file:gdt.data.entity.facet.ExtensionHandler.java

public static String[] listResourcesByType(String jar$, String type$) {
    try {// w  w  w .  jav a  2  s . c om

        ArrayList<String> sl = new ArrayList<String>();
        ZipFile zf = new ZipFile(jar$);
        Enumeration<? extends ZipEntry> entries = zf.entries();
        ZipEntry ze;
        String[] sa;
        String resource$;
        while (entries.hasMoreElements()) {
            try {
                ze = entries.nextElement();
                sa = ze.getName().split("/");
                resource$ = sa[sa.length - 1];
                //     System.out.println("ExtensionHandler:loadIcon:zip entry="+sa[sa.length-1]);
                if (type$.equals(FileExpert.getExtension(resource$))) {
                    sl.add(resource$);
                }
            } catch (Exception e) {

            }
        }
        return sl.toArray(new String[0]);
    } catch (Exception e) {
        Logger.getLogger(ExtensionHandler.class.getName()).severe(e.toString());
        return null;
    }

}