Example usage for java.util.zip ZipInputStream getNextEntry

List of usage examples for java.util.zip ZipInputStream getNextEntry

Introduction

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

Prototype

public ZipEntry getNextEntry() throws IOException 

Source Link

Document

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Usage

From source file:com.silverpeas.wiki.WikiInstanciator.java

protected void createPages(File directory, String componentId) throws IOException, WikiException {
    ZipInputStream zipFile = new ZipInputStream(loader.getResourceAsStream("pages.zip"));
    ZipEntry page = zipFile.getNextEntry();
    while (page != null) {
        String pageName = page.getName();
        File newPage = new File(directory, pageName);
        if (page.isDirectory()) {
            newPage.mkdirs();/*from   ww w  .j  a va2s  .c o m*/
        } else {
            FileUtil.writeFile(newPage, new InputStreamReader(zipFile, "UTF-8"));
            PageDetail newPageDetail = new PageDetail();
            newPageDetail.setInstanceId(componentId);
            newPageDetail.setPageName(pageName.substring(0, pageName.lastIndexOf('.')));
            wikiDAO.createPage(newPageDetail);
            zipFile.closeEntry();
            page = zipFile.getNextEntry();
        }
    }
}

From source file:eu.scape_project.archiventory.container.ZipContainer.java

/**
 * Extracts a zip file specified by the zipFilePath to a directory specified by
 * destDirectory (will be created if does not exists)
 * @param containerFileName/*from www  . j av a 2 s .  c o  m*/
 * @param destDirectory
 * @throws IOException
 */
public void unzip(String containerFileName, InputStream containerFileStream) throws IOException {
    extractDirectoryName = "/tmp/archiventory_" + RandomStringUtils.randomAlphabetic(10) + "/";
    File destDir = new File(extractDirectoryName);
    destDir.mkdir();
    String subDestDirStr = extractDirectoryName + containerFileName + "/";
    File subDestDir = new File(subDestDirStr);
    subDestDir.mkdir();
    ZipInputStream zipIn = new ZipInputStream(containerFileStream);
    ZipEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        String filePath = subDestDirStr + entry.getName();
        if (!entry.isDirectory()) {
            extractFile(zipIn, filePath);
        } else {
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:com.geocent.owf.openlayers.handler.KmzHandler.java

@Override
public String handleContent(HttpServletResponse response, InputStream responseStream) throws IOException {
    ZipInputStream zis = new ZipInputStream(responseStream);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];

    ZipEntry ze = zis.getNextEntry();
    while (ze != null) {
        if (ze.getName().endsWith("kml")) {
            int len;
            while ((len = zis.read(buffer)) > 0) {
                baos.write(buffer, 0, len);
            }// ww  w  .j a v a  2  s  .com

            response.setContentType(ContentTypes.KML.getContentType());
            return new String(baos.toByteArray(), Charset.defaultCharset());
        }

        ze = zis.getNextEntry();
    }

    throw new IOException("Missing KML file entry.");
}

From source file:com.arykow.autotools.generator.App.java

private void generate() throws Exception {
    File directory = new File(projectDirectory);
    if (!directory.isDirectory()) {
        throw new RuntimeException();
    }//from   w w  w. j ava  2  s .co  m
    File output = new File(directory, projectName);
    if (output.exists()) {
        if (projectForced) {
            if (!output.isDirectory()) {
                throw new RuntimeException();
            }
        } else {
            throw new RuntimeException();
        }
    } else if (!output.mkdir()) {
        throw new RuntimeException();
    }

    CodeSource src = getClass().getProtectionDomain().getCodeSource();
    if (src != null) {
        URL jar = src.getLocation();
        ZipInputStream zip = new ZipInputStream(jar.openStream());
        while (true) {
            ZipEntry e = zip.getNextEntry();
            if (e == null)
                break;
            if (!e.isDirectory() && e.getName().startsWith(String.format("%s", templateName))) {
                // generate(output, e);

                StringWriter writer = new StringWriter();
                IOUtils.copy(zip, writer);
                String content = writer.toString();

                Map<String, String> expressions = new HashMap<String, String>();
                expressions.put("author", "Karim DRIDI");
                expressions.put("name_undescore", projectName);
                expressions.put("license", "<Place your desired license here.>");
                expressions.put("name", projectName);
                expressions.put("version", "1.0");
                expressions.put("copyright", "Your copyright notice");
                expressions.put("description", "Hello World in C++,");

                for (Map.Entry<String, String> entry : expressions.entrySet()) {
                    content = content.replaceAll(String.format("<project\\.%s>", entry.getKey()),
                            entry.getValue());
                }

                String name = e.getName().substring(templateName.length() + 1);
                if ("gitignore".equals(name)) {
                    name = ".gitignore";
                }
                File file = new File(output, name);
                File parent = file.getParentFile();
                if (parent.exists()) {
                    if (!parent.isDirectory()) {
                        throw new RuntimeException();
                    }
                } else {
                    if (!parent.mkdirs()) {
                        throw new RuntimeException();
                    }
                }
                OutputStream stream = new FileOutputStream(file);
                IOUtils.copy(new StringReader(content), stream);
                IOUtils.closeQuietly(stream);
            }
        }
    }
}

From source file:com.celements.photo.utilities.Unzip.java

private ByteArrayOutputStream findAndExtractFile(String filename, ZipInputStream zipIn) throws IOException {
    ByteArrayOutputStream out = null;

    for (ZipEntry entry = zipIn.getNextEntry(); zipIn.available() > 0; entry = zipIn.getNextEntry()) {
        if (!entry.isDirectory() && entry.getName().equals(filename)) {
            // read the data and write it to the OutputStream
            int count;
            byte[] data = new byte[BUFFER];
            out = new ByteArrayOutputStream();
            BufferedOutputStream byteOut = new BufferedOutputStream(out, BUFFER);
            while ((count = zipIn.read(data, 0, BUFFER)) != -1) {
                byteOut.write(data, 0, count);
            }/*  ww w .  j  a v a  2 s.  c  o m*/
            byteOut.flush();
            break;
        }
    }

    zipIn.close();
    return out;
}

From source file:de.knowwe.revisions.upload.UploadRevisionZip.java

@SuppressWarnings("unchecked")
@Override/*  w  ww . ja  va  2 s  .  c o m*/
public void execute(UserActionContext context) throws IOException {

    HashMap<String, String> pages = new HashMap<>();
    List<FileItem> items = null;
    String zipname = null;
    try {
        items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(context.getRequest());
    } catch (FileUploadException e) {
        throw new IOException("error during processing upload", e);
    }

    for (FileItem item : items) {
        zipname = item.getName();
        InputStream filecontent = item.getInputStream();

        ZipInputStream zin = new ZipInputStream(filecontent);
        ZipEntry ze;

        while ((ze = zin.getNextEntry()) != null) {
            String name = ze.getName();
            if (!name.contains("/")) {
                // this is an article
                String title = Strings.decodeURL(name);
                title = title.substring(0, title.length() - 4);
                String content = IOUtils.toString(zin, "UTF-8");
                zin.closeEntry();
                pages.put(title, content);
            } else {
                // TODO: what to do here?
                // this is an attachment
                // String[] splittedName = name.split("/");
                // String title = Strings.decodeURL(splittedName[0]);
                // String filename = Strings.decodeURL(splittedName[1]);
                //
                // System.out.println("Attachment: " + name);
                // String content = IOUtils.toString(zin, "UTF-8");
                // Environment.getInstance().getWikiConnector().storeAttachment(title,
                // filename,
                // context.getUserName(), zin);
                zin.closeEntry();
            }
        }
        zin.close();
        filecontent.close();
    }
    if (zipname != null) {
        UploadedRevision rev = new UploadedRevision(context.getWeb(), pages, zipname);
        RevisionManager.getRM(context).setUploadedRevision(rev);
    }
    context.sendRedirect("../Wiki.jsp?page=" + context.getTitle());

}

From source file:ch.astina.hesperid.web.services.dbmigration.impl.ClasspathMigrationResolver.java

public Set<Migration> resolve() {
    Set<Migration> migrations = new HashSet<Migration>();

    try {//from  w ww  .  j a va 2  s.  c  o  m
        Enumeration<URL> enumeration = getClass().getClassLoader().getResources(classPath);

        while (enumeration.hasMoreElements()) {

            URL url = enumeration.nextElement();

            if (url.toExternalForm().startsWith("jar:")) {
                String file = url.getFile();

                file = file.substring(0, file.indexOf("!"));
                file = file.substring(file.indexOf(":") + 1);

                InputStream is = new FileInputStream(file);

                ZipInputStream zip = new ZipInputStream(is);

                ZipEntry ze;

                while ((ze = zip.getNextEntry()) != null) {
                    if (!ze.isDirectory() && ze.getName().startsWith(classPath)
                            && ze.getName().endsWith(".sql")) {

                        Resource r = new UrlResource(getClass().getClassLoader().getResource(ze.getName()));

                        String version = versionExtractor.extractVersion(r.getFilename());
                        migrations.add(migrationFactory.create(version, r));
                    }
                }
            } else {
                File file = new File(url.getFile());

                for (String s : file.list()) {
                    Resource r = new UrlResource(getClass().getClassLoader().getResource(classPath + "/" + s));

                    String version = versionExtractor.extractVersion(r.getFilename());
                    migrations.add(migrationFactory.create(version, r));
                }
            }
        }
    } catch (Exception e) {
        logger.error("Error while resolving migrations", e);
    }

    return migrations;
}

From source file:com.wavemaker.tools.project.upgrade.five_dot_zero.WebXmlUpgradeTask.java

@Override
public void doUpgrade(Project project, UpgradeInfo upgradeInfo) {

    File webXml = project.getWebXmlFile();
    if (webXml.exists()) {
        try {/*from w w  w . j av a2  s  .c o m*/
            File webXmlBak = project.getWebInfFolder().getFile(WEB_XML_BACKUP);
            webXmlBak.getContent().write(webXml);
            webXml.delete();
            File userWebXml = project.getWebInfFolder().getFile(ProjectConstants.USER_WEB_XML);
            InputStream resourceStream = this.getClass().getClassLoader()
                    .getResourceAsStream(ProjectManager._TEMPLATE_APP_RESOURCE_NAME);
            ZipInputStream resourceZipStream = new ZipInputStream(resourceStream);

            ZipEntry zipEntry = null;

            while ((zipEntry = resourceZipStream.getNextEntry()) != null) {
                if ("webapproot/WEB-INF/user-web.xml".equals(zipEntry.getName())) {
                    Writer writer = userWebXml.getContent().asWriter();
                    IOUtils.copy(resourceZipStream, writer);
                    writer.close();
                }
            }

            resourceZipStream.close();
            resourceStream.close();
        } catch (IOException e) {
            throw new WMRuntimeException(e);
        }

        upgradeInfo.addMessage("The web.xml file has changed.  If you have custom"
                + "modifications, please copy them from " + WEB_XML_BACKUP + " to the new user-web.xml.");
    }
}

From source file:com.wavemaker.tools.project.upgrade.UpgradeTemplateFile.java

@Override
public void doUpgrade(Project project, UpgradeInfo upgradeInfo) {

    if (this.relativePath == null) {
        throw new WMRuntimeException("No file provided");
    }//  w w  w.  j av  a 2s  .  co m

    try {
        File localFile = project.getRootFolder().getFile(this.relativePath);

        InputStream resourceStream = this.getClass().getClassLoader()
                .getResourceAsStream(ProjectManager._TEMPLATE_APP_RESOURCE_NAME);
        ZipInputStream resourceZipStream = new ZipInputStream(resourceStream);

        ZipEntry zipEntry = null;

        while ((zipEntry = resourceZipStream.getNextEntry()) != null) {
            if (this.relativePath.equals(zipEntry.getName())) {
                Writer writer = localFile.getContent().asWriter();
                IOUtils.copy(resourceZipStream, writer);
                writer.close();
            }
        }

        resourceZipStream.close();
        resourceStream.close();
    } catch (IOException e) {
        throw new WMRuntimeException(e);
    }

    if (this.message != null) {
        upgradeInfo.addMessage(this.message);
    }
}

From source file:be.fedict.eid.dss.document.zip.ZIPSignatureOutputStream.java

@Override
public void close() throws IOException {
    super.close();

    byte[] signatureData = toByteArray();

    /*/* w  w  w  .  ja  v a 2 s. c o m*/
     * Copy the original ZIP content.
     */
    ZipOutputStream zipOutputStream = new ZipOutputStream(this.targetOutputStream);
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(this.originalZipFile));
    ZipEntry zipEntry;
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (!zipEntry.getName().equals(ODFUtil.SIGNATURE_FILE)) {
            ZipEntry newZipEntry = new ZipEntry(zipEntry.getName());
            zipOutputStream.putNextEntry(newZipEntry);
            LOG.debug("copying " + zipEntry.getName());
            IOUtils.copy(zipInputStream, zipOutputStream);
        }
    }
    zipInputStream.close();

    /*
     * Add the XML signature file to the ZIP package.
     */
    zipEntry = new ZipEntry(ODFUtil.SIGNATURE_FILE);
    LOG.debug("writing " + zipEntry.getName());
    zipOutputStream.putNextEntry(zipEntry);
    IOUtils.write(signatureData, zipOutputStream);
    zipOutputStream.close();
}