Example usage for java.util.zip ZipFile ZipFile

List of usage examples for java.util.zip ZipFile ZipFile

Introduction

In this page you can find the example usage for java.util.zip ZipFile ZipFile.

Prototype

public ZipFile(File file) throws ZipException, IOException 

Source Link

Document

Opens a ZIP file for reading given the specified File object.

Usage

From source file:org.ambraproject.article.service.IngesterTest.java

@DataProvider(name = "imageArticle")
public Object[][] getImageArticle() throws Exception {
    File testFile = new File(
            IngesterTest.class.getClassLoader().getResource("ingest/test-ingest-image-article.zip").toURI());
    ZipFile archive = new ZipFile(testFile);
    //put the article in the db so we can reingest it
    Article article = new Article();
    article.setDoi("info:doi/10.1371/image.pcol.v01.i10");
    dummyDataStore.store(article);/*  w w  w .  j  a  v a2 s .  co  m*/

    Issue issue = new Issue();
    issue.setIssueUri("info:doi/10.1371/issue.pcol.v01.i01");
    issue.setImageUri(article.getDoi());
    issue.setDescription("This description should get overwritten");
    Long issueId = Long.valueOf(dummyDataStore.store(issue));

    return new Object[][] { { archive, issueId } };
}

From source file:gdt.data.entity.facet.ExtensionHandler.java

public static InputStream getResourceStream(String jar$, String resource$) {
    try {//from w  w  w .  j a  v a  2 s  .c o m
        //      System.out.println("ExtensionHandler:loadIcon:jar="+jar$);
        ZipFile zf = new ZipFile(jar$);
        Enumeration<? extends ZipEntry> entries = zf.entries();
        ZipEntry ze;
        String[] sa;
        while (entries.hasMoreElements()) {
            try {
                ze = entries.nextElement();
                sa = ze.getName().split("/");
                //     System.out.println("ExtensionHandler:loadIcon:zip entry="+sa[sa.length-1]);
                if (resource$.equals(sa[sa.length - 1])) {
                    InputStream is = zf.getInputStream(ze);
                    if (is != null)
                        return is;

                }
            } catch (Exception e) {

            }
        }
        return null;
    } catch (Exception e) {
        Logger.getLogger(ExtensionHandler.class.getName()).severe(e.toString());
        return null;
    }

}

From source file:it.polito.tellmefirst.web.rest.clients.ClientEpub.java

private HashMap<String, String> parseEpub(File file) throws IOException, TMFVisibleException {

    LOG.debug("[parseEpub] - BEGIN");

    ZipFile fi = new ZipFile(file);

    for (Enumeration e = fi.entries(); e.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        if (entry.getName().endsWith("ncx")) {
            InputStream tocMaybeDirty = fi.getInputStream(entry);
            Scanner scanner = new Scanner(tocMaybeDirty, "UTF-8").useDelimiter("\\A");
            String theString = scanner.hasNext() ? scanner.next() : "";
            tocMaybeDirty.close();/*ww w . j  a  va  2  s.  com*/
            scanner.close();

            String res = theString.replaceAll(">[\\s]*?<", "><");

            InputStream toc = new ByteArrayInputStream(res.getBytes(StandardCharsets.UTF_8));

            try {
                DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document doc = dBuilder.parse(toc);
                toc.close();

                if (doc.hasChildNodes()) {
                    findNavMap(doc.getChildNodes());
                }
            } catch (Exception ex) {
                LOG.error("Unable to navigate the TOC");
            }

            removeEmptyTOC(epub);

            //search anchors in links and split
            Set set = epub.entrySet();
            Iterator i = set.iterator();
            while (i.hasNext()) {
                Map.Entry me = (Map.Entry) i.next();
                if (me.getValue().toString().contains("#")) {
                    String[] parts = me.getValue().toString().split("#");
                    String anchor = parts[1];
                    epub.put(me.getKey().toString(), anchor);
                }
            }
        }
        if (entry.getName().endsWith("opf")) { //manage files because order is important
            InputStream content = fi.getInputStream(entry);

            Scanner scanner = new Scanner(content, "UTF-8").useDelimiter("\\A");
            String contentString = scanner.hasNext() ? scanner.next() : "";
            content.close();
            scanner.close();

            String filenameRegex = "href=\"(.*.htm(|l))\".*media-type=\"application/xhtml";
            Pattern pattern = Pattern.compile(filenameRegex);
            Matcher matcher = pattern.matcher(contentString);

            Integer count = 0;
            while (matcher.find()) {
                files.put(count, matcher.group(1));
                count++;
            }
        }
        if (entry.getName().endsWith("html") || entry.getName().endsWith("htm")
                || entry.getName().endsWith("xhtml")) {
            InputStream htmlFile = fi.getInputStream(entry);

            Scanner scanner = new Scanner(htmlFile, "UTF-8").useDelimiter("\\A");
            String htmlString = scanner.hasNext() ? scanner.next() : "";

            String regex1 = htmlString.replaceAll("^[^_]*?<body>", ""); //remove head
            String regex2 = regex1.replaceAll("</body>.*$", ""); //remove tail
            String htmlCleaned = regex2.replaceAll("<a.*?/>", ""); //anchor with one tag

            String[] bits = entry.getName().split("/");
            String fileName = bits[bits.length - 1];

            htmls.put(fileName, htmlCleaned);
        }
    }
    fi.close();
    Integer i;
    for (i = 0; i < files.size(); i++) {
        stringBuilder.append("<p id=\"" + files.get(i) + "\"></p>"); // "anchor" also the heads of each files
        stringBuilder.append(htmls.get(files.get(i)));
    }
    String htmlAll = stringBuilder.toString();

    /* We have all needed files, start to split
       For each link -> made a chunk
       Start from the bottom */
    Metadata metadata = new Metadata();
    Parser parser = new HtmlParser();
    ListIterator<Map.Entry<String, String>> iter = new ArrayList<>(epub.entrySet()).listIterator(epub.size());

    while (iter.hasPrevious()) {
        Map.Entry<String, String> me = iter.previous();
        try {
            ContentHandler contenthandler = new BodyContentHandler(10 * htmlAll.length());
            Scanner sc = new Scanner(htmlAll);
            sc.useDelimiter("id=\"" + me.getValue().toString() + "\">");
            htmlAll = sc.next();
            InputStream stream = new ByteArrayInputStream(sc.next().getBytes(StandardCharsets.UTF_8));
            parser.parse(stream, contenthandler, metadata, new ParseContext());
            String chapterText = contenthandler.toString().toLowerCase().replaceAll("\\d+.*", "");
            String chapterTextWithoutNo = chapterText.replaceAll("\\d+.*", "");
            // Remove the Project Gutenberg meta information from the text
            String chapterTextCleaned = chapterTextWithoutNo.split("end of the project gutenberg ebook")[0];
            epub.put(me.getKey().toString(), chapterTextCleaned);

        } catch (Exception ex) {
            LOG.error("Unable to parse content for index: " + me.getKey() + ", this chapter will be deleted");
            removeChapter(epub, me.getKey().toString());
        }
    }

    /* I remove the Project Gutenberg license chapter from the Map, because it is useless
      for the classification and it generates a Lucene Exception in case of the Italian language
      (the license text is always in English).
            
      You can use this method in order to remove each chapter that is useless for classifying
      your Epub document. */
    removeChapter(epub, "A Word from Project Gutenberg");
    removeEmptyItems(epub);

    //If the Epub file has a bad structure, I try to use the basic Epub extractor of Tika.
    if (epub.size() == 0) {
        LOG.info("The Epub file has a bad structure. Try to use the Tika extractor");
        epub.put("All text", autoParseAll(file));
    }

    removeEmptyItems(epub);

    if (epub.size() == 0) {
        LOG.error("Unable to extract text from this Epub");
        throw new TMFVisibleException("Unable to extract any text from this Epub.");
    }

    removeDownloadedFile(TEMPORARY_PATH);

    LOG.debug("[parseEpub] - END");

    return epub;
}

From source file:fr.certu.chouette.exchange.xml.neptune.importer.XMLNeptuneImportLinePlugin.java

/**
 * import ZipFile/*from   ww w.  ja v a2 s .com*/
 * 
 * @param filePath
 *           path to zip File
 * @param validate
 *           process XML and XSD format validation
 * @param importReport
 *           report to fill
 * @param optimizeMemory
 * @param unsharedData
 * @param sharedData
 * @return list of loaded lines
 */
private List<Line> processZipImport(String filePath, boolean validate, Report importReport,
        Report validationReport, boolean optimizeMemory, SharedImportedData sharedData,
        UnsharedImportedData unsharedData) {
    ZipFile zip = null;
    try {
        Charset encoding = FileTool.getZipCharset(filePath);
        if (encoding == null) {
            ReportItem item = new ExchangeReportItem(ExchangeReportItem.KEY.FILE_ERROR, Report.STATE.ERROR,
                    filePath, "unknown encoding");
            importReport.addItem(item);
            importReport.updateStatus(Report.STATE.ERROR);
            logger.error("zip import failed (unknown encoding)");
            return null;
        }
        zip = new ZipFile(filePath);
    } catch (IOException e) {
        // report for save
        ReportItem fileErrorItem = new ExchangeReportItem(ExchangeReportItem.KEY.ZIP_ERROR, Report.STATE.ERROR,
                e.getLocalizedMessage());
        importReport.addItem(fileErrorItem);
        // log
        logger.error("zip import failed (cannot open zip)" + e.getLocalizedMessage());
        return null;
    }
    List<Line> lines = new ArrayList<Line>();
    for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
        ZipEntry entry = entries.nextElement();

        // ignore directory without warning
        if (entry.isDirectory())
            continue;

        String entryName = entry.getName();
        if (!FilenameUtils.getExtension(entryName).toLowerCase().equals("xml")) {
            // report for save
            ReportItem fileReportItem = new ExchangeReportItem(ExchangeReportItem.KEY.FILE_IGNORED,
                    Report.STATE.OK, entryName);
            importReport.addItem(fileReportItem);
            // log
            logger.info("zip entry " + entryName + " bypassed ; not a XML file");
            continue;
        }
        logger.info("start import zip entry " + entryName);
        ReportItem fileReportItem = new ExchangeReportItem(ExchangeReportItem.KEY.FILE, Report.STATE.OK,
                entryName);
        importReport.addItem(fileReportItem);
        try {
            InputStream stream = zip.getInputStream(entry);
            stream.close();
        } catch (IOException e) {
            // report for save
            ReportItem errorItem = new ExchangeReportItem(ExchangeReportItem.KEY.FILE_ERROR, Report.STATE.ERROR,
                    e.getLocalizedMessage());
            fileReportItem.addItem(errorItem);
            // log
            logger.error("zip entry " + entryName + " import failed (get entry)" + e.getLocalizedMessage());
            continue;
        }
        ChouettePTNetworkHolder holder = null;
        try {
            holder = reader.read(zip, entry, validate);
            validationReport.addItem(holder.getReport());
        } catch (ExchangeRuntimeException e) {
            // report for save
            ReportItem errorItem = new ExchangeReportItem(ExchangeReportItem.KEY.FILE_ERROR, Report.STATE.ERROR,
                    e.getLocalizedMessage());
            fileReportItem.addItem(errorItem);
            // log
            logger.error("zip entry " + entryName + " import failed (read XML)" + e.getLocalizedMessage());
            continue;
        } catch (Exception e) {
            // report for save
            ReportItem errorItem = new ExchangeReportItem(ExchangeReportItem.KEY.FILE_ERROR, Report.STATE.ERROR,
                    e.getLocalizedMessage());
            fileReportItem.addItem(errorItem);
            // log
            logger.error(e.getLocalizedMessage());
            continue;
        }
        try {
            Line line = processImport(holder, validate, fileReportItem, validationReport, entryName, sharedData,
                    unsharedData, optimizeMemory);

            if (line != null) {
                lines.add(line);
            } else {
                logger.error("zip entry " + entryName + " import failed (build model)");
            }

        } catch (ExchangeException e) {
            // report for save
            ReportItem errorItem = new ExchangeReportItem(ExchangeReportItem.KEY.FILE_ERROR, Report.STATE.ERROR,
                    e.getLocalizedMessage());
            fileReportItem.addItem(errorItem);
            // log
            logger.error(
                    "zip entry " + entryName + " import failed (convert to model)" + e.getLocalizedMessage());
            continue;
        }
        logger.info("zip entry imported");
    }
    try {
        zip.close();
    } catch (IOException e) {
        logger.info("cannot close zip file");
    }
    if (lines.size() == 0) {
        logger.error("zip import failed (no valid entry)");
        return null;
    }
    return lines;
}

From source file:edu.kit.dama.util.ZipUtils.java

/**
 * Unzip file in output directory and delete zip file.
 *
 * @param fSourceZip zip file to extract.
 * @param outputDir directory where the files should be extracted.
 * @param delete delete source file or not.
 */// w w w  .  j  av  a  2 s  .c o m
public static void unzip(File fSourceZip, File outputDir, boolean delete) {
    try {
        if (outputDir.mkdirs()) {
            LOGGER.debug("Create directory '{}'", outputDir.getAbsolutePath());
        }
        try (ZipFile zipFile = new ZipFile(fSourceZip)) {
            Enumeration e = zipFile.entries();

            while (e.hasMoreElements()) {
                extractEntry(outputDir, zipFile, (ZipEntry) e.nextElement());
            }
        }

        // delete zip file in case of success
        //deletion must be outside of "try-with-resource" block as fSourceZip is in use inside
        if (delete && fSourceZip != null && fSourceZip.exists()
                && !org.apache.commons.io.FileUtils.deleteQuietly(fSourceZip)) {
            LOGGER.error("Error deleting '{}'!?", fSourceZip.getAbsolutePath());
        }
    } catch (IOException ioe) {
        LOGGER.error("Error while unzipping file!", ioe);
    }

}

From source file:com.t3.persistence.FileUtil.java

public static void unzipFile(File sourceFile, File destDir) throws IOException {
    if (!sourceFile.exists())
        throw new IOException("source file does not exist: " + sourceFile);

    try (ZipFile zipFile = new ZipFile(sourceFile)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory())
                continue;

            String path = destDir.getAbsolutePath() + File.separator + entry.getName();
            File file = new File(path);
            file.getParentFile().mkdirs();

            //System.out.println("Writing file: " + path);
            try (InputStream is = zipFile.getInputStream(entry);
                    BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(path));) {
                IOUtils.copy(is, os);//  ww w.  j a  v a  2  s .  co  m
            }
        }
    }
}

From source file:com.sshtools.j2ssh.util.DynamicClassLoader.java

private byte[] loadClassFromZipfile(File file, String name, ClassCacheEntry cache) throws IOException {
    // Translate class name to file name
    String classFileName = name.replace('.', '/') + ".class";

    ZipFile zipfile = new ZipFile(file);

    try {// w ww.  j  a va  2s.  c o m
        ZipEntry entry = zipfile.getEntry(classFileName);

        if (entry != null) {
            cache.origin = file;

            return loadBytesFromStream(zipfile.getInputStream(entry), (int) entry.getSize());
        } else {
            // Not found
            return null;
        }
    } finally {
        zipfile.close();
    }
}

From source file:bammerbom.ultimatecore.bukkit.UltimateUpdater.java

/**
 * Part of Zip-File-Extractor, modified by Gravity for use with Updater.
 *
 * @param file the location of the file to extract.
 *///from   ww  w. j a va  2  s .  c  o  m
private void unzip(String file) {
    final File fSourceZip = new File(file);
    try {
        final String zipPath = file.substring(0, file.length() - 4);
        ZipFile zipFile = new ZipFile(fSourceZip);
        Enumeration<? extends ZipEntry> e = zipFile.entries();
        while (e.hasMoreElements()) {
            ZipEntry entry = e.nextElement();
            File destinationFilePath = new File(zipPath, entry.getName());
            destinationFilePath.getParentFile().mkdirs();
            if (!entry.isDirectory()) {
                final BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
                int b;
                final byte[] buffer = new byte[UltimateUpdater.BYTE_SIZE];
                final FileOutputStream fos = new FileOutputStream(destinationFilePath);
                final BufferedOutputStream bos = new BufferedOutputStream(fos, UltimateUpdater.BYTE_SIZE);
                while ((b = bis.read(buffer, 0, UltimateUpdater.BYTE_SIZE)) != -1) {
                    bos.write(buffer, 0, b);
                }
                bos.flush();
                bos.close();
                bis.close();
                final String name = destinationFilePath.getName();
                if (name.endsWith(".jar") && this.pluginExists(name)) {
                    File output = new File(updateFolder, name);
                    destinationFilePath.renameTo(output);
                }
            }
        }
        zipFile.close();

        // Move any plugin data folders that were included to the right place, Bukkit won't do this for us.
        moveNewZipFiles(zipPath);

    } catch (final IOException e) {
    } finally {
        fSourceZip.delete();
    }
}

From source file:net.minecraftforge.fml.common.FMLModContainer.java

public Properties searchForVersionProperties() {
    try {/*from  w  ww. jav a  2 s  . com*/
        FMLLog.log(getModId(), Level.DEBUG,
                "Attempting to load the file version.properties from %s to locate a version number for %s",
                getSource().getName(), getModId());
        Properties version = null;
        if (getSource().isFile()) {
            ZipFile source = new ZipFile(getSource());
            ZipEntry versionFile = source.getEntry("version.properties");
            if (versionFile != null) {
                version = new Properties();
                version.load(source.getInputStream(versionFile));
            }
            source.close();
        } else if (getSource().isDirectory()) {
            File propsFile = new File(getSource(), "version.properties");
            if (propsFile.exists() && propsFile.isFile()) {
                version = new Properties();
                FileInputStream fis = new FileInputStream(propsFile);
                version.load(fis);
                fis.close();
            }
        }
        return version;
    } catch (Exception e) {
        Throwables.propagateIfPossible(e);
        FMLLog.log(getModId(), Level.TRACE, "Failed to find a usable version.properties file");
        return null;
    }
}

From source file:com.edgenius.wiki.service.impl.BackupServiceImpl.java

public String getFileComment(File zipFile) {
    String comment = "";
    ZipFile zip = null;/*from  w  w w .ja v a  2  s .co m*/
    try {
        zip = new ZipFile(zipFile);
        ZipEntry entry = zip.getEntry(COMMENT_FILE_NAME);
        if (entry != null) {
            comment = IOUtils.toString(zip.getInputStream(entry));
        }
    } catch (Exception e) {
        log.info("backup/restore file comment not available:" + zipFile.getAbsolutePath());
    } finally {
        if (zip != null)
            try {
                zip.close();
            } catch (Exception e) {
            }
    }

    return comment;
}