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:asciidoc.maven.plugin.tools.ZipHelper.java

public File unzipEntry(File _zipFile, String _name) {
    File directory = null;/*from w  ww .jav a2  s  .  co  m*/
    try {
        if (this.log.isDebugEnabled())
            this.log.debug("zip file: " + _zipFile.getAbsolutePath());
        ZipEntry zipEntry = null;
        String zipEntryName = null;
        ZipFile jarZipFile = new ZipFile(_zipFile);
        Enumeration<? extends ZipEntry> e = jarZipFile.entries();
        while (e.hasMoreElements()) {
            zipEntry = (ZipEntry) e.nextElement();
            zipEntryName = zipEntry.getName();
            if (zipEntryName.startsWith(_name) && zipEntryName.endsWith(".zip")) {
                if (this.log.isInfoEnabled())
                    this.log.info("Found in " + zipEntryName);
                directory = new File(_zipFile.getParent(), FilenameUtils.removeExtension(zipEntryName));
                break;
            }
        }
        if (directory != null && !directory.exists()) {
            unzipEntry(jarZipFile, zipEntry, _zipFile.getParentFile());
            File asciiDocArchive = new File(_zipFile.getParent(), zipEntryName);
            unzipArchive(asciiDocArchive, _zipFile.getParentFile());
            asciiDocArchive.deleteOnExit();
        }

    } catch (ZipException ze) {
        this.log.error(ze.getMessage(), ze);
    } catch (IOException ioe) {
        this.log.error(ioe.getMessage(), ioe);
    }
    return directory;
}

From source file:FeatureExtraction.FeatureExtractorOOXMLStructuralPaths.java

private void ExtractStructuralFeaturesInMemory(String filePath, Map<String, Integer> structuralPaths) {
    String path;/*from  w  w  w  .j  a va 2  s  .  c om*/
    String directoryPath;
    String fileExtension;
    try {
        ZipFile zipFile = new ZipFile(filePath);

        for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            path = entry.getName();
            directoryPath = FilenameUtils.getFullPath(path);
            fileExtension = FilenameUtils.getExtension(path);

            AddStructuralPath(directoryPath, structuralPaths);
            AddStructuralPath(path, structuralPaths);

            if (fileExtension.equals("rels") || fileExtension.equals("xml")) {
                AddXMLStructuralPaths(zipFile.getInputStream(entry), path, structuralPaths);
            }
        }
    } catch (IOException ex) {
        Console.PrintException(
                String.format("Error extracting OOXML structural features from file: %s", filePath), ex);
    }
}

From source file:com.alibaba.jstorm.yarn.utils.JStormUtils.java

/**
 * Extra dir from the jar to destdir//  ww  w  .  j  a  v a 2 s  .co m
 *
 * @param jarpath
 * @param dir
 * @param destdir
 */
public static void extractDirFromJar(String jarpath, String dir, String destdir) {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(jarpath);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries != null && entries.hasMoreElements()) {
            ZipEntry ze = entries.nextElement();
            if (!ze.isDirectory() && ze.getName().startsWith(dir)) {
                InputStream in = zipFile.getInputStream(ze);
                try {
                    File file = new File(destdir, ze.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    if (in != null)
                        in.close();
                }
            }
        }
    } catch (Exception e) {
        LOG.warn("No " + dir + " from " + jarpath + "!\n" + e.getMessage());
    } finally {
        if (zipFile != null)
            try {
                zipFile.close();
            } catch (Exception e) {
                LOG.warn(e.getMessage());
            }

    }
}

From source file:edu.stanford.epad.common.util.EPADFileUtils.java

/**
 * Unzip the specified file.//from ww w.  j ava2 s.  c  o  m
 * 
 * @param zipFilePath String path to zip file.
 * @throws IOException during zip or read process.
 */
public static void extractFolder(String zipFilePath) throws IOException {
    ZipFile zipFile = null;

    try {
        int BUFFER = 2048;
        File file = new File(zipFilePath);

        zipFile = new ZipFile(file);
        String newPath = zipFilePath.substring(0, zipFilePath.length() - 4);

        makeDirs(new File(newPath));
        Enumeration<?> zipFileEntries = zipFile.entries();

        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            File destFile = new File(newPath, currentEntry);
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            makeDirs(destinationParent);

            InputStream is = null;
            BufferedInputStream bis = null;
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;

            try {
                if (destFile.exists() && destFile.isDirectory())
                    continue;

                if (!entry.isDirectory()) {
                    int currentByte;
                    byte data[] = new byte[BUFFER];
                    is = zipFile.getInputStream(entry);
                    bis = new BufferedInputStream(is);
                    fos = new FileOutputStream(destFile);
                    bos = new BufferedOutputStream(fos, BUFFER);

                    while ((currentByte = bis.read(data, 0, BUFFER)) != -1) {
                        bos.write(data, 0, currentByte);
                    }
                    bos.flush();
                }
            } finally {
                IOUtils.closeQuietly(bis);
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(bos);
                IOUtils.closeQuietly(fos);
            }

            if (currentEntry.endsWith(".zip")) {
                extractFolder(destFile.getAbsolutePath());
            }
        }
    } catch (Exception e) {
        log.warning("Failed to unzip: " + zipFilePath, e);
        throw new IllegalStateException(e);
    } finally {
        if (zipFile != null)
            zipFile.close();
    }
}

From source file:gui.accessories.DownloadProgressWork.java

/**
 * Uncompress all the files contained in the compressed file and its folders in the same folder where the zip file is placed. Doesn't respects the
 * directory tree of the zip file. This method seems a clear candidate to ZipManager.
 *
 * @param file Zip file./*from w ww  .  j  av  a  2  s .c om*/
 * @return Count of files uncopressed.
 * @throws ZipException Exception.
 */
private int doUncompressZip(File file) throws ZipException {
    int fileCount = 0;
    try {
        byte[] buf = new byte[1024];
        ZipFile zipFile = new ZipFile(file);

        Enumeration zipFileEntries = zipFile.entries();

        while (zipFileEntries.hasMoreElements()) {
            ZipEntry zipentry = (ZipEntry) zipFileEntries.nextElement();
            if (zipentry.isDirectory()) {
                continue;
            }
            File entryZipFile = new File(zipentry.getName());
            File outputFile = new File(file.getParentFile(), entryZipFile.getName());
            FileOutputStream fileoutputstream = new FileOutputStream(outputFile);
            InputStream is = zipFile.getInputStream(zipentry);
            int n;
            while ((n = is.read(buf, 0, 1024)) > -1) {
                fileoutputstream.write(buf, 0, n);
            }
            fileoutputstream.close();
            is.close();
            fileoutputstream.close();
            fileCount++;
        }
        zipFile.close();

    } catch (IOException ex) {
        throw new ZipException(ex.getMessage());
    }

    return fileCount;
}

From source file:net.sourceforge.pmd.it.BinaryDistributionIT.java

@Test
public void testZipFileContent() throws IOException {
    Set<String> expectedFileNames = getExpectedFileNames();

    ZipFile zip = new ZipFile(getBinaryDistribution());

    Enumeration<? extends ZipEntry> entries = zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        expectedFileNames.remove(entry.getName());
    }// ww  w  .ja v  a  2s  .  c  o m

    zip.close();

    if (!expectedFileNames.isEmpty()) {
        fail("Missing files in archive: " + expectedFileNames);
    }
}

From source file:FeatureExtraction.FeatureExtractorOOXMLStructuralPathsInputStream.java

private void ExtractStructuralFeaturesInMemory(InputStream fileInputStream,
        Map<String, Integer> structuralPaths) {
    String path;/*from w  w w .ja  va  2  s .c o m*/
    String directoryPath;
    String fileExtension;
    try {
        File file = new File(fileInputStream.hashCode() + "");
        try (FileOutputStream fos = new FileOutputStream(file)) {
            IOUtils.copy(fileInputStream, fos);
        }

        ZipFile zipFile = new ZipFile(file);

        for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            path = entry.getName();
            directoryPath = FilenameUtils.getFullPath(path);
            fileExtension = FilenameUtils.getExtension(path);

            AddStructuralPath(directoryPath, structuralPaths);
            AddStructuralPath(path, structuralPaths);

            if (fileExtension.equals("rels") || fileExtension.equals("xml")) {
                AddXMLStructuralPaths(zipFile.getInputStream(entry), path, structuralPaths);
            }
        }
    } catch (IOException ex) {
        Console.PrintException(String.format("Error extracting OOXML structural features from file in memory"),
                ex);
    }
}

From source file:no.uio.medicine.virsurveillance.parsers.CSVsGBDdata.java

public void zip() throws IOException {
    ZipFile zipFile = new ZipFile("C:/test.zip");

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

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        InputStream stream = zipFile.getInputStream(entry);
    }/*w  w  w .ja v a2s  .  c o  m*/

}

From source file:de.onyxbits.raccoon.ptools.ToolSupport.java

private void unzip(File file) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    try {//from www.j  a v  a2  s.  c  o m
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryDestination = new File(binDir, entry.getName());
            if (entry.isDirectory())
                entryDestination.mkdirs();
            else {
                entryDestination.getParentFile().mkdirs();
                InputStream in = zipFile.getInputStream(entry);
                OutputStream out = new FileOutputStream(entryDestination);
                IOUtils.copy(in, out);
                IOUtils.closeQuietly(in);
                out.close();
            }
        }
    } finally {
        zipFile.close();
    }
}

From source file:edu.harvard.i2b2.PreProcessFhirSpec.java

public PreProcessFhirSpec() throws IOException, XQueryUtilException {
    logger.trace("hi");
    ArrayList<String> resourceList = new ArrayList<String>();
    resourceList.add("Patient".toLowerCase());
    resourceList.add("Medication".toLowerCase());
    resourceList.add("MedicationStatement".toLowerCase());
    resourceList.add("Observation".toLowerCase());
    ZipFile zipFile = new ZipFile(Utils.getFilePath("fhir-spec.zip"));// fhir-all-xsd.zip
    logger.trace("" + zipFile.getName());

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

    for (String rName : resourceList) {
        String fName = "site/" + rName + ".profile.xml";
        logger.trace("" + zipFile.getEntry(fName));
        String fContent = IOUtils.toString(zipFile.getInputStream(zipFile.getEntry(fName)));
        logger.trace("" + XQueryUtil.processXQuery(
                "declare default element namespace \"http://hl7.org/fhir\";//searchParam", fContent));

    }// ww  w  .j a va  2s.com

    /*while (entries.hasMoreElements()) {
       ZipEntry entry = entries.nextElement();
       if(!entry.getName().matches(".*\\.html$")) continue;
       Pattern p = Pattern.compile("^site/([^\\.\\\\]*)\\..*$");
       Matcher m = p.matcher(entry.getName());
       String rname="";
       if (m.matches()) {
    //logger.trace("" + entry.getName() + "->" + m.group(1));
    rname=m.group(1).toLowerCase();
       }
               
       if (resourceList.contains(rname))
    logger.trace(entry.getName()+"->" + rname);
       // InputStream stream = zipFile.getInputStream(entry);
               
    }*/

}