Example usage for org.apache.commons.compress.archivers.zip ZipFile getEntries

List of usage examples for org.apache.commons.compress.archivers.zip ZipFile getEntries

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipFile getEntries.

Prototype

public Enumeration getEntries() 

Source Link

Document

Returns all entries.

Usage

From source file:org.xwiki.filemanager.internal.job.PackJobTest.java

@Test
public void pack() throws Exception {
    Folder projects = mockFolder("Projects", "Pr\u00F4j\u00EA\u00E7\u021B\u0219", null,
            Arrays.asList("Concerto", "Resilience"), Arrays.asList("key.pub"));
    File key = mockFile("key.pub", "Projects");
    when(fileSystem.canView(key.getReference())).thenReturn(false);

    mockFolder("Concerto", "Projects", Collections.<String>emptyList(), Arrays.asList("pom.xml"));
    File pom = mockFile("pom.xml", "m&y p?o#m.x=m$l", "Concerto");
    setFileContent(pom, "foo");

    Folder resilience = mockFolder("Resilience", "Projects", Arrays.asList("src"), Arrays.asList("build.xml"));
    when(fileSystem.canView(resilience.getReference())).thenReturn(false);
    mockFolder("src", "Resilience");
    mockFile("build.xml");

    File readme = mockFile("readme.txt", "r\u00E9\u00E0dm\u00E8.txt");
    setFileContent(readme, "blah");

    PackRequest request = new PackRequest();
    request.setPaths(Arrays.asList(new Path(projects.getReference()), new Path(null, readme.getReference())));
    request.setOutputFileReference(//from  w w w  . jav  a  2  s.c o  m
            new AttachmentReference("out.zip", new DocumentReference("wiki", "Space", "Page")));

    PackJob job = (PackJob) execute(request);

    ZipFile zip = new ZipFile(
            new java.io.File(testFolder.getRoot(), "temp/filemanager/wiki/Space/Page/out.zip"));
    List<String> folders = new ArrayList<String>();
    Map<String, String> files = new HashMap<String, String>();
    Enumeration<ZipArchiveEntry> entries = zip.getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            folders.add(entry.getName());
        } else if (zip.canReadEntryData(entry)) {
            StringWriter writer = new StringWriter();
            IOUtils.copy(zip.getInputStream(entry), writer);
            files.put(entry.getName(), writer.toString());
        }
    }
    zip.close();

    assertEquals(Arrays.asList(projects.getName() + '/', projects.getName() + "/Concerto/"), folders);
    assertEquals(2, files.size());
    assertEquals("blah", files.get(readme.getName()));
    assertEquals("foo", files.get(projects.getName() + "/Concerto/" + pom.getName()));
    assertEquals(("blah" + "foo").getBytes().length, job.getStatus().getBytesWritten());
    assertTrue(job.getStatus().getOutputFileSize() > 0);
}

From source file:org.xwiki.filter.test.internal.ZIPFileAssertComparator.java

private static Map<String, byte[]> unzip(File filename) throws IOException {
    Map<String, byte[]> zipContent = new HashMap<String, byte[]>();

    ZipFile zipFile = new ZipFile(filename);

    try {//from www .j av  a2s  .  c om
        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();

            InputStream inputStream = zipFile.getInputStream(entry);
            try {
                zipContent.put(entry.getName(), IOUtils.toByteArray(inputStream));
            } finally {
                inputStream.close();
            }
        }
    } finally {
        zipFile.close();
    }

    return zipContent;
}

From source file:org.xwiki.xar.XarPackage.java

/**
 * Find and add the entries located in the passed XAR file.
 * /*  w  w  w.  j a va 2 s  . c  o m*/
 * @param zipFile the XAR file
 * @throws IOException when failing to read the file
 * @throws XarException when failing to parse the XAR package
 */
public void read(ZipFile zipFile) throws IOException, XarException {
    Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();

    while (zipEntries.hasMoreElements()) {
        ZipArchiveEntry entry = zipEntries.nextElement();

        if (!entry.isDirectory()) {
            InputStream stream = zipFile.getInputStream(entry);

            try {
                readEntry(stream, entry);
            } finally {
                stream.close();
            }
        }
    }
}

From source file:org.yes.cart.utils.impl.ZipUtils.java

/**
 * Unzip archive to given folder.// www. ja v a2  s  .  c o m
 * @param archive given archive
 * @param outputDir  given folder
 * @throws IOException in case of error
 */

public void unzipArchive(final File archive, final File outputDir) throws IOException {
    ZipFile zipfile = new ZipFile(archive, encoding);
    for (Enumeration e = zipfile.getEntries(); e.hasMoreElements();) {
        final ZipArchiveEntry entry = (ZipArchiveEntry) e.nextElement();
        unzipEntry(zipfile, entry, outputDir);
    }
    zipfile.close();
}

From source file:uk.nhs.cfh.dsp.srth.distribution.FileDownloader.java

private void extractZipFileContents(File file) {
    Enumeration entries;//  w ww.  j  ava2  s .  co  m

    try {
        logger.info("Extracting zip file contents...");
        ZipFile zipFile = new ZipFile(file);

        entries = zipFile.getEntries();

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

            if (entry.isDirectory()) {

                logger.info("Extracting directory: " + entry.getName());
                File dir = new File(file.getParent(), entry.getName());
                boolean canWrite = dir.exists();
                if (!canWrite) {
                    canWrite = dir.mkdir();
                }

                if (canWrite) {
                    continue;
                } else {
                    logger.warning("Error creating directory during extraction");
                }
            }

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

        zipFile.close();
        logger.info("Closed zip file.");
    } catch (IOException e) {
        logger.warning("Nested exception is : " + e.fillInStackTrace());
    }
}