Example usage for java.util.zip ZipOutputStream flush

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

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the compressed output stream.

Usage

From source file:edu.harvard.iq.dvn.core.analysis.NetworkDataServiceBean.java

private void addZipEntry(ZipOutputStream zout, String inputFileName, String outputFileName) throws IOException {
    FileInputStream tmpin = new FileInputStream(inputFileName);
    byte[] dataBuffer = new byte[8192];
    int i = 0;//from  w  w  w . j av a  2 s .  c o m

    ZipEntry e = new ZipEntry(outputFileName);
    zout.putNextEntry(e);

    while ((i = tmpin.read(dataBuffer)) > 0) {
        zout.write(dataBuffer, 0, i);
        zout.flush();
    }
    tmpin.close();
    zout.closeEntry();
}

From source file:de.hybris.platform.impex.jalo.ImpExMediasImportTest.java

/**
 * Calls the <code>importData</code> method of given handler with an zip-file path in different formats.
 * //from  www  .ja v a2s  .  c o m
 * @param handler
 *           handler which will be used for test
 * @param media
 *           media where the data will be imported to
 */
private void mediaImportFromMediasMedia(final MediaDataHandler handler, final Media media,
        final ImpExImportCronJob cronJob) {
    MediaDataHandler myHandler = handler;
    File testFile = null;
    try {
        testFile = File.createTempFile("mediaImportTest", ".zip");
        final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(testFile));
        zos.putNextEntry(new ZipEntry(new File("notunzip\\notexist.txt").getPath()));
        zos.putNextEntry(new ZipEntry(new File("files\\dummy.txt").getPath()));
        zos.putNextEntry(new ZipEntry(new File("files\\test.txt").getPath()));
        final PrintWriter printer = new PrintWriter(zos);
        printer.print("testest");
        printer.flush();
        zos.flush();
        printer.close();
        zos.close();
    } catch (final IOException e) {
        fail(e.getMessage());
    }
    try {
        final Media mediasMedia = ImpExManager.getInstance().createImpExMedia("mediasMedia", "UTF-8",
                new FileInputStream(testFile));
        cronJob.setMediasMedia(mediasMedia);

        mediaImport(myHandler, media, "files/test.txt", "testest");
        myHandler.cleanUp();
        myHandler = new DefaultCronJobMediaDataHandler(cronJob);
        mediaImport(myHandler, media, "files\\test.txt", "testest");
        myHandler.cleanUp();
        cronJob.setMediasTarget("files");
        myHandler = new DefaultCronJobMediaDataHandler(cronJob);
        mediaImport(myHandler, media, "test.txt", "testest");
        myHandler.cleanUp();
        cronJob.setMediasTarget(null);
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    if (!testFile.delete()) {
        fail("Can not delete temp file: " + testFile.getPath());
    }
}

From source file:org.openremote.beehive.configuration.www.UsersAPI.java

private void writeZipEntry(ZipOutputStream zipOutput, File file, java.nio.file.Path basePath)
        throws IOException {
    ZipEntry entry = new ZipEntry(basePath.relativize(file.toPath()).toString());
    entry.setSize(file.length());// w w w.  jav a2 s .  c  o  m
    entry.setTime(file.lastModified());
    zipOutput.putNextEntry(entry);

    IOUtils.copy(new FileInputStream(file), zipOutput);

    zipOutput.flush();
    zipOutput.closeEntry();
}

From source file:org.eclipse.mylyn.internal.context.core.InteractionContextExternalizer.java

/**
 * For testing//from ww w.j a  v  a  2 s.c  om
 */
public void writeContext(IInteractionContext context, ZipOutputStream outputStream,
        IInteractionContextWriter writer) throws IOException {
    String handleIdentifier = context.getHandleIdentifier();
    String encoded = URLEncoder.encode(handleIdentifier, InteractionContextManager.CONTEXT_FILENAME_ENCODING);
    ZipEntry zipEntry = new ZipEntry(encoded + InteractionContextManager.CONTEXT_FILE_EXTENSION_OLD);
    outputStream.putNextEntry(zipEntry);
    outputStream.setMethod(ZipOutputStream.DEFLATED);

    writer.setOutputStream(outputStream);
    writer.writeContextToStream(context);
    outputStream.flush();
    outputStream.closeEntry();

    addAdditionalInformation(context, outputStream);
}

From source file:org.dhatim.archive.Archive.java

/**
 * Create an archive of the specified name and containing entries
 * for the data contained in the streams supplied entries arg.
 * specifying the entry name and the value is a InputStream containing
 * the entry data./*from ww  w.java  2  s . co  m*/
 * @param archiveStream The archive output stream.
 * @throws java.io.IOException Write failure.
 */
public void toOutputStream(ZipOutputStream archiveStream) throws IOException {
    AssertArgument.isNotNull(archiveStream, "archiveStream");

    try {
        writeEntriesToArchive(archiveStream);
    } finally {
        try {
            archiveStream.flush();
        } finally {
            try {
                archiveStream.close();
            } catch (IOException e) {
                logger.info("Unable to close archive output stream.");
            }
        }
    }
}

From source file:com.joliciel.talismane.extensions.corpus.CorpusStatistics.java

@Override
public void onCompleteParse() {
    try {/*from  w  w  w .  j  a  v  a  2 s  .  co  m*/
        if (writer != null) {
            double unknownLexiconPercent = 1;
            if (referenceWords != null) {
                int unknownLexiconCount = 0;
                for (String word : words) {
                    if (!referenceWords.contains(word))
                        unknownLexiconCount++;
                }
                unknownLexiconPercent = (double) unknownLexiconCount / (double) words.size();
            }
            double unknownLowercaseLexiconPercent = 1;
            if (referenceLowercaseWords != null) {
                int unknownLowercaseLexiconCount = 0;
                for (String lowercase : lowerCaseWords) {
                    if (!referenceLowercaseWords.contains(lowercase))
                        unknownLowercaseLexiconCount++;
                }
                unknownLowercaseLexiconPercent = (double) unknownLowercaseLexiconCount
                        / (double) lowerCaseWords.size();
            }

            writer.write(CSV.format("sentenceCount") + CSV.format(sentenceCount) + "\n");
            writer.write(CSV.format("sentenceLengthMean") + CSV.format(sentenceLengthStats.getMean()) + "\n");
            writer.write(CSV.format("sentenceLengthStdDev")
                    + CSV.format(sentenceLengthStats.getStandardDeviation()) + "\n");
            writer.write(CSV.format("tokenLexiconSize") + CSV.format(words.size()) + "\n");
            writer.write(CSV.format("tokenLexiconUnknown") + CSV.format(unknownLexiconPercent * 100.0) + "\n");
            writer.write(CSV.format("tokenCount") + CSV.format(tokenCount) + "\n");

            double unknownTokenPercent = ((double) unknownTokenCount / (double) tokenCount) * 100.0;
            writer.write(CSV.format("tokenUnknown") + CSV.format(unknownTokenPercent) + "\n");

            writer.write(CSV.format("lowercaseLexiconSize") + CSV.format(lowerCaseWords.size()) + "\n");
            writer.write(CSV.format("lowercaseLexiconUnknown")
                    + CSV.format(unknownLowercaseLexiconPercent * 100.0) + "\n");
            writer.write(CSV.format("alphanumericCount") + CSV.format(alphanumericCount) + "\n");

            double unknownAlphanumericPercent = ((double) unknownAlphanumericCount / (double) alphanumericCount)
                    * 100.0;
            writer.write(CSV.format("alphanumericUnknown") + CSV.format(unknownAlphanumericPercent) + "\n");

            writer.write(CSV.format("syntaxDepthMean") + CSV.format(syntaxDepthStats.getMean()) + "\n");
            writer.write(CSV.format("syntaxDepthStdDev") + CSV.format(syntaxDepthStats.getStandardDeviation())
                    + "\n");
            writer.write(CSV.format("maxSyntaxDepthMean") + CSV.format(maxSyntaxDepthStats.getMean()) + "\n");
            writer.write(CSV.format("maxSyntaxDepthStdDev")
                    + CSV.format(maxSyntaxDepthStats.getStandardDeviation()) + "\n");
            writer.write(
                    CSV.format("sentAvgSyntaxDepthMean") + CSV.format(avgSyntaxDepthStats.getMean()) + "\n");
            writer.write(CSV.format("sentAvgSyntaxDepthStdDev")
                    + CSV.format(avgSyntaxDepthStats.getStandardDeviation()) + "\n");
            writer.write(CSV.format("syntaxDistanceMean") + CSV.format(syntaxDistanceStats.getMean()) + "\n");
            writer.write(CSV.format("syntaxDistanceStdDev")
                    + CSV.format(syntaxDistanceStats.getStandardDeviation()) + "\n");

            double nonProjectivePercent = ((double) nonProjectiveCount / (double) totalDepCount) * 100.0;
            writer.write(CSV.format("nonProjectiveCount") + CSV.format(nonProjectiveCount) + "\n");
            writer.write(CSV.format("nonProjectivePercent") + CSV.format(nonProjectivePercent) + "\n");
            writer.write(CSV.format("PosTagCounts") + "\n");

            for (String posTag : posTagCounts.keySet()) {
                int count = posTagCounts.get(posTag);
                writer.write(CSV.format(posTag) + CSV.format(count)
                        + CSV.format(((double) count / (double) tokenCount) * 100.0) + "\n");
            }

            writer.write(CSV.format("DepLabelCounts") + "\n");
            for (String depLabel : depLabelCounts.keySet()) {
                int count = depLabelCounts.get(depLabel);
                writer.write(CSV.format(depLabel) + CSV.format(count)
                        + CSV.format(((double) count / (double) totalDepCount) * 100.0) + "\n");
            }
            writer.flush();
            writer.close();
        }

        if (this.serializationFile != null) {
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(serializationFile, false));
            zos.putNextEntry(new ZipEntry("Contents.obj"));
            ObjectOutputStream oos = new ObjectOutputStream(zos);
            try {
                oos.writeObject(this);
            } finally {
                oos.flush();
            }
            zos.flush();
            zos.close();
        }
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }

}

From source file:com.joliciel.talismane.machineLearning.linearsvm.LinearSVMOneVsRestModel.java

@Override
public void writeModelToStream(OutputStream outputStream) {
    try {/*from  w  w w .  jav  a 2s.  c  om*/
        ZipOutputStream zos = new ZipOutputStream(outputStream);
        zos.setLevel(ZipOutputStream.STORED);
        int i = 0;
        for (Model model : models) {
            LOG.debug("Writing model " + i + " for outcome " + outcomes.get(i));
            ZipEntry zipEntry = new ZipEntry("model" + i);
            i++;
            zos.putNextEntry(zipEntry);
            Writer writer = new OutputStreamWriter(zos, "UTF-8");
            Writer unclosableWriter = new UnclosableWriter(writer);
            model.save(unclosableWriter);
            zos.closeEntry();
            zos.flush();
        }
    } catch (UnsupportedEncodingException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:com.jaspersoft.jasperserver.api.engine.scheduling.quartz.ReportExecutionJobMailNotificationImpl.java

protected void attachOutputs(ReportExecutionJob job, MimeMessageHelper messageHelper, List reportOutputs)
        throws MessagingException, JobExecutionException {
    String attachmentName = null;
    DataContainer attachmentData = job.createDataContainer();
    boolean close = true;
    ZipOutputStream zipOut = new ZipOutputStream(attachmentData.getOutputStream());
    try {/*from w w w . j  a va 2 s  . c  o  m*/
        for (Iterator it = reportOutputs.iterator(); it.hasNext();) {
            ReportOutput output = (ReportOutput) it.next();
            if (attachmentName == null)
                attachmentName = removeExtension(output.getFilename()) + ".zip";
            zipOutput(job, output, zipOut);
        }
        zipOut.finish();
        zipOut.flush();
        close = false;
        zipOut.close();
    } catch (IOException e) {
        throw new JSExceptionWrapper(e);
    } finally {
        if (close) {
            try {
                zipOut.close();
            } catch (IOException e) {
                log.error("Error closing stream", e);
            }
        }
    }
    try {
        attachmentName = MimeUtility.encodeWord(attachmentName, job.getCharacterEncoding(), null);
    } catch (UnsupportedEncodingException e) {
        throw new JSExceptionWrapper(e);
    }
    messageHelper.addAttachment(attachmentName, new DataContainerResource(attachmentData));
}

From source file:com.cisco.ca.cstg.pdi.services.ConfigurationServiceImpl.java

private void zipFolder(String srcFolder, String destZipFile) throws IOException {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;
    try {/*from ww  w.  ja  v  a  2  s.  c  o m*/
        fileWriter = new FileOutputStream(destZipFile);
        zip = new ZipOutputStream(fileWriter);
        addFileToZip(srcFolder, zip);
    } finally {
        try {
            if (zip != null) {
                zip.flush();
                zip.close();
            }
        } catch (IOException e) {
            LOGGER.error("Error occured while closing the resources after zipping the folder.", e);
        }
    }
    LOGGER.info("Created zip folder {}", destZipFile);
}

From source file:org.wso2.carbon.dashboard.migratetool.DSCarFileMigrationTool.java

/**
 * generate modified car file to deploy with relevant changes
 *///from w w w  . j  a v  a 2  s  . com
private void generateModifiedCarFile() {
    ZipOutputStream zip = null;
    try {
        FileOutputStream fos = new FileOutputStream(destCarDir);
        zip = new ZipOutputStream(fos);
        File folder = new File(tempDir);
        if (folder.list() != null) {
            for (String fileName : folder.list()) {
                addFileToZip("", tempDir + File.separator + fileName, zip);
            }
        }
    } catch (IOException e) {
        log.error("Error in generating modified car file ", e);
    } finally {
        if (zip != null) {
            try {
                zip.flush();
                zip.close();
            } catch (IOException e) {
                log.error("Unable to close the zip output stream ", e);
            }
        }
    }
}