List of usage examples for java.util.zip ZipOutputStream flush
public void flush() throws IOException
From source file:org.openremote.modeler.cache.LocalFileCache.java
/** * Compresses a set of files into a target zip archive. The file instances should be relative * paths used to structure the archive into directories. The relative paths will be resolved * to actual file paths in the current account's file cache. * * @param target Target file path where the zip archive will be stored. * @param files Set of <b>relative</b> file paths to include in the zip archive. The file * paths should be set to match the expected directory structure in the final * archive (therefore should not reflect the absolute file paths expected to * be included in the archive). * * @throws CacheOperationException/*from w w w. j a v a 2 s.co m*/ * if any of the zip file operations fail * * @throws ConfigurationException * if there are any security restrictions about reading the set of included files * or writing the target zip archive file */ private void compress(File target, Set<File> files) throws CacheOperationException, ConfigurationException { ZipOutputStream zipOutput = null; try { zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target))); for (File file : files) { BufferedInputStream fileInput = null; // translate the relative zip archive directory path to existing user cache absolute path... File cachePathName = new File(cacheFolder, file.getPath()); try { if (!cachePathName.exists()) { throw new CacheOperationException( "Expected to add file ''{0}'' to export archive ''{1}'' (Account : {2}) but it " + "has gone missing (cause unknown). This can indicate implementation or deployment " + "error. Aborting export operation as a safety precaution.", cachePathName.getPath(), target.getAbsolutePath(), currentUserAccount.getAccount().getOid()); } fileInput = new BufferedInputStream(new FileInputStream(cachePathName)); ZipEntry entry = new ZipEntry(file.getPath()); entry.setSize(cachePathName.length()); entry.setTime(cachePathName.lastModified()); zipOutput.putNextEntry(entry); cacheLog.debug("Added new export zip entry ''{0}''.", file.getPath()); int count, total = 0; final int BUFFER_SIZE = 2048; byte[] data = new byte[BUFFER_SIZE]; while ((count = fileInput.read(data, 0, BUFFER_SIZE)) != -1) { zipOutput.write(data, 0, count); total += count; } zipOutput.flush(); // Sanity check... if (total != cachePathName.length()) { throw new CacheOperationException( "Only wrote {0} out of {1} bytes when archiving file ''{2}'' (Account : {3}). " + "This could have occured either due implementation error or file I/O error. " + "Aborting archive operation to prevent a potentially corrupt export archive to " + "be created.", total, cachePathName.length(), cachePathName.getPath(), currentUserAccount.getAccount().getOid()); } else { cacheLog.debug("Wrote {0} out of {1} bytes to zip entry ''{2}''", total, cachePathName.length(), file.getPath()); } } catch (SecurityException e) { // we've messed up deployment... quite likely unrecoverable... throw new ConfigurationException( "Security manager has denied r/w access when attempting to read file ''{0}'' and " + "write it to archive ''{1}'' (Account : {2}) : {3}", e, cachePathName.getPath(), target, currentUserAccount.getAccount().getOid(), e.getMessage()); } catch (IllegalArgumentException e) { // This may occur if we overrun some fixed size limits in ZIP format... throw new CacheOperationException("Error creating ZIP archive for account ID = {0} : {1}", e, currentUserAccount.getAccount().getOid(), e.getMessage()); } catch (FileNotFoundException e) { throw new CacheOperationException( "Attempted to include file ''{0}'' in export archive but it has gone missing " + "(Account : {1}). Possible implementation error in local file cache. Aborting " + "export operation as a precaution ({2})", e, cachePathName.getPath(), currentUserAccount.getAccount().getOid(), e.getMessage()); } catch (ZipException e) { throw new CacheOperationException("Error writing export archive for account ID = {0} : {1}", e, currentUserAccount.getAccount().getOid(), e.getMessage()); } catch (IOException e) { throw new CacheOperationException( "I/O error while creating export archive for account ID = {0}. " + "Operation aborted ({1})", e, currentUserAccount.getAccount().getOid(), e.getMessage()); } finally { if (zipOutput != null) { try { zipOutput.closeEntry(); } catch (Throwable t) { cacheLog.warn( "Unable to close zip entry for file ''{0}'' in export archive ''{1}'' " + "(Account : {2}) : {3}.", t, file.getPath(), target.getAbsolutePath(), currentUserAccount.getAccount().getOid(), t.getMessage()); } } if (fileInput != null) { try { fileInput.close(); } catch (Throwable t) { cacheLog.warn( "Failed to close input stream from file ''{0}'' being added " + "to export archive (Account : {1}) : {2}", t, cachePathName.getPath(), currentUserAccount.getAccount().getOid(), t.getMessage()); } } } } } catch (FileNotFoundException e) { throw new CacheOperationException( "Unable to create target export archive ''{0}'' for account {1) : {2}", e, target, currentUserAccount.getAccount().getOid(), e.getMessage()); } finally { try { if (zipOutput != null) { zipOutput.close(); } } catch (Throwable t) { cacheLog.warn("Failed to close the stream to export archive ''{0}'' : {1}.", t, target, t.getMessage()); } } }
From source file:cross.io.misc.WorkflowZipper.java
/** * Saves the currently assigned workflow elements, matching currently * assigned FileFilter to File. Marks all files for deletion on exit. * * @param f the file to save to//from w w w . ja va2s.c o m * @return true if the workflow was zipped, false otherwise * @throws RuntimeException if IOExceptions are encountered */ public boolean save(final File f) { if (this.zipWorkflow) { HashSet<String> zipEntries = new HashSet<>(); final int bufsize = 1024; final File zipFile = f; ZipOutputStream zos; try { final FileOutputStream fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(new BufferedOutputStream(fos)); log.info("Created zip output stream"); final byte[] input_buffer = new byte[bufsize]; File basedir = FileTools.prependDefaultDirsWithPrefix("", null, this.iw.getStartupDate()); if (this.deleteOnExit) { log.info("marked basedir for deletion on exit: {}", basedir); basedir.deleteOnExit(); } if (flatten) { log.info("setting basedir to parent file: {}", basedir.getParentFile()); basedir = basedir.getParentFile(); final Iterator<IWorkflowResult> iter = this.iw.getResults(); while (iter.hasNext()) { final IWorkflowResult iwr = iter.next(); if (iwr instanceof IWorkflowFileResult) { final IWorkflowFileResult iwfr = (IWorkflowFileResult) iwr; final File file = iwfr.getFile(); log.info("Retrieving file result {}", file); // mark file for deletion final File parent = file.getParentFile(); log.info("Retrieving parent of file result {}", parent); // Also delete the parent directory in which file was // contained, // unless it is the base directory + possibly additional // defaultDirs if (parent.getAbsolutePath().startsWith(basedir.getAbsolutePath()) && !parent.getAbsolutePath().equals(basedir.getAbsolutePath())) { log.info("Marking file and parent for deletion"); if (this.deleteOnExit) { parent.deleteOnExit(); file.deleteOnExit(); } } if (file.getAbsolutePath().startsWith(basedir.getAbsolutePath())) { log.info("Marking file for deletion"); if (this.deleteOnExit) { file.deleteOnExit(); } } if ((this.ff != null) && !this.ff.accept(file)) { // Skip file if file filter does not accept it continue; } else { log.info("Adding zip entry!"); addZipEntry(bufsize, zos, input_buffer, file, zipEntries); } } } } else { LinkedList<File> files = new LinkedList<>(Arrays.asList(basedir.listFiles(ff))); File archiveBase = basedir.getParentFile(); while (!files.isEmpty()) { File currentFile = files.removeFirst(); if (currentFile.isDirectory()) { files.addAll(Arrays.asList(currentFile.listFiles(ff))); } else { try { String relativePath = FileTools.getRelativeFile(archiveBase, currentFile).getPath() .replaceAll("\\\\", "/"); log.info("Adding zip entry for {} below {}", relativePath, archiveBase); addRelativeZipEntry(bufsize, zos, input_buffer, relativePath, currentFile, zipEntries); } catch (Exception ex) { log.warn("Caught exception while retrieving relative path:", ex); } } if (this.deleteOnExit) { log.info("Marking file for deletion"); currentFile.deleteOnExit(); } } } try { zos.flush(); zos.close(); } catch (final IOException e) { throw new RuntimeException(e); } } catch (final IOException e) { throw new RuntimeException(e); } return true; } else { log.debug("Configured to not zip Workflow results!"); return false; } }
From source file:com.joliciel.csvLearner.CSVEventListWriter.java
public void writeFile(GenericEvents events) { try {//ww w. ja va2s. c o m LOG.debug("writeFile: " + file.getName()); file.delete(); file.createNewFile(); if (file.getName().endsWith(".zip")) isZip = true; Writer writer = null; ZipOutputStream zos = null; try { if (isZip) { zos = new ZipOutputStream(new FileOutputStream(file, false)); writer = new BufferedWriter(new OutputStreamWriter(zos)); } else { writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8")); } Set<String> features = new TreeSet<String>(); if (!filePerEvent) { if (isZip) { zos.putNextEntry(new ZipEntry( file.getName().substring(0, file.getName().lastIndexOf('.')) + ".csv")); } for (GenericEvent event : events) { if (LOG.isTraceEnabled()) LOG.trace("Writing event: " + event.getIdentifier()); for (String feature : event.getFeatures()) { int classIndex = feature.indexOf(CSVLearner.NOMINAL_MARKER); if (classIndex < 0 || denominalise) features.add(feature); else features.add(feature.substring(0, classIndex)); } } writer.append("ID,"); if (includeOutcomes) writer.append("outcome,"); for (String feature : features) { writer.append(CSVFormatter.format(feature) + ","); } writer.append("\n"); writer.flush(); } for (GenericEvent event : events) { if (filePerEvent) { features = new TreeSet<String>(); for (String feature : event.getFeatures()) { int classIndex = feature.indexOf(CSVLearner.NOMINAL_MARKER); if (classIndex < 0 || denominalise) features.add(feature); else features.add(feature.substring(0, classIndex)); } if (isZip) zos.putNextEntry(new ZipEntry(event.getIdentifier() + ".csv")); writer.append("ID,"); if (includeOutcomes) writer.append("outcome,"); for (String feature : features) { writer.append(CSVFormatter.format(feature) + ","); } writer.append("\n"); writer.flush(); } writer.append(CSVFormatter.format(identifierPrefix + event.getIdentifier()) + ","); if (includeOutcomes) writer.append(CSVFormatter.format(event.getOutcome()) + ","); for (String feature : features) { Integer featureIndexObj = event.getFeatureIndex(feature); int featureIndex = featureIndexObj == null ? -1 : featureIndexObj.intValue(); if (featureIndex < 0) { writer.append(missingValueString + ","); } else { String eventFeature = event.getFeatures().get(featureIndex); if (!eventFeature.equals(feature)) { int classIndex = eventFeature.indexOf(CSVLearner.NOMINAL_MARKER); String clazz = eventFeature .substring(classIndex + CSVLearner.NOMINAL_MARKER.length()); writer.append(CSVFormatter.format(clazz) + ","); } else { double value = event.getWeights().get(featureIndex); writer.append(CSVFormatter.format(value) + ","); } } } writer.append("\n"); writer.flush(); if (filePerEvent && isZip) zos.closeEntry(); } if (!filePerEvent && isZip) zos.closeEntry(); } finally { if (zos != null) { zos.flush(); zos.close(); } if (writer != null) { writer.flush(); writer.close(); } } } catch (IOException ioe) { throw new RuntimeException(ioe); } }
From source file:it.govpay.web.rs.dars.monitoraggio.incassi.IncassiHandler.java
@Override public String esporta(List<Long> idsToExport, List<RawParamValue> rawValues, UriInfo uriInfo, BasicBD bd, ZipOutputStream zout) throws WebApplicationException, ConsoleException, ExportException { StringBuffer sb = new StringBuffer(); if (idsToExport != null && idsToExport.size() > 0) { for (Long long1 : idsToExport) { if (sb.length() > 0) { sb.append(", "); }//from www.j a va 2 s . co m sb.append(long1); } } String methodName = "esporta " + this.titoloServizio + "[" + sb.toString() + "]"; int numeroZipEntries = 0; String pathLoghi = ConsoleProperties.getInstance().getPathEstrattoContoPdfLoghi(); // if(idsToExport.size() == 1) { // return this.esporta(idsToExport.get(0), uriInfo, bd, zout); // } String fileName = "Export.zip"; try { this.log.info("Esecuzione " + methodName + " in corso..."); // Operazione consentita solo ai ruoli con diritto di lettura this.darsService.checkDirittiServizioLettura(bd, this.funzionalita); int limit = ConsoleProperties.getInstance().getNumeroMassimoElementiExport(); boolean simpleSearch = Utils.containsParameter(rawValues, DarsService.SIMPLE_SEARCH_PARAMETER_ID); IncassiBD incassiBD = new IncassiBD(bd); IncassoFilter filter = incassiBD.newFilter(simpleSearch); // se ho ricevuto anche gli id li utilizzo per fare il check della count if (idsToExport != null && idsToExport.size() > 0) filter.setIdIncasso(idsToExport); boolean eseguiRicerca = this.popolaFiltroRicerca(rawValues, bd, simpleSearch, filter); if (!eseguiRicerca) { List<String> msg = new ArrayList<String>(); msg.add(Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esporta.operazioneNonPermessa")); throw new ExportException(msg, EsitoOperazione.ERRORE); } long count = incassiBD.count(filter); if (count < 1) { List<String> msg = new ArrayList<String>(); msg.add(Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esporta.nessunElementoDaEsportare")); throw new ExportException(msg, EsitoOperazione.ERRORE); } if (count > ConsoleProperties.getInstance().getNumeroMassimoElementiExport()) { List<String> msg = new ArrayList<String>(); msg.add(Utils.getInstance(this.getLanguage()).getMessageFromResourceBundle( this.nomeServizio + ".esporta.numeroElementiDaEsportareSopraSogliaMassima")); throw new ExportException(msg, EsitoOperazione.ERRORE); } filter.setOffset(0); filter.setLimit(limit); FilterSortWrapper fsw = new FilterSortWrapper(); fsw.setField(it.govpay.orm.Incasso.model().DATA_ORA_INCASSO); fsw.setSortOrder(SortOrder.DESC); filter.getFilterSortList().add(fsw); List<Incasso> findAll = incassiBD.findAll(filter); for (Incasso incasso : findAll) { Applicazione applicazione = incasso.getApplicazione(incassiBD); List<Pagamento> pagamenti = incasso.getPagamenti(incassiBD); List<it.govpay.model.Pagamento> pagamentiList = new ArrayList<it.govpay.model.Pagamento>(); pagamentiList.addAll(pagamenti); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IncassoPdf.getPdfIncasso(pathLoghi, incasso, pagamentiList, applicazione, baos, this.log); String incassoPdfEntryName = incasso.getTrn() + ".pdf"; numeroZipEntries++; ZipEntry rtPdf = new ZipEntry(incassoPdfEntryName); zout.putNextEntry(rtPdf); zout.write(baos.toByteArray()); zout.closeEntry(); } // se non ho inserito nessuna entry if (numeroZipEntries == 0) { String noEntriesTxt = "/README"; ZipEntry entryTxt = new ZipEntry(noEntriesTxt); zout.putNextEntry(entryTxt); zout.write("Non sono state trovate informazioni sugli incassi selezionati.".getBytes()); zout.closeEntry(); } zout.flush(); zout.close(); this.log.info("Esecuzione " + methodName + " completata."); return fileName; } catch (WebApplicationException e) { throw e; } catch (Exception e) { throw new ConsoleException(e); } }
From source file:org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.java
/** * This method saves ParagraphVectors model into compressed zip file and sends it to output stream *///from w w w . j a va 2s .c o m public static void writeParagraphVectors(ParagraphVectors vectors, OutputStream stream) throws IOException { ZipOutputStream zipfile = new ZipOutputStream( new BufferedOutputStream(new CloseShieldOutputStream(stream))); ZipEntry syn0 = new ZipEntry("syn0.txt"); zipfile.putNextEntry(syn0); // writing out syn0 File tempFileSyn0 = File.createTempFile("paravec", "0"); tempFileSyn0.deleteOnExit(); writeWordVectors(vectors.lookupTable(), tempFileSyn0); BufferedInputStream fis = new BufferedInputStream(new FileInputStream(tempFileSyn0)); writeEntry(fis, zipfile); fis.close(); // writing out syn1 File tempFileSyn1 = File.createTempFile("paravec", "1"); tempFileSyn1.deleteOnExit(); INDArray syn1 = ((InMemoryLookupTable<VocabWord>) vectors.getLookupTable()).getSyn1(); if (syn1 != null) try (PrintWriter writer = new PrintWriter(new FileWriter(tempFileSyn1))) { for (int x = 0; x < syn1.rows(); x++) { INDArray row = syn1.getRow(x); StringBuilder builder = new StringBuilder(); for (int i = 0; i < row.length(); i++) { builder.append(row.getDouble(i)).append(" "); } writer.println(builder.toString().trim()); } } ZipEntry zSyn1 = new ZipEntry("syn1.txt"); zipfile.putNextEntry(zSyn1); fis = new BufferedInputStream(new FileInputStream(tempFileSyn1)); writeEntry(fis, zipfile); fis.close(); File tempFileCodes = File.createTempFile("paravec", "h"); tempFileCodes.deleteOnExit(); ZipEntry hC = new ZipEntry("codes.txt"); zipfile.putNextEntry(hC); // writing out huffman tree try (PrintWriter writer = new PrintWriter(new FileWriter(tempFileCodes))) { for (int i = 0; i < vectors.getVocab().numWords(); i++) { VocabWord word = vectors.getVocab().elementAtIndex(i); StringBuilder builder = new StringBuilder(encodeB64(word.getLabel())).append(" "); for (int code : word.getCodes()) { builder.append(code).append(" "); } writer.println(builder.toString().trim()); } } fis = new BufferedInputStream(new FileInputStream(tempFileCodes)); writeEntry(fis, zipfile); fis.close(); File tempFileHuffman = File.createTempFile("paravec", "h"); tempFileHuffman.deleteOnExit(); ZipEntry hP = new ZipEntry("huffman.txt"); zipfile.putNextEntry(hP); // writing out huffman tree try (PrintWriter writer = new PrintWriter(new FileWriter(tempFileHuffman))) { for (int i = 0; i < vectors.getVocab().numWords(); i++) { VocabWord word = vectors.getVocab().elementAtIndex(i); StringBuilder builder = new StringBuilder(encodeB64(word.getLabel())).append(" "); for (int point : word.getPoints()) { builder.append(point).append(" "); } writer.println(builder.toString().trim()); } } fis = new BufferedInputStream(new FileInputStream(tempFileHuffman)); writeEntry(fis, zipfile); fis.close(); ZipEntry config = new ZipEntry("config.json"); zipfile.putNextEntry(config); writeEntry(new ByteArrayInputStream(vectors.getConfiguration().toJson().getBytes()), zipfile); ZipEntry labels = new ZipEntry("labels.txt"); zipfile.putNextEntry(labels); StringBuilder builder = new StringBuilder(); for (VocabWord word : vectors.getVocab().tokens()) { if (word.isLabel()) builder.append(encodeB64(word.getLabel())).append("\n"); } writeEntry(new ByteArrayInputStream(builder.toString().trim().getBytes()), zipfile); ZipEntry hF = new ZipEntry("frequencies.txt"); zipfile.putNextEntry(hF); File tempFileFreqs = File.createTempFile("paravec", "h"); tempFileFreqs.deleteOnExit(); // writing out word frequencies try (PrintWriter writer = new PrintWriter(new FileWriter(tempFileFreqs))) { for (int i = 0; i < vectors.getVocab().numWords(); i++) { VocabWord word = vectors.getVocab().elementAtIndex(i); builder = new StringBuilder(encodeB64(word.getLabel())).append(" ") .append(word.getElementFrequency()).append(" ") .append(vectors.getVocab().docAppearedIn(word.getLabel())); writer.println(builder.toString().trim()); } } fis = new BufferedInputStream(new FileInputStream(tempFileFreqs)); writeEntry(fis, zipfile); fis.close(); zipfile.flush(); zipfile.close(); }
From source file:it.govpay.web.rs.dars.monitoraggio.versamenti.VersamentiHandler.java
@Override public String esporta(Long idToExport, List<RawParamValue> rawValues, UriInfo uriInfo, BasicBD bd, ZipOutputStream zout) throws WebApplicationException, ConsoleException, ExportException { String methodName = "esporta " + this.titoloServizio + "[" + idToExport + "]"; Printer printer = null;//ww w .jav a2s. co m try { int numeroZipEntries = 0; this.log.info("Esecuzione " + methodName + " in corso..."); // Operazione consentita solo ai ruoli con diritto di lettura this.darsService.checkDirittiServizioLettura(bd, this.funzionalita); Set<Long> setDomini = this.darsService.getIdDominiAbilitatiLetturaServizio(bd, this.funzionalita); boolean eseguiRicerca = !setDomini.isEmpty(); VersamentiBD versamentiBD = new VersamentiBD(bd); EstrattiContoBD estrattiContoBD = new EstrattiContoBD(bd); it.govpay.core.business.EstrattoConto estrattoContoBD = new it.govpay.core.business.EstrattoConto(bd); VersamentoFilter filter = versamentiBD.newFilter(); List<Long> ids = new ArrayList<Long>(); ids.add(idToExport); if (!setDomini.contains(-1L)) { List<Long> lstCodDomini = new ArrayList<Long>(); lstCodDomini.addAll(setDomini); filter.setIdDomini(lstCodDomini); // l'operatore puo' vedere i domini associati, controllo se c'e' un versamento con Id nei domini concessi. if (eseguiRicerca) { filter.setIdVersamento(ids); eseguiRicerca = eseguiRicerca && versamentiBD.count(filter) > 0; } } Versamento versamento = eseguiRicerca ? versamentiBD.getVersamento(idToExport) : null; String fileName = "Export.zip"; if (versamento != null) { // Prelevo il dominio UnitaOperativa uo = AnagraficaManager.getUnitaOperativa(bd, versamento.getIdUo()); Dominio dominio = AnagraficaManager.getDominio(bd, uo.getIdDominio()); // Estratto conto per iban e codiceversamento. List<Long> idVersamentiDominio = new ArrayList<Long>(); idVersamentiDominio.add(idToExport); it.govpay.core.business.model.EstrattoConto input = it.govpay.core.business.model.EstrattoConto .creaEstrattoContoVersamentiPDF(dominio, idVersamentiDominio); List<it.govpay.core.business.model.EstrattoConto> listInputEstrattoConto = new ArrayList<it.govpay.core.business.model.EstrattoConto>(); listInputEstrattoConto.add(input); String pathLoghi = ConsoleProperties.getInstance().getPathEstrattoContoPdfLoghi(); List<it.govpay.core.business.model.EstrattoConto> listOutputEstattoConto = estrattoContoBD .getEstrattoContoVersamenti(listInputEstrattoConto, pathLoghi); for (it.govpay.core.business.model.EstrattoConto estrattoContoOutput : listOutputEstattoConto) { Map<String, ByteArrayOutputStream> estrattoContoVersamenti = estrattoContoOutput.getOutput(); for (String nomeEntry : estrattoContoVersamenti.keySet()) { numeroZipEntries++; ByteArrayOutputStream baos = estrattoContoVersamenti.get(nomeEntry); ZipEntry estrattoContoEntry = new ZipEntry( estrattoContoOutput.getDominio().getCodDominio() + "/" + nomeEntry); zout.putNextEntry(estrattoContoEntry); zout.write(baos.toByteArray()); zout.closeEntry(); } } //Estratto conto in formato CSV EstrattoContoFilter ecFilter = estrattiContoBD.newFilter(true); ecFilter.setIdVersamento(ids); List<EstrattoConto> findAll = estrattiContoBD.estrattoContoFromIdVersamenti(ecFilter); if (findAll != null && findAll.size() > 0) { //ordinamento record Collections.sort(findAll, new EstrattoContoComparator()); numeroZipEntries++; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ZipEntry pagamentoCsv = new ZipEntry("estrattoConto.csv"); zout.putNextEntry(pagamentoCsv); printer = new Printer(this.getFormat(), baos); printer.printRecord(CSVUtils.getEstrattoContoCsvHeader()); for (EstrattoConto pagamento : findAll) { printer.printRecord(CSVUtils.getEstrattoContoAsCsvRow(pagamento, this.sdf)); } } finally { try { if (printer != null) { printer.close(); } } catch (Exception e) { throw new Exception("Errore durante la chiusura dello stream ", e); } } zout.write(baos.toByteArray()); zout.closeEntry(); } } // se non ho inserito nessuna entry if (numeroZipEntries == 0) { String noEntriesTxt = "/README"; ZipEntry entryTxt = new ZipEntry(noEntriesTxt); zout.putNextEntry(entryTxt); zout.write("Non sono state trovate informazioni sui versamenti selezionati.".getBytes()); zout.closeEntry(); } zout.flush(); zout.close(); this.log.info("Esecuzione " + methodName + " completata."); return fileName; } catch (WebApplicationException e) { throw e; } catch (ExportException e) { throw e; } catch (Exception e) { throw new ConsoleException(e); } }
From source file:org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.java
/** * This method saves Word2Vec model into compressed zip file and sends it to output stream * PLEASE NOTE: This method saves FULL model, including syn0 AND syn1 * *///from w ww . java2s . c o m public static void writeWord2VecModel(Word2Vec vectors, OutputStream stream) throws IOException { ZipOutputStream zipfile = new ZipOutputStream( new BufferedOutputStream(new CloseShieldOutputStream(stream))); ZipEntry syn0 = new ZipEntry("syn0.txt"); zipfile.putNextEntry(syn0); // writing out syn0 File tempFileSyn0 = File.createTempFile("word2vec", "0"); tempFileSyn0.deleteOnExit(); writeWordVectors(vectors.lookupTable(), tempFileSyn0); BufferedInputStream fis = new BufferedInputStream(new FileInputStream(tempFileSyn0)); writeEntry(fis, zipfile); fis.close(); // writing out syn1 File tempFileSyn1 = File.createTempFile("word2vec", "1"); tempFileSyn1.deleteOnExit(); INDArray syn1 = ((InMemoryLookupTable<VocabWord>) vectors.getLookupTable()).getSyn1(); if (syn1 != null) try (PrintWriter writer = new PrintWriter(new FileWriter(tempFileSyn1))) { for (int x = 0; x < syn1.rows(); x++) { INDArray row = syn1.getRow(x); StringBuilder builder = new StringBuilder(); for (int i = 0; i < row.length(); i++) { builder.append(row.getDouble(i)).append(" "); } writer.println(builder.toString().trim()); } } ZipEntry zSyn1 = new ZipEntry("syn1.txt"); zipfile.putNextEntry(zSyn1); fis = new BufferedInputStream(new FileInputStream(tempFileSyn1)); writeEntry(fis, zipfile); fis.close(); // writing out syn1 File tempFileSyn1Neg = File.createTempFile("word2vec", "n"); tempFileSyn1Neg.deleteOnExit(); INDArray syn1Neg = ((InMemoryLookupTable<VocabWord>) vectors.getLookupTable()).getSyn1Neg(); if (syn1Neg != null) try (PrintWriter writer = new PrintWriter(new FileWriter(tempFileSyn1Neg))) { for (int x = 0; x < syn1Neg.rows(); x++) { INDArray row = syn1Neg.getRow(x); StringBuilder builder = new StringBuilder(); for (int i = 0; i < row.length(); i++) { builder.append(row.getDouble(i)).append(" "); } writer.println(builder.toString().trim()); } } ZipEntry zSyn1Neg = new ZipEntry("syn1Neg.txt"); zipfile.putNextEntry(zSyn1Neg); fis = new BufferedInputStream(new FileInputStream(tempFileSyn1Neg)); writeEntry(fis, zipfile); fis.close(); File tempFileCodes = File.createTempFile("word2vec", "h"); tempFileCodes.deleteOnExit(); ZipEntry hC = new ZipEntry("codes.txt"); zipfile.putNextEntry(hC); // writing out huffman tree try (PrintWriter writer = new PrintWriter(new FileWriter(tempFileCodes))) { for (int i = 0; i < vectors.getVocab().numWords(); i++) { VocabWord word = vectors.getVocab().elementAtIndex(i); StringBuilder builder = new StringBuilder(encodeB64(word.getLabel())).append(" "); for (int code : word.getCodes()) { builder.append(code).append(" "); } writer.println(builder.toString().trim()); } } fis = new BufferedInputStream(new FileInputStream(tempFileCodes)); writeEntry(fis, zipfile); fis.close(); File tempFileHuffman = File.createTempFile("word2vec", "h"); tempFileHuffman.deleteOnExit(); ZipEntry hP = new ZipEntry("huffman.txt"); zipfile.putNextEntry(hP); // writing out huffman tree try (PrintWriter writer = new PrintWriter(new FileWriter(tempFileHuffman))) { for (int i = 0; i < vectors.getVocab().numWords(); i++) { VocabWord word = vectors.getVocab().elementAtIndex(i); StringBuilder builder = new StringBuilder(encodeB64(word.getLabel())).append(" "); for (int point : word.getPoints()) { builder.append(point).append(" "); } writer.println(builder.toString().trim()); } } fis = new BufferedInputStream(new FileInputStream(tempFileHuffman)); writeEntry(fis, zipfile); fis.close(); File tempFileFreqs = File.createTempFile("word2vec", "f"); tempFileFreqs.deleteOnExit(); ZipEntry hF = new ZipEntry("frequencies.txt"); zipfile.putNextEntry(hF); // writing out word frequencies try (PrintWriter writer = new PrintWriter(new FileWriter(tempFileFreqs))) { for (int i = 0; i < vectors.getVocab().numWords(); i++) { VocabWord word = vectors.getVocab().elementAtIndex(i); StringBuilder builder = new StringBuilder(encodeB64(word.getLabel())).append(" ") .append(word.getElementFrequency()).append(" ") .append(vectors.getVocab().docAppearedIn(word.getLabel())); writer.println(builder.toString().trim()); } } fis = new BufferedInputStream(new FileInputStream(tempFileFreqs)); writeEntry(fis, zipfile); fis.close(); ZipEntry config = new ZipEntry("config.json"); zipfile.putNextEntry(config); //log.info("Current config: {}", vectors.getConfiguration().toJson()); writeEntry(new ByteArrayInputStream(vectors.getConfiguration().toJson().getBytes()), zipfile); zipfile.flush(); zipfile.close(); try { tempFileCodes.delete(); tempFileFreqs.delete(); tempFileHuffman.delete(); tempFileSyn0.delete(); tempFileSyn1.delete(); tempFileSyn1Neg.delete(); } catch (Exception e) { // } }
From source file:it.govpay.web.rs.dars.reportistica.pagamenti.PagamentiHandler.java
@Override public String esporta(Long idToExport, UriInfo uriInfo, BasicBD bd, ZipOutputStream zout) throws WebApplicationException, ConsoleException { String methodName = "esporta " + this.titoloServizio + "[" + idToExport + "]"; Printer printer = null;/*from w w w . ja va 2s .co m*/ int numeroZipEntries = 0; try { String fileName = "Pagamenti.zip"; this.log.info("Esecuzione " + methodName + " in corso..."); Operatore operatore = this.darsService.getOperatoreByPrincipal(bd); ProfiloOperatore profilo = operatore.getProfilo(); boolean isAdmin = profilo.equals(ProfiloOperatore.ADMIN); it.govpay.core.business.EstrattoConto estrattoContoBD = new it.govpay.core.business.EstrattoConto(bd); EstrattiContoBD pagamentiBD = new EstrattiContoBD(bd); EstrattoContoFilter filter = pagamentiBD.newFilter(); boolean eseguiRicerca = true; List<Long> ids = new ArrayList<Long>(); ids.add(idToExport); SingoliVersamentiBD singoliVersamentiBD = new SingoliVersamentiBD(bd); if (!isAdmin) { AclBD aclBD = new AclBD(bd); List<Acl> aclOperatore = aclBD.getAclOperatore(operatore.getId()); boolean vediTuttiDomini = false; List<Long> idDomini = new ArrayList<Long>(); for (Acl acl : aclOperatore) { if (Tipo.DOMINIO.equals(acl.getTipo())) { if (acl.getIdDominio() == null) { vediTuttiDomini = true; break; } else { idDomini.add(acl.getIdDominio()); } } } if (!vediTuttiDomini) { if (idDomini.isEmpty()) { eseguiRicerca = false; } else { filter.setIdDomini(toListCodDomini(idDomini, bd)); } } // l'operatore puo' vedere i domini associati, controllo se c'e' un versamento con Id nei domini concessi. if (eseguiRicerca) { filter.setIdSingoloVersamento(ids); eseguiRicerca = eseguiRicerca && pagamentiBD.count(filter) > 0; } } if (eseguiRicerca) { // recupero oggetto filter.setIdSingoloVersamento(ids); List<EstrattoConto> findAll = eseguiRicerca ? pagamentiBD.estrattoContoFromIdSingoliVersamenti(filter) : new ArrayList<EstrattoConto>(); if (findAll != null && findAll.size() > 0) { numeroZipEntries++; //ordinamento record Collections.sort(findAll, new EstrattoContoComparator()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ZipEntry pagamentoCsv = new ZipEntry("pagamenti.csv"); zout.putNextEntry(pagamentoCsv); printer = new Printer(this.getFormat(), baos); printer.printRecord(CSVUtils.getEstrattoContoCsvHeader()); for (EstrattoConto pagamento : findAll) { printer.printRecord(CSVUtils.getEstrattoContoAsCsvRow(pagamento, this.sdf)); } } finally { try { if (printer != null) { printer.close(); } } catch (Exception e) { throw new Exception("Errore durante la chiusura dello stream ", e); } } zout.write(baos.toByteArray()); zout.closeEntry(); } SingoloVersamento singoloVersamento = singoliVersamentiBD.getSingoloVersamento(idToExport); Versamento versamento = singoloVersamento.getVersamento(bd); UnitaOperativa uo = AnagraficaManager.getUnitaOperativa(bd, versamento.getIdUo()); Dominio dominio = AnagraficaManager.getDominio(bd, uo.getIdDominio()); // Estratto conto per iban e codiceversamento. List<Long> idSingoliVersamentiDominio = new ArrayList<Long>(); idSingoliVersamentiDominio.add(idToExport); it.govpay.core.business.model.EstrattoConto input = it.govpay.core.business.model.EstrattoConto .creaEstrattoContoPagamentiPDF(dominio, idSingoliVersamentiDominio); List<it.govpay.core.business.model.EstrattoConto> listInputEstrattoConto = new ArrayList<it.govpay.core.business.model.EstrattoConto>(); listInputEstrattoConto.add(input); String pathLoghi = ConsoleProperties.getInstance().getPathEstrattoContoPdfLoghi(); List<it.govpay.core.business.model.EstrattoConto> listOutputEstattoConto = estrattoContoBD .getEstrattoContoPagamenti(listInputEstrattoConto, pathLoghi); for (it.govpay.core.business.model.EstrattoConto estrattoContoOutput : listOutputEstattoConto) { Map<String, ByteArrayOutputStream> estrattoContoVersamenti = estrattoContoOutput.getOutput(); for (String nomeEntry : estrattoContoVersamenti.keySet()) { numeroZipEntries++; ByteArrayOutputStream baos = estrattoContoVersamenti.get(nomeEntry); // ZipEntry estrattoContoEntry = new ZipEntry(estrattoContoOutput.getDominio().getCodDominio() + "/" + nomeEntry); ZipEntry estrattoContoEntry = new ZipEntry(nomeEntry); zout.putNextEntry(estrattoContoEntry); zout.write(baos.toByteArray()); zout.closeEntry(); } } } // se non ho inserito nessuna entry if (numeroZipEntries == 0) { String noEntriesTxt = "/README"; ZipEntry entryTxt = new ZipEntry(noEntriesTxt); zout.putNextEntry(entryTxt); zout.write("Non sono state trovate informazioni sui pagamenti selezionati.".getBytes()); zout.closeEntry(); } zout.flush(); zout.close(); this.log.info("Esecuzione " + methodName + " completata."); return fileName; } catch (WebApplicationException e) { throw e; } catch (Exception e) { throw new ConsoleException(e); } }
From source file:it.govpay.web.rs.dars.monitoraggio.pagamenti.ReportisticaPagamentiHandler.java
@Override public String esporta(Long idToExport, List<RawParamValue> rawValues, UriInfo uriInfo, BasicBD bd, ZipOutputStream zout) throws WebApplicationException, ConsoleException, ExportException { String methodName = "esporta " + this.titoloServizio + "[" + idToExport + "]"; Printer printer = null;//from www. ja va 2 s . c o m int numeroZipEntries = 0; try { String fileName = "Pagamenti.zip"; this.log.info("Esecuzione " + methodName + " in corso..."); // Operatore operatore = this.darsService.getOperatoreByPrincipal(bd); it.govpay.core.business.EstrattoConto estrattoContoBD = new it.govpay.core.business.EstrattoConto(bd); EstrattiContoBD pagamentiBD = new EstrattiContoBD(bd); EstrattoContoFilter filter = pagamentiBD.newFilter(); boolean eseguiRicerca = true; List<Long> ids = new ArrayList<Long>(); ids.add(idToExport); SingoliVersamentiBD singoliVersamentiBD = new SingoliVersamentiBD(bd); // if(!isAdmin){ // // AclBD aclBD = new AclBD(bd); // List<Acl> aclOperatore = aclBD.getAclOperatore(operatore.getId()); // // boolean vediTuttiDomini = false; // List<Long> idDomini = new ArrayList<Long>(); // for(Acl acl: aclOperatore) { // if(Tipo.DOMINIO.equals(acl.getTipo())) { // if(acl.getIdDominio() == null) { // vediTuttiDomini = true; // break; // } else { // idDomini.add(acl.getIdDominio()); // } // } // } // if(!vediTuttiDomini) { // if(idDomini.isEmpty()) { // eseguiRicerca = false; // } else { // filter.setIdDomini(toListCodDomini(idDomini, bd)); // } // } // // // l'operatore puo' vedere i domini associati, controllo se c'e' un versamento con Id nei domini concessi. // if(eseguiRicerca){ // filter.setIdSingoloVersamento(ids); // eseguiRicerca = eseguiRicerca && pagamentiBD.count(filter) > 0; // } // } if (eseguiRicerca) { // recupero oggetto filter.setIdSingoloVersamento(ids); List<EstrattoConto> findAll = eseguiRicerca ? pagamentiBD.estrattoContoFromIdSingoliVersamenti(filter) : new ArrayList<EstrattoConto>(); if (findAll != null && findAll.size() > 0) { numeroZipEntries++; //ordinamento record Collections.sort(findAll, new EstrattoContoComparator()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ZipEntry pagamentoCsv = new ZipEntry("pagamenti.csv"); zout.putNextEntry(pagamentoCsv); printer = new Printer(this.getFormat(), baos); printer.printRecord(CSVUtils.getEstrattoContoCsvHeader()); for (EstrattoConto pagamento : findAll) { printer.printRecord(CSVUtils.getEstrattoContoAsCsvRow(pagamento, this.sdf)); } } finally { try { if (printer != null) { printer.close(); } } catch (Exception e) { throw new Exception("Errore durante la chiusura dello stream ", e); } } zout.write(baos.toByteArray()); zout.closeEntry(); } SingoloVersamento singoloVersamento = singoliVersamentiBD.getSingoloVersamento(idToExport); Versamento versamento = singoloVersamento.getVersamento(bd); UnitaOperativa uo = AnagraficaManager.getUnitaOperativa(bd, versamento.getIdUo()); Dominio dominio = AnagraficaManager.getDominio(bd, uo.getIdDominio()); // Estratto conto per iban e codiceversamento. List<Long> idSingoliVersamentiDominio = new ArrayList<Long>(); idSingoliVersamentiDominio.add(idToExport); it.govpay.core.business.model.EstrattoConto input = it.govpay.core.business.model.EstrattoConto .creaEstrattoContoPagamentiPDF(dominio, idSingoliVersamentiDominio); List<it.govpay.core.business.model.EstrattoConto> listInputEstrattoConto = new ArrayList<it.govpay.core.business.model.EstrattoConto>(); listInputEstrattoConto.add(input); String pathLoghi = ConsoleProperties.getInstance().getPathEstrattoContoPdfLoghi(); List<it.govpay.core.business.model.EstrattoConto> listOutputEstattoConto = estrattoContoBD .getEstrattoContoPagamenti(listInputEstrattoConto, pathLoghi); for (it.govpay.core.business.model.EstrattoConto estrattoContoOutput : listOutputEstattoConto) { Map<String, ByteArrayOutputStream> estrattoContoVersamenti = estrattoContoOutput.getOutput(); for (String nomeEntry : estrattoContoVersamenti.keySet()) { numeroZipEntries++; ByteArrayOutputStream baos = estrattoContoVersamenti.get(nomeEntry); // ZipEntry estrattoContoEntry = new ZipEntry(estrattoContoOutput.getDominio().getCodDominio() + "/" + nomeEntry); ZipEntry estrattoContoEntry = new ZipEntry(nomeEntry); zout.putNextEntry(estrattoContoEntry); zout.write(baos.toByteArray()); zout.closeEntry(); } } } // se non ho inserito nessuna entry if (numeroZipEntries == 0) { String noEntriesTxt = "/README"; ZipEntry entryTxt = new ZipEntry(noEntriesTxt); zout.putNextEntry(entryTxt); zout.write("Non sono state trovate informazioni sui pagamenti selezionati.".getBytes()); zout.closeEntry(); } zout.flush(); zout.close(); this.log.info("Esecuzione " + methodName + " completata."); return fileName; } catch (WebApplicationException e) { throw e; } catch (Exception e) { throw new ConsoleException(e); } }
From source file:it.govpay.web.rs.dars.reportistica.pagamenti.PagamentiHandler.java
@Override public String esporta(List<Long> idsToExport, UriInfo uriInfo, BasicBD bd, ZipOutputStream zout) throws WebApplicationException, ConsoleException { StringBuffer sb = new StringBuffer(); if (idsToExport != null && idsToExport.size() > 0) { for (Long long1 : idsToExport) { if (sb.length() > 0) { sb.append(", "); }//from ww w .j ava 2s. c om sb.append(long1); } } Printer printer = null; String methodName = "esporta " + this.titoloServizio + "[" + sb.toString() + "]"; int numeroZipEntries = 0; if (idsToExport.size() == 1) { return this.esporta(idsToExport.get(0), uriInfo, bd, zout); } String fileName = "Pagamenti.zip"; try { this.log.info("Esecuzione " + methodName + " in corso..."); Operatore operatore = this.darsService.getOperatoreByPrincipal(bd); ProfiloOperatore profilo = operatore.getProfilo(); boolean isAdmin = profilo.equals(ProfiloOperatore.ADMIN); SingoliVersamentiBD singoliVersamentiBD = new SingoliVersamentiBD(bd); EstrattiContoBD estrattiContoBD = new EstrattiContoBD(bd); EstrattoContoFilter filter = estrattiContoBD.newFilter(); boolean eseguiRicerca = true; List<Long> ids = idsToExport; if (!isAdmin) { AclBD aclBD = new AclBD(bd); List<Acl> aclOperatore = aclBD.getAclOperatore(operatore.getId()); boolean vediTuttiDomini = false; List<Long> idDomini = new ArrayList<Long>(); for (Acl acl : aclOperatore) { if (Tipo.DOMINIO.equals(acl.getTipo())) { if (acl.getIdDominio() == null) { vediTuttiDomini = true; break; } else { idDomini.add(acl.getIdDominio()); } } } if (!vediTuttiDomini) { if (idDomini.isEmpty()) { eseguiRicerca = false; } else { filter.setIdDomini(toListCodDomini(idDomini, bd)); } } // l'operatore puo' vedere i domini associati, controllo se c'e' un versamento con Id nei domini concessi. if (eseguiRicerca) { filter.setIdSingoloVersamento(ids); eseguiRicerca = eseguiRicerca && estrattiContoBD.count(filter) > 0; } } if (eseguiRicerca) { Map<String, List<Long>> mappaInputEstrattoConto = new HashMap<String, List<Long>>(); Map<String, Dominio> mappaInputDomini = new HashMap<String, Dominio>(); // recupero oggetto filter.setIdSingoloVersamento(ids); List<EstrattoConto> findAll = eseguiRicerca ? estrattiContoBD.estrattoContoFromIdSingoliVersamenti(filter) : new ArrayList<EstrattoConto>(); if (findAll != null && findAll.size() > 0) { numeroZipEntries++; //ordinamento record Collections.sort(findAll, new EstrattoContoComparator()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ZipEntry pagamentoCsv = new ZipEntry("pagamenti.csv"); zout.putNextEntry(pagamentoCsv); printer = new Printer(this.getFormat(), baos); printer.printRecord(CSVUtils.getEstrattoContoCsvHeader()); for (EstrattoConto pagamento : findAll) { printer.printRecord(CSVUtils.getEstrattoContoAsCsvRow(pagamento, this.sdf)); } } finally { try { if (printer != null) { printer.close(); } } catch (Exception e) { throw new Exception("Errore durante la chiusura dello stream ", e); } } zout.write(baos.toByteArray()); zout.closeEntry(); } for (Long idSingoloVersamento : idsToExport) { SingoloVersamento singoloVersamento = singoliVersamentiBD .getSingoloVersamento(idSingoloVersamento); Versamento versamento = singoloVersamento.getVersamento(bd); // Prelevo il dominio UnitaOperativa uo = AnagraficaManager.getUnitaOperativa(bd, versamento.getIdUo()); Dominio dominio = AnagraficaManager.getDominio(bd, uo.getIdDominio()); // Aggrego i versamenti per dominio per generare gli estratti conto List<Long> idSingoliVersamentiDominio = null; if (mappaInputEstrattoConto.containsKey(dominio.getCodDominio())) { idSingoliVersamentiDominio = mappaInputEstrattoConto.get(dominio.getCodDominio()); } else { idSingoliVersamentiDominio = new ArrayList<Long>(); mappaInputEstrattoConto.put(dominio.getCodDominio(), idSingoliVersamentiDominio); mappaInputDomini.put(dominio.getCodDominio(), dominio); } idSingoliVersamentiDominio.add(idSingoloVersamento); } List<it.govpay.core.business.model.EstrattoConto> listInputEstrattoConto = new ArrayList<it.govpay.core.business.model.EstrattoConto>(); for (String codDominio : mappaInputEstrattoConto.keySet()) { it.govpay.core.business.model.EstrattoConto input = it.govpay.core.business.model.EstrattoConto .creaEstrattoContoPagamentiPDF(mappaInputDomini.get(codDominio), mappaInputEstrattoConto.get(codDominio)); listInputEstrattoConto.add(input); } String pathLoghi = ConsoleProperties.getInstance().getPathEstrattoContoPdfLoghi(); it.govpay.core.business.EstrattoConto estrattoContoBD = new it.govpay.core.business.EstrattoConto( bd); List<it.govpay.core.business.model.EstrattoConto> listOutputEstattoConto = estrattoContoBD .getEstrattoContoPagamenti(listInputEstrattoConto, pathLoghi); for (it.govpay.core.business.model.EstrattoConto estrattoContoOutput : listOutputEstattoConto) { Map<String, ByteArrayOutputStream> estrattoContoVersamenti = estrattoContoOutput.getOutput(); for (String nomeEntry : estrattoContoVersamenti.keySet()) { numeroZipEntries++; ByteArrayOutputStream baos = estrattoContoVersamenti.get(nomeEntry); ZipEntry estrattoContoEntry = new ZipEntry(nomeEntry); // ZipEntry estrattoContoEntry = new ZipEntry(estrattoContoOutput.getDominio().getCodDominio() + "/" + nomeEntry); zout.putNextEntry(estrattoContoEntry); zout.write(baos.toByteArray()); zout.closeEntry(); } } } // se non ho inserito nessuna entry if (numeroZipEntries == 0) { String noEntriesTxt = "/README"; ZipEntry entryTxt = new ZipEntry(noEntriesTxt); zout.putNextEntry(entryTxt); zout.write("Non sono state trovate informazioni sui pagamenti selezionati.".getBytes()); zout.closeEntry(); } zout.flush(); zout.close(); this.log.info("Esecuzione " + methodName + " completata."); return fileName; } catch (WebApplicationException e) { throw e; } catch (Exception e) { throw new ConsoleException(e); } }