Example usage for java.util.zip ZipFile getInputStream

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

Introduction

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

Prototype

public InputStream getInputStream(ZipEntry entry) throws IOException 

Source Link

Document

Returns an input stream for reading the contents of the specified zip file entry.

Usage

From source file:com.mc.printer.model.utils.ZipHelper.java

/**
 * jar????/*ww  w.  j  a  v a 2s. c  o m*/
 *
 * @param jar ????jar
 * @param subDir jar???????
 * @param loc ????
 * @param force ????
 * @return
 */
public static boolean unZip(String jar, String subDir, String loc, boolean force) {
    try {
        File base = new File(loc);
        if (!base.exists()) {
            base.mkdirs();
        }

        ZipFile zip = new ZipFile(new File(jar));
        Enumeration<? extends ZipEntry> entrys = zip.entries();
        while (entrys.hasMoreElements()) {
            ZipEntry entry = entrys.nextElement();
            String name = entry.getName();
            if (!name.startsWith(subDir)) {
                continue;
            }
            //subDir
            name = name.replace(subDir, "").trim();
            if (name.length() < 2) {
                log.debug(name + "  < 2");
                continue;
            }
            if (entry.isDirectory()) {
                File dir = new File(base, name);
                if (!dir.exists()) {
                    dir.mkdirs();
                    log.debug("create directory");
                } else {
                    log.debug("the directory is existing.");
                }
                log.debug(name + " is a directory");
            } else {
                File file = new File(base, name);
                if (file.exists() && force) {
                    file.delete();
                }
                if (!file.exists()) {
                    InputStream in = zip.getInputStream(entry);
                    FileUtils.copyInputStreamToFile(in, file);
                    log.debug("create file.");
                    in.close();
                } else {
                    log.debug("the file is existing");
                }
                log.debug(name + " is not a directory");
            }
        }
        zip.close();
    } catch (ZipException ex) {
        ex.printStackTrace();
        log.error("file unzip failed.", ex);
        return false;
    } catch (IOException ex) {
        ex.printStackTrace();
        log.error("file IO failed", ex);
        return false;
    }
    return true;
}

From source file:org.geoserver.wps.gs.download.DownloadAnimationProcessTest.java

BufferedImage grabImageFromZip(File file, String entryName) throws IOException {
    ZipFile zipFile = new ZipFile(file);

    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.getName().equalsIgnoreCase(entryName)) {
            try (InputStream stream = zipFile.getInputStream(entry)) {
                return ImageIO.read(stream);
            }/*from  w w  w  .  j a v a 2  s  .co  m*/
        }
    }

    return null;
}

From source file:nl.ordina.bag.etl.loader.ExtractLoader.java

protected BAGExtractLevering processFile(ZipFile zipFile, final String filename)
        throws JAXBException, IOException {
    ZipEntry entry = zipFile.getEntry(filename);
    return entry == null ? null
            : XMLMessageBuilder.getInstance(BAGExtractLevering.class).handle(zipFile.getInputStream(entry));
}

From source file:io.apiman.common.plugin.PluginClassLoader.java

/**
 * Searches the plugin artifact ZIP and all dependency ZIPs for a zip entry for
 * the given fully qualified class name.
 * @param className name of class// w w w . j  ava2 s .c o m
 * @throws IOException if an I/O error has occurred
 */
protected InputStream findClassContent(String className) throws IOException {
    String primaryArtifactEntryName = "WEB-INF/classes/" + className.replace('.', '/') + ".class";
    String dependencyEntryName = className.replace('.', '/') + ".class";
    ZipEntry entry = this.pluginArtifactZip.getEntry(primaryArtifactEntryName);
    if (entry != null) {
        return this.pluginArtifactZip.getInputStream(entry);
    }
    for (ZipFile zipFile : this.dependencyZips) {
        entry = zipFile.getEntry(dependencyEntryName);
        if (entry != null) {
            return zipFile.getInputStream(entry);
        }
    }
    return null;
}

From source file:org.eclipse.tycho.nexus.internal.plugin.UnzipRepositoryPluginITCase.java

private String getTestData(String localArtifactPath, String testDataPrefix) throws ZipException, IOException {
    ZipFile zipFile = new ZipFile(testData().resolveFile(testDataPrefix + localArtifactPath));
    String expectedContent;/*  ww w.j  a v  a2s  . co  m*/
    try {
        ZipEntry entry = zipFile.getEntry(POM_PROPERTIES_PATH_IN_ZIP);
        expectedContent = IOUtils.toString(zipFile.getInputStream(entry));
    } finally {
        zipFile.close();
    }
    return expectedContent;
}

From source file:org.devproof.portal.core.module.theme.service.ThemeServiceImpl.java

@Override
public ValidationKey validateTheme(File themeArchive) {
    try {/*from  w ww .j av  a 2 s.  c om*/
        ZipFile zip = new ZipFile(themeArchive);
        ZipEntry entry = zip.getEntry("theme.properties");
        if (entry != null) {
            InputStream is = zip.getInputStream(entry);
            ThemeBean bean = getBeanFromInputStream("", is);
            if (StringUtils.isBlank(bean.getAuthor()) || StringUtils.isBlank(bean.getUrl())
                    || StringUtils.isBlank(bean.getPortalThemeVersion())
                    || StringUtils.isBlank(bean.getPortalVersion()) || StringUtils.isBlank(bean.getTheme())) {
                return ValidationKey.INVALID_DESCRIPTOR_FILE;
            } else {
                if (themeVersion.equals(bean.getPortalThemeVersion())) {
                    return ValidationKey.VALID;
                } else {
                    return ValidationKey.WRONG_VERSION;
                }
            }
        }
        return ValidationKey.MISSING_DESCRIPTOR_FILE;
    } catch (ZipException e) {
        logger.warn(themeArchive.toString() + " was not valid", e);
        return ValidationKey.NOT_A_JARFILE;
    } catch (IOException e) {
        logger.warn(themeArchive.toString() + " was not valid", e);
        return ValidationKey.NOT_A_JARFILE;
    }
}

From source file:com.axelor.apps.base.service.imports.importer.Importer.java

protected void unZip(File file, File directory) throws ZipException, IOException {

    File extractFile = null;//from   w w  w  .j a v  a  2  s.c  o m
    FileOutputStream fileOutputStream = null;
    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        try {
            ZipEntry entry = entries.nextElement();
            InputStream entryInputStream = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            extractFile = new File(directory, entry.getName());
            if (entry.isDirectory()) {
                extractFile.mkdirs();
                continue;
            } else {
                extractFile.getParentFile().mkdirs();
                extractFile.createNewFile();
            }

            fileOutputStream = new FileOutputStream(extractFile);
            while ((bytesRead = entryInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }

        } catch (IOException ioException) {
            log.error(ioException.getMessage());
            continue;
        } finally {
            if (fileOutputStream == null) {
                continue;
            }
            try {
                fileOutputStream.close();
            } catch (IOException e) {
            }
        }
    }

    zipFile.close();

}

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

/**
 * Extract single entry of zip file.//w  ww.  j av a2  s .  c  o  m
 *
 * @param pOutputDir where to store the unzipped entry.
 * @param pZipFile file containing zipped files.
 * @param pEntry one entry to extract.
 * @throws IOException missing access rights?
 */
private static void extractEntry(File pOutputDir, ZipFile pZipFile, ZipEntry pEntry) throws IOException {
    File destinationFilePath = new File(pOutputDir, pEntry.getName());
    if (pEntry.isDirectory()) {
        // The following command is neccessary to create also
        // empty directories.
        if (destinationFilePath.mkdirs()) {
            LOGGER.trace("create directory: '{}'", destinationFilePath.getPath());
        }
    } else {
        //create directories if required.
        if (destinationFilePath.getParentFile().mkdirs()) {
            LOGGER.trace("create directory: '{}'", destinationFilePath.getPath());
        }
    }
    //if the entry is directory, leave it. Otherwise extract it.
    if (!pEntry.isDirectory()) {
        LOGGER.debug("Extracting " + destinationFilePath);

        /*
         * Get the InputStream for current entry
         * of the zip file using
         *
         * InputStream getInputStream(Entry entry) method.
         */
        int noOfBytes;
        byte buffer[] = new byte[1024];

        try (BufferedInputStream bis = new BufferedInputStream(pZipFile.getInputStream(pEntry));
                FileOutputStream fos = new FileOutputStream(destinationFilePath);
                BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);) {
            /*
             * read the current entry from the zip file, extract it
             * and write the extracted file.
             */
            while ((noOfBytes = bis.read(buffer, 0, buffer.length)) != -1) {
                bos.write(buffer, 0, noOfBytes);
            }
            //flush the output stream.
            bos.flush();
        }
    }
}

From source file:com.samczsun.helios.LoadedFile.java

public void reset() throws IOException {
    files.clear();//from ww  w. j  a  v  a 2s.co  m
    classes.clear();
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
        while (enumeration.hasMoreElements()) {
            ZipEntry zipEntry = enumeration.nextElement();
            if (!zipEntry.isDirectory()) {
                load(zipEntry.getName(), zipFile.getInputStream(zipEntry));
            }
        }
    } catch (ZipException e) { //Probably not a ZIP file
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            load(this.name, inputStream);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:de.tudarmstadt.ukp.clarin.webanno.project.page.ImportUtil.java

/**
 * copy guidelines from the exported project
 * @param zip the ZIP file./*from  ww w .j  a v a  2 s .c o  m*/
 * @param aProject the project.
 * @param aRepository the repository service.
 * @throws IOException if an I/O error occurs.
 */
@SuppressWarnings("rawtypes")
public static void createProjectGuideline(ZipFile zip, Project aProject, RepositoryService aRepository)
        throws IOException {
    for (Enumeration zipEnumerate = zip.entries(); zipEnumerate.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) zipEnumerate.nextElement();

        // Strip leading "/" that we had in ZIP files prior to 2.0.8 (bug #985)
        String entryName = normalizeEntryName(entry);

        if (entryName.startsWith(GUIDELINE)) {
            String filename = FilenameUtils.getName(entry.getName());
            File guidelineDir = aRepository.getGuidelinesFile(aProject);
            FileUtils.forceMkdir(guidelineDir);
            FileUtils.copyInputStreamToFile(zip.getInputStream(entry), new File(guidelineDir, filename));

            LOG.info("Imported guideline [" + filename + "] for project [" + aProject.getName() + "] with id ["
                    + aProject.getId() + "]");
        }
    }
}