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

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

Introduction

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

Prototype

public boolean canReadEntryData(ZipArchiveEntry ze) 

Source Link

Document

Whether this class is able to read the given entry.

Usage

From source file:org.apache.ant.compress.resources.ZipScanner.java

/**
 * Fills the file and directory maps with resources read from the
 * archive.//from w  ww . j a  va2s . c  o m
 *
 * @param src the archive to scan.
 * @param encoding encoding used to encode file names inside the archive.
 * @param fileEntries Map (name to resource) of non-directory
 * resources found inside the archive.
 * @param matchFileEntries Map (name to resource) of non-directory
 * resources found inside the archive that matched all include
 * patterns and didn't match any exclude patterns.
 * @param dirEntries Map (name to resource) of directory
 * resources found inside the archive.
 * @param matchDirEntries Map (name to resource) of directory
 * resources found inside the archive that matched all include
 * patterns and didn't match any exclude patterns.
 */
protected void fillMapsFromArchive(Resource src, String encoding, Map fileEntries, Map matchFileEntries,
        Map dirEntries, Map matchDirEntries) {

    FileProvider fp = (FileProvider) src.as(FileProvider.class);
    if (fp == null) {
        super.fillMapsFromArchive(src, encoding, fileEntries, matchFileEntries, dirEntries, matchDirEntries);
        return;
    }

    File srcFile = fp.getFile();
    ZipArchiveEntry entry = null;
    ZipFile zf = null;

    try {
        try {
            zf = new ZipFile(srcFile, encoding);
        } catch (ZipException ex) {
            throw new BuildException("Problem reading " + srcFile, ex);
        } catch (IOException ex) {
            throw new BuildException("Problem opening " + srcFile, ex);
        }
        Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            entry = (ZipArchiveEntry) e.nextElement();
            if (getSkipUnreadableEntries() && !zf.canReadEntryData(entry)) {
                log(Messages.skippedIsUnreadable(entry));
                continue;
            }
            Resource r = new ZipResource(srcFile, encoding, entry);
            String name = entry.getName();
            if (entry.isDirectory()) {
                name = trimSeparator(name);
                dirEntries.put(name, r);
                if (match(name)) {
                    matchDirEntries.put(name, r);
                }
            } else {
                fileEntries.put(name, r);
                if (match(name)) {
                    matchFileEntries.put(name, r);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zf);
    }
}

From source file:org.apache.ant.compress.taskdefs.Unzip.java

protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
    log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
    ZipFile zf = null;
    FileNameMapper mapper = getMapper();
    if (!srcF.exists()) {
        throw new BuildException("Unable to expand " + srcF + " as the file does not exist", getLocation());
    }//from  ww  w . j  a  va2  s  . c  om
    try {
        zf = new ZipFile(srcF, getEncoding(), true);
        boolean empty = true;
        Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            empty = false;
            ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            if (getSkipUnreadableEntries() && !zf.canReadEntryData(ze)) {
                log(Messages.skippedIsUnreadable(ze));
                continue;
            }
            log("extracting " + ze.getName(), Project.MSG_DEBUG);
            InputStream is = null;
            try {
                extractFile(fileUtils, srcF, dir, is = zf.getInputStream(ze), ze.getName(),
                        new Date(ze.getTime()), ze.isDirectory(), mapper);
            } finally {
                FileUtils.close(is);
            }
        }
        if (empty && getFailOnEmptyArchive()) {
            throw new BuildException("archive '" + srcF + "' is empty");
        }
        log("expand complete", Project.MSG_VERBOSE);
    } catch (IOException ioe) {
        throw new BuildException("Error while expanding " + srcF.getPath() + "\n" + ioe.toString(), ioe);
    } finally {
        ZipFile.closeQuietly(zf);
    }
}

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  .  j a  v  a  2  s.  com
            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);
}