Example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry getName

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry getName

Introduction

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

Prototype

public String getName() 

Source Link

Document

Get the name of the entry.

Usage

From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java

public InputStream readZipEntry(String match) throws IOException {
    InputStream in = this.openInputStream();
    try {//from w ww . j  a va2s.c  o  m
        ZipArchiveInputStream zip = new ZipArchiveInputStream(in);
        byte[] buf = new byte[1024];
        ZipArchiveEntry entry = zip.getNextZipEntry();
        do {
            if (entry.getName().equals(match)) {
                return zip;
            }
            long size = entry.getSize();
            if (size > 0) {
                zip.skip(size);
            } else {
                while (zip.read(buf, 0, buf.length) >= 0)
                    ;
            }
            entry = zip.getNextZipEntry();
        } while (entry != null);
        zip.close();
    } catch (RuntimeException | Error | IOException e) {
        in.close();
        throw e;
    }
    return null;
}

From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java

public XMLEventReader createAtomFeedFromArchive(final String id, final String entryPattern) throws IOException {
    final FileObject file = this;
    final XMLEventFactory ef = XMLEventFactory.newInstance();
    final byte[] buf = new byte[1024];
    final ZipArchiveInputStream zip = new ZipArchiveInputStream(file.openInputStream());
    return new XMLEventReaderBase() {
        private boolean started;
        private boolean ended;

        public void close() throws XMLStreamException {
            try {
                zip.close();//from ww w .j  a va2  s .co m
            } catch (IOException e) {
                throw new XMLStreamException(e);
            }
        }

        protected boolean more() throws XMLStreamException {
            try {
                ZipArchiveEntry entry;
                if (!started) {
                    Namespace atom = ef.createNamespace(FEED.getPrefix(), FEED.getNamespaceURI());
                    add(ef.createStartDocument());
                    add(ef.createStartElement(FEED, null, Arrays.asList(atom).iterator()));
                    add(ef.createStartElement(TITLE, null, null));
                    add(ef.createCharacters(file.getName()));
                    add(ef.createEndElement(TITLE, null));
                    add(ef.createStartElement(ID, null, null));
                    add(ef.createCharacters(id));
                    add(ef.createEndElement(ID, null));
                    Attribute href = ef.createAttribute("href", file.toUri().toASCIIString());
                    List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", "application/zip"));
                    add(ef.createStartElement(LINK, attrs.iterator(), null));
                    add(ef.createEndElement(LINK, null));
                    add(ef.createStartElement(UPDATED, null, null));
                    add(ef.createCharacters(format(new Date(file.getLastModified()))));
                    add(ef.createEndElement(UPDATED, null));
                    started = true;
                    return true;
                } else if (started && !ended && (entry = zip.getNextZipEntry()) != null) {
                    String name = entry.getName();
                    String link = entryPattern.replace("{entry}", PercentCodec.encode(name));
                    MimetypesFileTypeMap mimetypes = new javax.activation.MimetypesFileTypeMap();
                    String type = mimetypes.getContentType(name);
                    if (type == null || type.length() == 0) {
                        type = "application/octet-stream";
                    }
                    add(ef.createStartElement(ENTRY, null, null));
                    add(ef.createStartElement(TITLE, null, null));
                    add(ef.createCharacters(name));
                    add(ef.createEndElement(TITLE, null));
                    Attribute href = ef.createAttribute("href", link);
                    List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", type));
                    add(ef.createStartElement(LINK, attrs.iterator(), null));
                    add(ef.createEndElement(LINK, null));
                    long size = entry.getSize();
                    if (size > 0) {
                        zip.skip(size);
                    } else {
                        while (zip.read(buf, 0, buf.length) >= 0)
                            ;
                    }
                    add(ef.createEndElement(ENTRY, null));
                    return true;
                } else if (!ended) {
                    add(ef.createEndElement(FEED, null));
                    add(ef.createEndDocument());
                    ended = true;
                    return true;
                } else {
                    return false;
                }
            } catch (IOException e) {
                throw new XMLStreamException(e);
            }
        }
    };
}

From source file:org.callimachusproject.io.CarInputStream.java

private String readEntryType(ZipArchiveEntry entry, BufferedInputStream in) throws IOException {
    if (entry == null)
        return null;
    String type = ContentTypeExtraField.parseExtraField(entry);
    if (type != null || entry.isDirectory())
        return type;
    if (FILE_NAME.matcher(entry.getName()).find())
        return mimetypes.getContentType(entry.getName());
    return detectRdfType(in);
}

From source file:org.cloudfoundry.util.ResourceMatchingUtils.java

private static Flux<ArtifactMetadata> getArtifactMetadataFromZip(Path application) {
    List<ArtifactMetadata> artifactMetadatas = new ArrayList<>();

    try (ZipFile zipFile = new ZipFile(application.toFile())) {
        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();

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

            if (!entry.isDirectory()) {
                try (InputStream in = zipFile.getInputStream(entry)) {
                    String hash = FileUtils.hash(in);
                    String path = entry.getName();
                    String permissions = FileUtils.permissions(entry.getUnixMode());
                    int size = (int) entry.getSize();

                    artifactMetadatas.add(new ArtifactMetadata(hash, path, permissions, size));
                }//w  ww.  java 2s  .  c o m

            }
        }
    } catch (IOException e) {
        throw Exceptions.propagate(e);
    }

    return Flux.fromIterable(artifactMetadatas);
}

From source file:org.codehaus.mojo.unix.maven.zip.ZipPackageTest.java

private void assertDirectory(ZipArchiveEntry entry, String name, LocalDateTime time) throws IOException {
    assertNotNull(name, entry);/*from w w w  . j  a  v  a 2  s .  co  m*/
    assertTrue(name + " should be file", entry.isDirectory());
    assertEquals(name + ", name", name, entry.getName());
    assertEquals(name + ", timestamp", time, new LocalDateTime(entry.getTime()));
}

From source file:org.codehaus.mojo.unix.maven.zip.ZipPackageTest.java

private void assertFile(ZipFile file, ZipArchiveEntry entry, String name, int size, LocalDateTime time,
        String content, UnixFileMode mode) throws IOException {
    InputStream in = file.getInputStream(entry);

    assertFalse(name + " should be file", entry.isDirectory());
    assertEquals(name + ", name", name, entry.getName());
    assertEquals(name + ", timestamp", time, new LocalDateTime(entry.getTime()));
    // wtf: http://vimalathithen.blogspot.no/2006/06/using-zipentrygetsize.html
    // assertEquals( name + ", size", size, entry.getSize() );

    byte[] bytes = new byte[1000];
    assertEquals(size, in.read(bytes, 0, bytes.length));
    assertEquals(content, new String(bytes, 0, size, charset));

    assertEquals(ZipArchiveEntry.PLATFORM_UNIX, entry.getPlatform());
    assertEquals(name + ", mode", mode.toString(), UnixFileMode.fromInt(entry.getUnixMode()).toString());
}

From source file:org.codehaus.plexus.archiver.jar.JarArchiver.java

/**
 * Grab lists of all root-level files and all directories
 * contained in the given archive./*w  w w .  j  a  v  a2  s  .c o m*/
 *
 * @param file  .
 * @param files .
 * @param dirs  .
 * @throws java.io.IOException .
 */
protected static void grabFilesAndDirs(String file, List<String> dirs, List<String> files) throws IOException {
    File zipFile = new File(file);
    if (!zipFile.exists()) {
        Logger logger = new ConsoleLogger(Logger.LEVEL_INFO, "console");
        logger.error("JarArchive skipping non-existing file: " + zipFile.getAbsolutePath());
    } else if (zipFile.isDirectory()) {
        Logger logger = new ConsoleLogger(Logger.LEVEL_INFO, "console");
        logger.info("JarArchiver skipping indexJar " + zipFile + " because it is not a jar");
    } else {
        org.apache.commons.compress.archivers.zip.ZipFile zf = null;
        try {
            zf = new org.apache.commons.compress.archivers.zip.ZipFile(file, "utf-8");
            Enumeration<ZipArchiveEntry> entries = zf.getEntries();
            HashSet<String> dirSet = new HashSet<String>();
            while (entries.hasMoreElements()) {
                ZipArchiveEntry ze = entries.nextElement();
                String name = ze.getName();
                // avoid index for manifest-only jars.
                if (!name.equals(META_INF_NAME) && !name.equals(META_INF_NAME + '/') && !name.equals(INDEX_NAME)
                        && !name.equals(MANIFEST_NAME)) {
                    if (ze.isDirectory()) {
                        dirSet.add(name);
                    } else if (!name.contains("/")) {
                        files.add(name);
                    } else {
                        // a file, not in the root
                        // since the jar may be one without directory
                        // entries, add the parent dir of this file as
                        // well.
                        dirSet.add(name.substring(0, name.lastIndexOf("/") + 1));
                    }
                }
            }
            dirs.addAll(dirSet);
        } finally {
            if (zf != null) {
                zf.close();
            }
        }
    }
}

From source file:org.codehaus.plexus.archiver.zip.AbstractZipArchiveContentLister.java

protected List<ArchiveContentEntry> execute() throws ArchiverException {
    ArrayList<ArchiveContentEntry> archiveContentList = new ArrayList<ArchiveContentEntry>();

    getLogger().debug("listing: " + getSourceFile());
    org.apache.commons.compress.archivers.zip.ZipFile zf = null;
    try {//from   w ww . j a v  a  2  s  .co m
        zf = new org.apache.commons.compress.archivers.zip.ZipFile(getSourceFile(), encoding);
        final Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo(zf, ze);
            if (isSelected(ze.getName(), fileInfo)) {
                ArchiveContentEntry ae = fileInfo.asArchiveContentEntry();
                archiveContentList.add(ae);
            }
        }
        getLogger().debug("listing complete");
    } catch (final IOException ioe) {
        throw new ArchiverException("Error while listing " + getSourceFile().getAbsolutePath(), ioe);
    } finally {
        IOUtils.closeQuietly(zf);
    }
    return archiveContentList;
}

From source file:org.codehaus.plexus.archiver.zip.AbstractZipUnArchiver.java

protected void execute() throws ArchiverException {
    getLogger().debug("Expanding: " + getSourceFile() + " into " + getDestDirectory());
    org.apache.commons.compress.archivers.zip.ZipFile zf = null;
    try {//from  w ww.j a v  a 2s  .  c  om
        zf = new org.apache.commons.compress.archivers.zip.ZipFile(getSourceFile(), encoding);
        final Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo(zf, ze);
            if (isSelected(ze.getName(), fileInfo)) {
                InputStream in = zf.getInputStream(ze);
                extractFileIfIncluded(getSourceFile(), getDestDirectory(), in, ze.getName(),
                        new Date(ze.getTime()), ze.isDirectory(),
                        ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink(zf, ze));
                IOUtil.close(in);
            }

        }

        getLogger().debug("expand complete");
    } catch (final IOException ioe) {
        throw new ArchiverException("Error while expanding " + getSourceFile().getAbsolutePath(), ioe);
    } finally {
        IOUtils.closeQuietly(zf);
    }
}

From source file:org.codehaus.plexus.archiver.zip.AbstractZipUnArchiver.java

protected void execute(final String path, final File outputDirectory) throws ArchiverException {
    org.apache.commons.compress.archivers.zip.ZipFile zipFile = null;

    try {//from   ww  w .  j a  va 2  s  .  c  o  m
        zipFile = new org.apache.commons.compress.archivers.zip.ZipFile(getSourceFile(), encoding);

        final Enumeration e = zipFile.getEntries();

        while (e.hasMoreElements()) {
            final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo(zipFile, ze);
            if (!isSelected(ze.getName(), fileInfo)) {
                continue;
            }

            if (ze.getName().startsWith(path)) {
                final InputStream inputStream = zipFile.getInputStream(ze);
                extractFileIfIncluded(getSourceFile(), outputDirectory, inputStream, ze.getName(),
                        new Date(ze.getTime()), ze.isDirectory(),
                        ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink(zipFile, ze));
                IOUtil.close(inputStream);
            }
        }
    } catch (final IOException ioe) {
        throw new ArchiverException("Error while expanding " + getSourceFile().getAbsolutePath(), ioe);
    } finally {
        IOUtils.closeQuietly(zipFile);
    }
}