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.alfresco.repo.dictionary.CMMDownloadTestUtil.java

public Set<String> getDownloadEntries(final NodeRef downloadNode) {
    return transactionHelper.doInTransaction(new RetryingTransactionCallback<Set<String>>() {
        @Override/*from ww  w.  j a va  2  s.co  m*/
        public Set<String> execute() throws Throwable {
            Set<String> entryNames = new TreeSet<String>();
            ContentReader reader = contentService.getReader(downloadNode, ContentModel.PROP_CONTENT);
            try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(
                    reader.getContentInputStream())) {
                ZipArchiveEntry zipEntry = null;
                while ((zipEntry = zipInputStream.getNextZipEntry()) != null) {
                    String name = zipEntry.getName();
                    entryNames.add(name);
                }
            }
            return entryNames;
        }
    });
}

From source file:org.alfresco.repo.download.DownloadServiceIntegrationTest.java

private Set<String> getEntries(final NodeRef downloadNode) {
    return TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Set<String>>() {

        @Override//from w  w w  .j a va 2  s.c o m
        public Set<String> execute() throws Throwable {
            Set<String> entryNames = new TreeSet<String>();
            ContentReader reader = CONTENT_SERVICE.getReader(downloadNode, ContentModel.PROP_CONTENT);
            ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(reader.getContentInputStream());
            try {
                ZipArchiveEntry zipEntry = zipInputStream.getNextZipEntry();
                while (zipEntry != null) {
                    String name = zipEntry.getName();
                    entryNames.add(name);
                    zipEntry = zipInputStream.getNextZipEntry();
                }
            } finally {
                zipInputStream.close();
            }
            return entryNames;
        }
    });
}

From source file:org.alfresco.repo.importer.ACPImportPackageHandler.java

public Reader getDataStream() {
    try {//  w  w w  . j ava2s.  c  om
        // find xml meta-data file
        ZipArchiveEntry xmlMetaDataEntry = null;

        // TODO: First, locate xml meta-data file by name

        // Scan the zip entries one by one (the slow approach)
        Enumeration entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                // Locate xml file in root of .acp
                String entryName = entry.getName();
                if (entryName.endsWith(".xml") && entryName.indexOf('/') == -1
                        && entryName.indexOf('\\') == -1) {
                    if (xmlMetaDataEntry != null) {
                        throw new ImporterException(
                                "Failed to find unique xml meta-data file within .acp package - multiple xml meta-data files exist.");
                    }
                    xmlMetaDataEntry = entry;
                }
            }
        }

        // oh dear, there's no data file
        if (xmlMetaDataEntry == null) {
            throw new ImporterException("Failed to find xml meta-data file within .acp package");
        }

        // open the meta-data xml file
        InputStream dataStream = zipFile.getInputStream(xmlMetaDataEntry);
        Reader inputReader = (dataFileEncoding == null) ? new InputStreamReader(dataStream, DEFAULT_ENCODING)
                : new InputStreamReader(dataStream, dataFileEncoding);
        return new BufferedReader(inputReader);
    } catch (UnsupportedEncodingException e) {
        throw new ImporterException("Encoding " + dataFileEncoding + " is not supported");
    } catch (IOException e) {
        throw new ImporterException(
                "Failed to open xml meta-data file within .acp package due to " + e.getMessage());
    }
}

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

/**
 * Fills the file and directory maps with resources read from the
 * archive.//ww w .  j  av  a  2 s  . com
 *
 * @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;//from   w  w  w  . j av a2 s . c o m
    FileNameMapper mapper = getMapper();
    if (!srcF.exists()) {
        throw new BuildException("Unable to expand " + srcF + " as the file does not exist", getLocation());
    }
    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.apache.james.mailbox.backup.ZipArchiveEntryAssert.java

private static BasicErrorMessageFactory shouldHaveName(ZipArchiveEntry zipArchiveEntry, String expected) {
    return new BasicErrorMessageFactory("%nExpecting %s to have name %s but was %s", zipArchiveEntry, expected,
            zipArchiveEntry.getName());
}

From source file:org.apache.karaf.decanter.kibana6.KibanaController.java

public void download() throws Exception {
    File target = new File(workingDirectory, KIBANA_FOLDER);
    if (target.exists()) {
        LOGGER.warn("Kibana folder already exists, download is skipped");
        return;//from www  .  j a  v  a2 s .c om
    }
    LOGGER.debug("Downloading Kibana from {}", KIBANA_LOCATION);
    if (isWindows()) {
        try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(
                new URL(KIBANA_LOCATION).openStream())) {
            ZipArchiveEntry entry;
            while ((entry = (ZipArchiveEntry) inputStream.getNextEntry()) != null) {
                File file = new File(workingDirectory, entry.getName());
                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    int read;
                    byte[] buffer = new byte[4096];
                    try (FileOutputStream outputStream = new FileOutputStream(file)) {
                        while ((read = inputStream.read(buffer, 0, 4096)) != -1) {
                            outputStream.write(buffer, 0, read);
                        }
                    }
                }
            }
        }
    } else {
        try (GzipCompressorInputStream gzInputStream = new GzipCompressorInputStream(
                new URL(KIBANA_LOCATION).openStream())) {
            try (TarArchiveInputStream inputStream = new TarArchiveInputStream(gzInputStream)) {
                TarArchiveEntry entry;
                while ((entry = (TarArchiveEntry) inputStream.getNextEntry()) != null) {
                    File file = new File(workingDirectory, entry.getName());
                    if (entry.isDirectory()) {
                        file.mkdirs();
                    } else {
                        int read;
                        byte[] buffer = new byte[4096];
                        try (FileOutputStream outputStream = new FileOutputStream(file)) {
                            while ((read = inputStream.read(buffer, 0, 4096)) != -1) {
                                outputStream.write(buffer, 0, read);
                            }
                        }
                        file.setLastModified(entry.getLastModifiedDate().getTime());
                        if (entry instanceof TarArchiveEntry) {
                            int mode = ((TarArchiveEntry) entry).getMode();
                            if ((mode & 00100) > 0) {
                                file.setExecutable(true, (mode & 00001) == 0);
                            }
                        }
                    }
                }
            }
        }
    }
    overrideConfig();
}

From source file:org.apache.slider.common.tools.SliderUtils.java

public static InputStream getApplicationResourceInputStream(FileSystem fs, Path appPath, String entry)
        throws IOException {
    InputStream is = null;/*from  w  ww .ja  v  a  2  s . c o m*/
    FSDataInputStream appStream = null;
    try {
        appStream = fs.open(appPath);
        ZipArchiveInputStream zis = new ZipArchiveInputStream(appStream);
        ZipArchiveEntry zipEntry;
        boolean done = false;
        while (!done && (zipEntry = zis.getNextZipEntry()) != null) {
            if (entry.equals(zipEntry.getName())) {
                int size = (int) zipEntry.getSize();
                if (size != -1) {
                    log.info("Reading {} of size {}", zipEntry.getName(), zipEntry.getSize());
                    byte[] content = new byte[size];
                    int offset = 0;
                    while (offset < size) {
                        offset += zis.read(content, offset, size - offset);
                    }
                    is = new ByteArrayInputStream(content);
                } else {
                    log.debug("Size unknown. Reading {}", zipEntry.getName());
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    while (true) {
                        int byteRead = zis.read();
                        if (byteRead == -1) {
                            break;
                        }
                        baos.write(byteRead);
                    }
                    is = new ByteArrayInputStream(baos.toByteArray());
                }
                done = true;
            }
        }
    } finally {
        IOUtils.closeStream(appStream);
    }

    return is;
}

From source file:org.apache.tika.parser.iwork.IWorkPackageParser.java

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {
    ZipArchiveInputStream zip = new ZipArchiveInputStream(stream);
    ZipArchiveEntry entry = zip.getNextZipEntry();

    while (entry != null) {
        if (!IWORK_CONTENT_ENTRIES.contains(entry.getName())) {
            entry = zip.getNextZipEntry();
            continue;
        }/*from   w ww  . jav a  2 s . co m*/

        InputStream entryStream = new BufferedInputStream(zip, 4096);
        entryStream.mark(4096);
        IWORKDocumentType type = IWORKDocumentType.detectType(entryStream);
        entryStream.reset();

        if (type != null) {
            XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
            ContentHandler contentHandler;

            switch (type) {
            case KEYNOTE:
                contentHandler = new KeynoteContentHandler(xhtml, metadata);
                break;
            case NUMBERS:
                contentHandler = new NumbersContentHandler(xhtml, metadata);
                break;
            case PAGES:
                contentHandler = new PagesContentHandler(xhtml, metadata);
                break;
            case ENCRYPTED:
                // We can't do anything for the file right now
                contentHandler = null;
                break;
            default:
                throw new TikaException("Unhandled iWorks file " + type);
            }

            metadata.add(Metadata.CONTENT_TYPE, type.getType().toString());
            xhtml.startDocument();
            if (contentHandler != null) {
                context.getSAXParser().parse(new CloseShieldInputStream(entryStream),
                        new OfflineContentHandler(contentHandler));
            }
            xhtml.endDocument();
        }

        entry = zip.getNextZipEntry();
    }
    // Don't close the zip InputStream (TIKA-1117).
}

From source file:org.apache.tika.parser.microsoft.ooxml.TruncatedOOXMLTest.java

@Test
@Ignore("for dev/debugging only")
public void listStreams() throws Exception {
    File tstDir = new File(TruncatedOOXMLTest.class.getResource("/test-documents").toURI());
    for (File f : tstDir.listFiles()) {
        if (f.isDirectory()) {
            continue;
        }// ww w  .  j a v  a2  s  . c om
        if (f.getName().endsWith(".xlsx")) {// || f.getName().endsWith(".pptx") || f.getName().endsWith(".docx")) {

        } else {
            continue;
        }
        try (InputStream is = new FileInputStream(f)) {
            ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(is);
            ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry();
            int cnt = 0;
            while (zae != null && !zae.isDirectory() && ++cnt <= 10) {
                System.out.println(f.getName() + " : " + zae.getName());
                if (zae.getName().equals("_rels/.rels")) {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    IOUtils.copy(zipArchiveInputStream, bos);
                    System.out.println(new String(bos.toByteArray(), StandardCharsets.UTF_8));
                }
                zae = zipArchiveInputStream.getNextZipEntry();
            }
        } catch (Exception e) {
            System.out.println(f.getName() + " : " + e.getMessage());
        }
    }
}