List of usage examples for java.util.zip ZipOutputStream flush
public void flush() throws IOException
From source file:com.siemens.sw360.portal.portlets.admin.ComponentUploadPortlet.java
public void backUpLicenses(ResourceRequest request, ResourceResponse response) throws IOException, TException { Map<String, InputStream> fileNameToStreams = getFilenameToCSVStreams(); final ByteArrayOutputStream outB = new ByteArrayOutputStream(); final ZipOutputStream zipOutputStream = new ZipOutputStream(outB); for (Map.Entry<String, InputStream> entry : fileNameToStreams.entrySet()) { ZipTools.addToZip(zipOutputStream, entry.getKey(), entry.getValue()); }//from w w w.j a v a 2s. co m zipOutputStream.flush(); zipOutputStream.close(); // this closes outB final ByteArrayInputStream zipFile = new ByteArrayInputStream(outB.toByteArray()); PortletResponseUtil.sendFile(request, response, "LicensesBackup.lics", zipFile, "application/zip"); }
From source file:org.laptop.linuxdevice.api.util.ZipUtil.java
private static boolean createZipArchive(String srcFolder) throws IOException { BufferedInputStream origin = null; ZipOutputStream out = null; try {/*w w w. ja v a2 s . c om*/ final int BUFFER = 2048; FileOutputStream dest = new FileOutputStream(new File(srcFolder + ".zip")); out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[BUFFER]; File subDir = new File(srcFolder); String subdirList[] = subDir.list(); if (subdirList == null) { return false; } for (String sd : subdirList) { // get a list of files from current directory File f = new File(srcFolder + "/" + sd); if (f.isDirectory()) { String files[] = f.list(); if (files == null) { return false; } for (int i = 0; i < files.length; i++) { FileInputStream fi = new FileInputStream(srcFolder + "/" + sd + "/" + files[i]); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(sd + "/" + files[i]); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); out.flush(); } } } else //it is just a file { FileInputStream fi = new FileInputStream(f); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(sd); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); out.flush(); } } } out.flush(); } finally { if (origin != null) { origin.close(); } if (out != null) { out.close(); } } return true; }
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 w w w. j a v a2 s . co m * @param handler * handler which will be used for test * @param media * media where the data will be imported to */ private void mediaImportFromZip(final MediaDataHandler handler, final Media media) { File testFile = null; try { testFile = File.createTempFile("mediaImportTest", ".zip"); final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(testFile)); 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) { e.printStackTrace(); fail(e.getMessage()); } final String unixPathRel = ImpExConstants.Syntax.ZIP_BASED_FILE_PATH_PREFIX + FilenameUtils.separatorsToUnix(testFile.getPath()) + "&files/test.txt"; final String unixPathAbs = ImpExConstants.Syntax.ZIP_BASED_FILE_PATH_PREFIX + FilenameUtils.separatorsToUnix(testFile.getAbsolutePath()) + "&files/test.txt"; final String winPathRel = ImpExConstants.Syntax.ZIP_BASED_FILE_PATH_PREFIX + FilenameUtils.separatorsToWindows(testFile.getPath()) + "&files\\test.txt"; final String winPathAbs = ImpExConstants.Syntax.ZIP_BASED_FILE_PATH_PREFIX + FilenameUtils.separatorsToWindows(testFile.getAbsolutePath()) + "&files\\test.txt"; try { mediaImport(handler, media, unixPathRel, "testest"); mediaImport(handler, media, unixPathAbs, "testest"); mediaImport(handler, media, winPathRel, "testest"); mediaImport(handler, media, winPathAbs, "testest"); } catch (final Exception e) { fail(e.getMessage()); } if (!testFile.delete()) { fail("Can not delete temp file: " + testFile.getPath()); } }
From source file:com.mgmtp.jfunk.common.util.ExtendedFile.java
private void zip(String prefix, final File file, final ZipOutputStream zipOut) throws IOException { if (file.isDirectory()) { prefix = prefix + file.getName() + '/'; for (File child : file.listFiles()) { zip(prefix, child, zipOut);/* w ww . j ava 2 s . c o m*/ } } else { FileInputStream in = null; try { in = new FileInputStream(file); zipOut.putNextEntry(new ZipEntry(prefix + file.getName())); IOUtils.copy(in, zipOut); } finally { IOUtils.closeQuietly(in); zipOut.flush(); zipOut.closeEntry(); } } }
From source file:org.fao.geonet.api.site.LoggingApi.java
@ApiOperation(value = "Get last activity in a ZIP", notes = "", nickname = "getLastActivityInAZip") @RequestMapping(value = "/activity/zip", method = RequestMethod.GET, produces = { "application/zip" }) @ResponseBody/* w w w .j av a2 s. c o m*/ public void getLastActivityInAZip(HttpServletResponse response) throws IOException { if (isAppenderLogFileLoaded(fileAppender)) { File file = new File(fileAppender.getFile()); // create ZIP FILE String fname = String.valueOf(Calendar.getInstance().getTimeInMillis()); // set headers for the response response.setContentType("application/zip"); response.setContentLength((int) file.length()); String headerKey = "Content-Disposition"; String headerValue = String.format("attachment; filename=\"catalog-log-%s-%s.zip\"", fname, new SimpleDateFormat("ddMMyyyyHHmmss").format(new Date())); response.setHeader(headerKey, headerValue); int read = 0; byte[] bytes = new byte[1024]; ZipOutputStream zos = null; ZipEntry ze; InputStream in = null; try { zos = new ZipOutputStream(response.getOutputStream()); ze = new ZipEntry(file.getName()); zos.putNextEntry(ze); in = new FileInputStream(file); while ((read = in.read(bytes)) != -1) { zos.write(bytes, 0, read); } } finally { IOUtils.closeQuietly(in); if (zos != null) zos.flush(); IOUtils.closeQuietly(zos); } } else { throw new RuntimeException("No log file found for download. Check logger configuration."); } }
From source file:password.pwm.http.servlet.configmanager.DebugItemGenerator.java
static void outputZipDebugFile(final PwmRequest pwmRequest, final ZipOutputStream zipOutput, final String pathPrefix) throws IOException, PwmUnrecoverableException { final PwmApplication pwmApplication = pwmRequest.getPwmApplication(); final String DEBUG_FILENAME = "zipDebugGeneration.csv"; final ByteArrayOutputStream debugGeneratorLogBaos = new ByteArrayOutputStream(); final CSVPrinter debugGeneratorLogFile = JavaHelper.makeCsvPrinter(debugGeneratorLogBaos); for (final Class<? extends DebugItemGenerator.Generator> serviceClass : DEBUG_ZIP_ITEM_GENERATORS) { try {/* w w w. j a va 2 s.c o m*/ final Instant startTime = Instant.now(); LOGGER.trace(pwmRequest, "beginning output of item " + serviceClass.getSimpleName()); final Object newInstance = serviceClass.newInstance(); final DebugItemGenerator.Generator newGeneratorItem = (DebugItemGenerator.Generator) newInstance; zipOutput.putNextEntry(new ZipEntry(pathPrefix + newGeneratorItem.getFilename())); newGeneratorItem.outputItem(pwmApplication, pwmRequest, zipOutput); zipOutput.closeEntry(); zipOutput.flush(); final String finishMsg = "completed output of " + newGeneratorItem.getFilename() + " in " + TimeDuration.fromCurrent(startTime).asCompactString(); LOGGER.trace(pwmRequest, finishMsg); debugGeneratorLogFile.printRecord(JavaHelper.toIsoDate(new Date()), finishMsg); } catch (Throwable e) { final String errorMsg = "unexpected error executing debug item output class '" + serviceClass.getName() + "', error: " + e.toString(); LOGGER.error(pwmRequest, errorMsg); debugGeneratorLogFile.printRecord(JavaHelper.toIsoDate(new Date()), errorMsg); final Writer stackTraceOutput = new StringWriter(); e.printStackTrace(new PrintWriter(stackTraceOutput)); debugGeneratorLogFile.printRecord(stackTraceOutput); } } try { zipOutput.putNextEntry(new ZipEntry(pathPrefix + DEBUG_FILENAME)); debugGeneratorLogFile.flush(); zipOutput.write(debugGeneratorLogBaos.toByteArray()); zipOutput.closeEntry(); } catch (Exception e) { LOGGER.error("error generating " + DEBUG_FILENAME + ": " + e.getMessage()); } zipOutput.flush(); }
From source file:org.wso2.carbon.sampledevice.api.util.ZipUtil.java
/** * Write the zip file./* ww w .j a v a2s. c om*/ * * @param srcFolder Path of the source folder * @return zip distribution. * @throws IOException Error creating the zip file. */ private static boolean createZipArchive(String srcFolder) throws IOException { BufferedInputStream origin = null; ZipOutputStream out = null; try { final int buffer = 2048; FileOutputStream dest = new FileOutputStream(new File(srcFolder + ".zip")); out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[buffer]; File subDir = new File(srcFolder); String subdirList[] = subDir.list(); if (subdirList == null) { return false; } for (String sd : subdirList) { // get a list of files from current directory File f = new File(srcFolder + "/" + sd); if (f.isDirectory()) { String files[] = f.list(); if (files == null) { return false; } for (int i = 0; i < files.length; i++) { FileInputStream fi = new FileInputStream(srcFolder + "/" + sd + "/" + files[i]); origin = new BufferedInputStream(fi, buffer); ZipEntry entry = new ZipEntry(sd + "/" + files[i]); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, buffer)) != -1) { out.write(data, 0, count); out.flush(); } } } else { //it is just a file FileInputStream fi = new FileInputStream(f); origin = new BufferedInputStream(fi, buffer); ZipEntry entry = new ZipEntry(sd); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, buffer)) != -1) { out.write(data, 0, count); out.flush(); } } } out.flush(); } finally { if (origin != null) { origin.close(); } if (out != null) { out.close(); } } return true; }
From source file:com.microsoft.tfs.client.common.ui.teambuild.commands.CreateUploadZipCommand.java
/** * Zip a file or folder to a zip file/*from w w w. j a v a 2 s.co m*/ * * * @param srcFolder * @param destZipFile * @throws Exception */ public void zip(final String srcFolder, final String destZipFile) throws Exception { checkFolder(localPath); final FileOutputStream fileWriter = new FileOutputStream(destZipFile); final ZipOutputStream zipout = new ZipOutputStream(fileWriter); try { loadIgnoreFile(srcFolder); /* preserve the folder when archive */ final String parentPath = LocalPath .removeTrailingSeparators(new File(srcFolder).getParentFile().getAbsolutePath()); addFileToZip(parentPath, new File(srcFolder).getAbsoluteFile(), zipout); zipout.flush(); } catch (final Exception e) { errorMsg = MessageFormat.format( Messages.getString("CreateUploadZipCommand.CreateArchiveErrorMessageFormat"), //$NON-NLS-1$ srcFolder); log.error("Exceptions when creating zip archive ", e); //$NON-NLS-1$ throw e; } finally { zipout.close(); } }
From source file:org.codice.alliance.nsili.endpoint.requests.OrderRequestImpl.java
private void getZip(ZipOutputStream zipOut, InputStream data, String name) throws IOException { ZipEntry zipEntry = new ZipEntry(name); zipOut.putNextEntry(zipEntry);//ww w. ja v a 2 s . co m IOUtils.copy(data, zipOut); zipOut.flush(); }
From source file:com.joliciel.talismane.extensions.corpus.PosTaggerStatistics.java
@Override public void onCompleteAnalysis() { try {//from w w w .java2 s. c om if (writer != null) { PosTagSet posTagSet = talismaneSession.getPosTagSet(); for (PosTag posTag : posTagSet.getTags()) { if (!posTagCounts.containsKey(posTag.getCode())) { posTagCounts.put(posTag.getCode(), 0); } } 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("lexiconSize") + CSV.format(words.size()) + "\n"); writer.write( CSV.format("lexiconUnknownInRefCorpus") + 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("tokenUnknownInRefCorpus") + CSV.format(unknownTokenPercent) + "\n"); double unknownInLexiconPercent = ((double) unknownInLexiconCount / (double) tokenCount) * 100.0; writer.write(CSV.format("tokenUnknownInRefLexicon") + CSV.format(unknownInLexiconPercent) + "\n"); writer.write(CSV.format("lowercaseLexiconSize") + CSV.format(lowerCaseWords.size()) + "\n"); writer.write(CSV.format("lowercaseLexiconUnknownInRefCorpus") + 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("alphaUnknownInRefCorpus") + CSV.format(unknownAlphanumericPercent) + "\n"); double unknownAlphaInLexiconPercent = ((double) unknownAlphaInLexiconCount / (double) alphanumericCount) * 100.0; writer.write( CSV.format("alphaUnknownInRefLexicon") + CSV.format(unknownAlphaInLexiconPercent) + "\n"); writer.write(CSV.format("openClassCount") + CSV.format(openClassCount) + "\n"); double openClassUnknownPercent = ((double) openClassUnknownInRefCorpus / (double) openClassCount) * 100.0; writer.write( CSV.format("openClassUnknownInRefCorpus") + CSV.format(openClassUnknownPercent) + "\n"); double openClassUnknownInLexiconPercent = ((double) openClassUnknownInLexicon / (double) openClassCount) * 100.0; writer.write(CSV.format("openClassUnknownInRefLexicon") + CSV.format(openClassUnknownInLexiconPercent) + "\n"); writer.write(CSV.format("closedClassCount") + CSV.format(closedClassCount) + "\n"); double closedClassUnknownPercent = ((double) closedClassUnknownInRefCorpus / (double) closedClassCount) * 100.0; writer.write( CSV.format("closedClassUnknownInRefCorpus") + CSV.format(closedClassUnknownPercent) + "\n"); double closedClassUnknownInLexiconPercent = ((double) closedClassUnknownInLexicon / (double) closedClassCount) * 100.0; writer.write(CSV.format("closedClassUnknownInRefLexicon") + CSV.format(closedClassUnknownInLexiconPercent) + "\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.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); } }