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.apache.tika.parser.microsoft.ooxml.xps.XPSExtractorDecorator.java

private static InputStream getZipStream(String zipPath, ZipPackage zipPackage)
        throws IOException, TikaException {
    String targPath = (zipPath.length() > 1 && zipPath.startsWith("/") ? zipPath.substring(1) : zipPath);
    ZipEntrySource zipEntrySource = zipPackage.getZipArchive();
    Enumeration<? extends ZipArchiveEntry> zipEntryEnumeration = zipEntrySource.getEntries();
    ZipArchiveEntry zipEntry = null;/*from   ww w  . j a va 2 s.  c  om*/
    while (zipEntryEnumeration.hasMoreElements()) {
        ZipArchiveEntry ze = zipEntryEnumeration.nextElement();
        if (ze.getName().equals(targPath)) {
            zipEntry = ze;
            break;
        }
    }
    if (zipEntry == null) {
        throw new TikaException("Couldn't find required zip entry: " + zipPath);
    }
    return zipEntrySource.getInputStream(zipEntry);
}

From source file:org.apache.tika.parser.pkg.StreamingZipContainerDetector.java

/**
 *
 * @param is inputstream to read from. Callers must mark/reset the stream
 *           before/after this call to detect.  This call does not close the stream!
 *           Depending on the file type, this call to detect may read the entire stream.
 *           Make sure to use a {@link org.apache.tika.io.BoundedInputStream} or similar
 *           if you want to protect against reading the entire stream.
 * @return//w ww. j a v  a2 s .  c om
 */
static MediaType detect(InputStream is) {

    Set<String> fileNames = new HashSet<>();
    Set<String> directoryNames = new HashSet<>();
    try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(
            new CloseShieldInputStream(is))) {
        ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry();
        while (zae != null) {
            String name = zae.getName();
            if (zae.isDirectory()) {
                directoryNames.add(name);
                zae = zipArchiveInputStream.getNextZipEntry();
                continue;
            }
            fileNames.add(name);
            //we could also parse _rel/.rels, but if
            // there isn't a valid content_types, then POI
            //will throw an exception...Better to backoff to PKG
            //than correctly identify a truncated
            if (name.equals("[Content_Types].xml")) {
                MediaType mt = parseOOXMLContentTypes(zipArchiveInputStream);
                if (mt != null) {
                    return mt;
                }
                return TIKA_OOXML;
            } else if (IWorkPackageParser.IWORK_CONTENT_ENTRIES.contains(name)) {
                IWorkPackageParser.IWORKDocumentType type = IWorkPackageParser.IWORKDocumentType
                        .detectType(zipArchiveInputStream);
                if (type != null) {
                    return type.getType();
                }
            } else if (name.equals("mimetype")) {
                //odt -- TODO -- bound the read and check that the results are
                //valid
                return MediaType.parse(IOUtils.toString(zipArchiveInputStream, UTF_8));
            }
            zae = zipArchiveInputStream.getNextZipEntry();
        }
    } catch (SecurityException e) {
        throw e;
    } catch (Exception e) {
        //swallow
    }
    //entrynames is the union of directory names and file names
    Set<String> entryNames = new HashSet<>(fileNames);
    entryNames.addAll(fileNames);
    MediaType mt = detectKmz(fileNames);
    if (mt != null) {
        return mt;
    }
    mt = detectJar(entryNames);
    if (mt != null) {
        return mt;
    }
    mt = detectIpa(entryNames);
    if (mt != null) {
        return mt;
    }
    mt = detectIWorks(entryNames);
    if (mt != null) {
        return mt;
    }
    int hits = 0;
    for (String s : OOXML_HINTS) {
        if (entryNames.contains(s)) {
            if (++hits > 2) {
                return TIKA_OOXML;
            }
        }
    }
    return MediaType.APPLICATION_ZIP;
}

From source file:org.apache.tika.parser.utils.ZipSalvager.java

/**
 * This streams the broken zip and rebuilds a new zip that
 * is at least a valid zip file.  The contents of the final stream
 * may be truncated, but the result should be a valid zip file.
 * <p>/*  w w w  . j  a v a  2 s.co  m*/
 * This does nothing fancy to fix the underlying broken zip.
 *
 * @param brokenZip
 * @param salvagedZip
 */
public static void salvageCopy(InputStream brokenZip, File salvagedZip) {
    try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(salvagedZip)) {
        ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(brokenZip);
        ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry();
        while (zae != null) {
            try {
                if (!zae.isDirectory() && zipArchiveInputStream.canReadEntryData(zae)) {
                    //create a new ZAE and copy over only the name so that
                    //if there is bad info (e.g. CRC) in brokenZip's zae, that
                    //won't be propagated or cause an exception
                    outputStream.putArchiveEntry(new ZipArchiveEntry(zae.getName()));
                    //this will copy an incomplete stream...so there
                    //could be truncation of the xml/contents, but the zip file
                    //should be intact.
                    boolean successfullyCopied = false;
                    try {
                        IOUtils.copy(zipArchiveInputStream, outputStream);
                        successfullyCopied = true;
                    } catch (IOException e) {
                        //this can hit a "truncated ZipFile" IOException
                    }
                    outputStream.flush();
                    outputStream.closeArchiveEntry();
                    if (!successfullyCopied) {
                        break;
                    }
                }
                zae = zipArchiveInputStream.getNextZipEntry();
            } catch (ZipException | EOFException e) {
                break;
            }

        }
        outputStream.flush();
        outputStream.finish();

    } catch (IOException e) {
        LOG.warn("problem fixing zip", e);
    }
}

From source file:org.apache.tika.server.CXFTestBase.java

protected Map<String, String> readZipArchive(InputStream inputStream) throws IOException {
    Map<String, String> data = new HashMap<String, String>();
    Path tempFile = writeTemporaryArchiveFile(inputStream, "zip");
    ZipFile zip = new ZipFile(tempFile.toFile());
    Enumeration<ZipArchiveEntry> entries = zip.getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        IOUtils.copy(zip.getInputStream(entry), bos);
        data.put(entry.getName(), DigestUtils.md5Hex(bos.toByteArray()));
    }//  ww  w .j  a v a2  s.c o m
    zip.close();
    Files.delete(tempFile);
    return data;
}

From source file:org.apache.wookie.w3c.util.WidgetPackageUtils.java

/**
 * uses apache commons compress to unpack all the zip entries into a target folder
 * partly adapted from the examples on the apache commons compress site, and an
 * example of generic Zip unpacking. Note this iterates over the ZipArchiveEntry enumeration rather
 * than use the more typical ZipInputStream parsing model, as according to the doco it will
 * more reliably read the entries correctly. More info here: http://commons.apache.org/compress/zip.html
 * @param zipfile the Zip File to unpack
 * @param targetFolder the folder into which to unpack the Zip file
 * @throws IOException// www.j a  v  a2  s .c  om
 */
@SuppressWarnings("unchecked")
public static void unpackZip(ZipFile zipfile, File targetFolder) throws IOException {
    targetFolder.mkdirs();
    BufferedOutputStream out = null;
    InputStream in = null;
    ZipArchiveEntry zipEntry;

    Enumeration entries = zipfile.getEntries();
    try {
        while (entries.hasMoreElements()) {
            zipEntry = (ZipArchiveEntry) entries.nextElement();
            // Don't add directories - use mkdirs instead
            if (!zipEntry.isDirectory()) {
                File outFile = new File(targetFolder, zipEntry.getName());

                // Ensure that the parent Folder exists
                if (!outFile.getParentFile().exists()) {
                    outFile.getParentFile().mkdirs();
                }
                // Read the entry
                in = new BufferedInputStream(zipfile.getInputStream(zipEntry));
                out = new BufferedOutputStream(new FileOutputStream(outFile));
                IOUtils.copy(in, out);
                // Restore time stamp
                outFile.setLastModified(zipEntry.getTime());

                // Close File
                out.close();
                // Close Stream
                in.close();
            }
        }

    }
    // We'll catch this exception to close the file otherwise it remains locked
    catch (IOException ex) {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.flush();
            out.close();
        }
        // And throw it again
        throw ex;
    }
}

From source file:org.artificer.atom.archive.ArchiveUtils.java

/**
 * Unpacks the given archive file into the output directory.
 * @param archiveFile an archive file/*from   www .jav a2 s .  c o  m*/
 * @param toDir where to unpack the archive to
 * @throws IOException
 */
public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);
        Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntriesInPhysicalOrder();
        while (zipEntries.hasMoreElements()) {
            ZipArchiveEntry entry = zipEntries.nextElement();
            String entryName = entry.getName();
            File outFile = new File(toDir, entryName);
            if (!outFile.getParentFile().exists()) {
                if (!outFile.getParentFile().mkdirs()) {
                    throw new IOException(Messages.i18n.format("FAILED_TO_CREATE_PARENT_DIR",
                            outFile.getParentFile().getCanonicalPath()));
                }
            }

            if (entry.isDirectory()) {
                if (outFile.isDirectory()) {
                    // Do nothing - already created.
                } else if (outFile.isFile()) {
                    throw new IOException(
                            Messages.i18n.format("FAILED_TO_CREATE_DIR", outFile.getCanonicalPath()));
                } else if (!outFile.mkdir()) {
                    throw new IOException(
                            Messages.i18n.format("FAILED_TO_CREATE_DIR", outFile.getCanonicalPath()));
                }
            } else {
                InputStream zipStream = null;
                OutputStream outFileStream = null;

                zipStream = zipFile.getInputStream(entry);
                outFileStream = new FileOutputStream(outFile);
                try {
                    IOUtils.copy(zipStream, outFileStream);
                } finally {
                    IOUtils.closeQuietly(zipStream);
                    IOUtils.closeQuietly(outFileStream);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}

From source file:org.beangle.commons.archiver.ZipUtils.java

public static List<String> unzip(final File zipFile, final String destination, String encoding) {
    List<String> fileNames = CollectUtils.newArrayList();
    String dest = destination;// w w w  .  ja v a  2s.  c  om
    if (!destination.endsWith(File.separator)) {
        dest = destination + File.separator;
    }
    ZipFile file;
    try {
        file = null;
        if (null == encoding)
            file = new ZipFile(zipFile);
        else
            file = new ZipFile(zipFile, encoding);
        @SuppressWarnings("unchecked")
        Enumeration<ZipArchiveEntry> en = file.getEntries();
        ZipArchiveEntry ze = null;
        while (en.hasMoreElements()) {
            ze = en.nextElement();
            File f = new File(dest, ze.getName());
            if (ze.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                InputStream is = file.getInputStream(ze);
                OutputStream os = new FileOutputStream(f);
                IOUtils.copy(is, os);
                is.close();
                os.close();
                fileNames.add(f.getAbsolutePath());
            }
        }
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileNames;
}

From source file:org.beangle.ems.util.ZipUtils.java

/**
 * <p>//ww  w.j  ava  2s.c  o m
 * unzip.
 * </p>
 * 
 * @param zipFile a {@link java.io.File} object.
 * @param destination a {@link java.lang.String} object.
 * @param encoding a {@link java.lang.String} object.
 * @return a {@link java.util.List} object.
 */
public static List<String> unzip(final File zipFile, final String destination, String encoding) {
    List<String> fileNames = CollectUtils.newArrayList();
    String dest = destination;
    if (!destination.endsWith(File.separator)) {
        dest = destination + File.separator;
    }
    ZipFile file;
    try {
        file = null;
        if (null == encoding)
            file = new ZipFile(zipFile);
        else
            file = new ZipFile(zipFile, encoding);
        Enumeration<ZipArchiveEntry> en = file.getEntries();
        ZipArchiveEntry ze = null;
        while (en.hasMoreElements()) {
            ze = en.nextElement();
            File f = new File(dest, ze.getName());
            if (ze.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                InputStream is = file.getInputStream(ze);
                OutputStream os = new FileOutputStream(f);
                IOs.copy(is, os);
                is.close();
                os.close();
                fileNames.add(f.getAbsolutePath());
            }
        }
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileNames;
}

From source file:org.betaconceptframework.astroboa.engine.jcr.io.ContentSourceExtractor.java

public InputStream extractStream(URI contentSource) throws Exception {
    dispose();//www . j a  v a 2s  .  c  om

    String filename = contentSource.toURL().getFile();

    if (StringUtils.isBlank(filename)) {
        throw new Exception("No file name from URL " + contentSource.toString());
    }

    if (filename.endsWith(".zip")) {
        tmpZip = File.createTempFile("rep", ".zip");

        FileUtils.copyURLToFile(contentSource.toURL(), tmpZip);

        zipFile = new ZipFile(tmpZip);

        Enumeration entries = zipFile.getEntries();

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

            if (entry.getName() != null && entry.getName().endsWith(".xml")) {
                return zipFile.getInputStream(entry);
            }
        }

        return null;
    } else if (filename.endsWith(".xml")) {
        return contentSource.toURL().openStream();
    } else {
        throw new Exception("Unsupported file extension " + filename);
    }
}

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

public void validateZipAndClose(InputStream in) throws IOException {
    ZipArchiveInputStream zip = new ZipArchiveInputStream(in);
    try {/*from  w w  w .  j a va  2 s . c  o m*/
        byte[] buf = new byte[1024];
        ZipArchiveEntry entry = zip.getNextZipEntry();
        if (entry == null)
            throw new BadRequest("Archive is empty");
        do {
            entry.getName();
            entry.getMethod();
            entry.getSize();
            while (zip.read(buf, 0, buf.length) >= 0)
                ;
            entry = zip.getNextZipEntry();
        } while (entry != null);
    } finally {
        zip.close();
    }
}