Example usage for java.util.zip ZipOutputStream write

List of usage examples for java.util.zip ZipOutputStream write

Introduction

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

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:com.webpagebytes.cms.controllers.FlatStorageImporterExporter.java

protected void exportSitePages(ZipOutputStream zos, String path) throws WPBIOException {
    try {/*from  w w w  .j a v a 2  s .c  o  m*/
        List<WPBPage> pages = dataStorage.getAllRecords(WPBPage.class);
        for (WPBPage page : pages) {
            String pageXmlPath = path + page.getExternalKey() + "/" + "metadata.xml";
            Map<String, Object> map = new HashMap<String, Object>();
            exporter.export(page, map);
            ZipEntry metadataZe = new ZipEntry(pageXmlPath);
            zos.putNextEntry(metadataZe);
            exportToXMLFormat(map, zos);
            zos.closeEntry();
            String pageSourcePath = path + page.getExternalKey() + "/" + "pageSource.txt";
            String pageSource = page.getHtmlSource() != null ? page.getHtmlSource() : "";
            ZipEntry pageSourceZe = new ZipEntry(pageSourcePath);
            zos.putNextEntry(pageSourceZe);
            zos.write(pageSource.getBytes("UTF-8"));
            zos.closeEntry();

            String parametersPath = String.format(PATH_SITE_PAGES_PARAMETERS, page.getExternalKey());
            ZipEntry paramsZe = new ZipEntry(parametersPath);
            zos.putNextEntry(paramsZe);
            zos.closeEntry();

            exportParameters(page.getExternalKey(), zos, parametersPath);

        }
    } catch (IOException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        throw new WPBIOException("Cannot export site web pages to Zip", e);
    }
}

From source file:com.webpagebytes.cms.controllers.FlatStorageImporterExporter.java

protected void exportPageModules(ZipOutputStream zos, String path) throws WPBIOException {
    try {/*from  w  w w .ja  v  a  2  s.  c  o  m*/
        List<WPBPageModule> modules = dataStorage.getAllRecords(WPBPageModule.class);
        for (WPBPageModule module : modules) {
            String moduleXmlPath = path + module.getExternalKey() + "/" + "metadata.xml";
            Map<String, Object> map = new HashMap<String, Object>();
            exporter.export(module, map);
            ZipEntry metadataZe = new ZipEntry(moduleXmlPath);
            zos.putNextEntry(metadataZe);
            exportToXMLFormat(map, zos);
            zos.closeEntry();
            String moduleSourcePath = path + module.getExternalKey() + "/" + "moduleSource.txt";
            String moduleSource = module.getHtmlSource() != null ? module.getHtmlSource() : "";
            ZipEntry pageSourceZe = new ZipEntry(moduleSourcePath);
            zos.putNextEntry(pageSourceZe);
            zos.write(moduleSource.getBytes("UTF-8"));
            zos.closeEntry();
        }
    } catch (IOException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        throw new WPBIOException("Cannot export site web page modules to Zip", e);
    }
}

From source file:com.webpagebytes.cms.controllers.FlatStorageImporterExporter.java

protected void exportArticles(ZipOutputStream zos, String path) throws WPBIOException {
    try {//from w  w  w. java  2s . c o m
        List<WPBArticle> articles = dataStorage.getAllRecords(WPBArticle.class);
        for (WPBArticle article : articles) {
            String articleXmlPath = path + article.getExternalKey() + "/" + "metadata.xml";
            Map<String, Object> map = new HashMap<String, Object>();
            exporter.export(article, map);
            ZipEntry metadataZe = new ZipEntry(articleXmlPath);
            zos.putNextEntry(metadataZe);
            exportToXMLFormat(map, zos);
            zos.closeEntry();
            String articleSourcePath = path + article.getExternalKey() + "/" + "articleSource.txt";
            String pageSource = article.getHtmlSource() != null ? article.getHtmlSource() : "";
            ZipEntry pageSourceZe = new ZipEntry(articleSourcePath);
            zos.putNextEntry(pageSourceZe);
            zos.write(pageSource.getBytes("UTF-8"));
            zos.closeEntry();
        }
    } catch (IOException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        throw new WPBIOException("Cannot export articles to Zip", e);
    }
}

From source file:edu.umd.cs.marmoset.modelClasses.Project.java

public void exportProject(Connection conn, OutputStream out) throws SQLException, IOException {
    ZipOutputStream zipOutputStream = new ZipOutputStream(out);

    TestSetup testSetup = TestSetup.lookupByTestSetupPK(getTestSetupPK(), conn);
    if (testSetup != null) {

        // Test-setup
        zipOutputStream.putNextEntry(new ZipEntry(getProjectNumber() + "-test-setup.zip"));
        zipOutputStream.write(testSetup.downloadArchive(conn));

        // Canonical
        Submission canonical = Submission.lookupBySubmissionPK(
                (TestRun.lookupByTestRunPK(testSetup.getTestRunPK(), conn)).getSubmissionPK(), conn);
        zipOutputStream.putNextEntry(new ZipEntry(getProjectNumber() + "-canonical.zip"));
        zipOutputStream.write(canonical.downloadArchive(conn));
    }/*  w  w  w.  ja v  a2  s . co m*/

    // Serialize the project object itself and include it
    zipOutputStream.putNextEntry(new ZipEntry(getProjectNumber() + "-project.out"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(baos);
    objectOutputStream.writeObject(this);
    objectOutputStream.flush();
    objectOutputStream.close();
    zipOutputStream.write(baos.toByteArray());

    // project starter files, if any
    if (getArchivePK() != null) {
        zipOutputStream.putNextEntry(new ZipEntry(getProjectNumber() + "-project-starter-files.zip"));
        zipOutputStream.write(getBaselineZip(conn));
    }

    zipOutputStream.close();
}

From source file:com.cdd.bao.importer.ImportControlledVocab.java

private void writeMappedAssays() throws IOException {
    File f = new File(dstFN);
    Util.writeln("Writing to: " + f.getCanonicalPath());

    ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(f));

    for (int n = 0; n < srcRows.length(); n++) {
        JSONObject json = null;/*www . j  ava2 s. com*/
        try {
            json = map.createAssay(srcRows.getJSONObject(n), schema, treeCache);
        } catch (Exception ex) {
            zip.close();
            throw new IOException("Failed to translate assay at row #" + (n + 1), ex);
        }

        if (!json.has("uniqueID")) {
            Util.writeln("** Row#" + (n + 1) + " is missing an identifier: cannot proceed.");
            break;
        }

        String fn = "assay_" + (n + 1) + "_";
        for (char ch : json.getString("uniqueID").toCharArray()) {
            if (ch == ' ')
                fn += ' ';
            else if (ch == '_' || ch == ':' || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z')
                    || (ch >= 'A' && ch <= 'Z'))
                fn += ch;
            else
                fn += '-';
        }

        zip.putNextEntry(new ZipEntry(fn + ".json"));
        zip.write(json.toString(2).getBytes());
        zip.closeEntry();
    }

    zip.close();
}

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

private File createDefaultTheme(String[] themePaths, String[] filterPaths) {
    try {// w w w.  j  ava 2  s .com
        @SuppressWarnings("unchecked")
        Set<String> libs = servletContext.getResourcePaths("/WEB-INF/lib");
        Set<String> zipResources = new HashSet<String>();
        File back = File.createTempFile("devproof-defaulttheme-", ".zip");
        FileOutputStream fos = new FileOutputStream(back);
        ZipOutputStream zos = new ZipOutputStream(fos);
        if (libs.isEmpty()) {
            // development variant
            Resource root[] = applicationContext.getResources("classpath*:/");
            for (String ext : ThemeConstants.ALLOWED_THEME_EXT) {
                for (String themePath : themePaths) {
                    final Resource resources[];
                    if (themePath.endsWith("/")) {
                        resources = applicationContext.getResources("classpath*:" + themePath + "**/*" + ext);
                    } else {
                        resources = applicationContext.getResources("classpath*:" + themePath + ext);
                    }
                    for (Resource r : resources) {
                        String zipPath = getZipPath(root, r);
                        if (zipPath != null && !isFiltered(null, filterPaths, zipPath)
                                && !zipResources.contains(zipPath)) {
                            zipResources.add(zipPath);
                            ZipEntry ze = new ZipEntry(zipPath);
                            zos.putNextEntry(ze);
                            InputStream is = r.getInputStream();
                            byte[] file = new byte[is.available()];
                            is.read(file);
                            is.close();
                            zos.write(file);
                            zos.closeEntry();
                        }
                    }
                }
            }
        } else {
            // prod variant
            for (String lib : libs) {
                URL url = servletContext.getResource(lib);
                JarFile file = new JarFile(url.getFile());
                Enumeration<JarEntry> entries = file.entries();
                while (entries.hasMoreElements()) {
                    JarEntry jarEntry = entries.nextElement();
                    if (!isFiltered(themePaths, filterPaths, jarEntry.getName())
                            && !zipResources.contains(jarEntry.getName())) {
                        zipResources.add(jarEntry.getName());
                        ZipEntry ze = new ZipEntry(jarEntry.getName());
                        InputStream is = file.getInputStream(jarEntry);
                        byte[] content = new byte[is.available()];
                        is.read(content);
                        is.close();
                        zos.putNextEntry(ze);
                        zos.write(content);
                        zos.closeEntry();
                    }
                }
            }
        }
        zos.finish();
        zos.close();
        fos.close();
        return back;
    } catch (FileNotFoundException e) {
        logger.error("Unknown: ", e);
    } catch (IOException e) {
        logger.error("Unknown: ", e);
    }
    return null;
}

From source file:brut.androlib.res.AndrolibResources.java

public void installFramework(File frameFile, String tag) throws AndrolibException {
    InputStream in = null;/*from  ww w  . j  a v  a 2 s  .c o  m*/
    ZipOutputStream out = null;
    try {
        ZipFile zip = new ZipFile(frameFile);
        ZipEntry entry = zip.getEntry("resources.arsc");

        if (entry == null) {
            throw new AndrolibException("Can't find resources.arsc file");
        }

        in = zip.getInputStream(entry);
        byte[] data = IOUtils.toByteArray(in);

        ARSCData arsc = ARSCDecoder.decode(new ByteArrayInputStream(data), true, true);
        publicizeResources(data, arsc.getFlagsOffsets());

        File outFile = new File(getFrameworkDir(),
                String.valueOf(arsc.getOnePackage().getId()) + (tag == null ? "" : '-' + tag) + ".apk");

        out = new ZipOutputStream(new FileOutputStream(outFile));
        out.setMethod(ZipOutputStream.STORED);
        CRC32 crc = new CRC32();
        crc.update(data);
        entry = new ZipEntry("resources.arsc");
        entry.setSize(data.length);
        entry.setCrc(crc.getValue());
        out.putNextEntry(entry);
        out.write(data);
        zip.close();
        LOGGER.info("Framework installed to: " + outFile);
    } catch (IOException ex) {
        throw new AndrolibException(ex);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:org.bonitasoft.engine.test.APITestUtil.java

protected byte[] createTestPageContent(final String pageName, final String displayName,
        final String description) throws Exception {
    try {//from  w w  w  . j av a  2 s. c o m
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final ZipOutputStream zos = new ZipOutputStream(baos);
        zos.putNextEntry(new ZipEntry("Index.groovy"));
        zos.write("return \"\";".getBytes());

        zos.putNextEntry(new ZipEntry("page.properties"));
        final StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("name=");
        stringBuilder.append(pageName);
        stringBuilder.append("\n");
        stringBuilder.append("displayName=");
        stringBuilder.append(displayName);
        stringBuilder.append("\n");
        stringBuilder.append("description=");
        stringBuilder.append(description);
        stringBuilder.append("\n");
        zos.write(stringBuilder.toString().getBytes());

        zos.closeEntry();
        return baos.toByteArray();
    } catch (final IOException e) {
        throw new BonitaException(e);
    }
}

From source file:org.etudes.mneme.impl.ExportQtiServiceImpl.java

/**
 * Write Xml document to zip package// w  ww .  j  a  va  2s .c  o  m
 * 
 * @param zip
 * @param fileTitle
 * @param document
 */
protected void writeDocumentToZip(ZipOutputStream zip, String subFolder, String fileTitle, Document document) {
    try {
        if (subFolder != null)
            zip.putNextEntry(new ZipEntry(subFolder));
        zip.putNextEntry(new ZipEntry(fileTitle));
        zip.write(Xml.writeDocumentToString(document).getBytes("UTF-8"));
        zip.closeEntry();
        zip.flush();
    } catch (IOException e) {
        M_log.warn("zipSubmissionsQuestion: zipping question: " + e.toString());
    }
}

From source file:org.evosuite.utils.ReportGenerator.java

/**
 * <p>//from   w  ww .  ja v a 2  s. com
 * writeCSVData
 * </p>
 * 
 * @param filename
 *            a {@link java.lang.String} object.
 * @param data
 *            a {@link java.util.List} object.
 */
protected void writeCSVData(String filename, List<?>... data) {
    try {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(filename, false));
        out.putNextEntry(new ZipEntry(filename.replace(".gz", "")));

        //BufferedWriter out = new BufferedWriter(new FileWriter(filename, true));
        int length = Integer.MAX_VALUE;

        out.write("Generation,Fitness,Coverage,Size,Length,AverageLength,Evaluations,Tests,Statements,Time\n"
                .getBytes());
        for (List<?> d : data) {
            length = Math.min(length, d.size());
        }
        for (int i = 0; i < length; i++) {
            out.write(("" + i).getBytes());
            for (List<?> d : data) {
                out.write(("," + d.get(i)).getBytes());
            }
            out.write("\n".getBytes());
        }
        out.close();
    } catch (IOException e) {
        logger.info("Exception while writing CSV data: " + e);
    }
}