List of usage examples for java.util.zip ZipOutputStream ZipOutputStream
public ZipOutputStream(OutputStream out)
From source file:edu.isi.wings.portal.classes.StorageHandler.java
private static void streamDirectory(File directory, OutputStream os) { try {//from www .j a v a2 s . c om // Start the ZipStream reader. Whatever is read is streamed to response PipedInputStream pis = new PipedInputStream(2048); ZipStreamer pipestreamer = new ZipStreamer(pis, os); pipestreamer.start(); // Start Zipping folder and piping to the ZipStream reader PipedOutputStream pos = new PipedOutputStream(pis); ZipOutputStream zos = new ZipOutputStream(pos); StorageHandler.zipAndStream(directory, zos, directory.getName() + "/"); zos.flush(); zos.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:fr.cirad.mgdb.exporting.markeroriented.EigenstratExportHandler.java
@Override public void exportData(OutputStream outputStream, String sModule, List<SampleId> sampleIDs, ProgressIndicator progress, DBCursor markerCursor, Map<Comparable, Comparable> markerSynonyms, int nMinimumGenotypeQuality, int nMinimumReadDepth, Map<String, InputStream> readyToExportFiles) throws Exception { // long before = System.currentTimeMillis(); File warningFile = File.createTempFile("export_warnings_", ""); FileWriter warningFileWriter = new FileWriter(warningFile); File snpFile = null;// w ww . jav a 2 s . co m try { snpFile = File.createTempFile("snpFile", ""); FileWriter snpFileWriter = new FileWriter(snpFile); ZipOutputStream zos = new ZipOutputStream(outputStream); if (ByteArrayOutputStream.class.isAssignableFrom(outputStream.getClass())) zos.setLevel(ZipOutputStream.STORED); if (readyToExportFiles != null) for (String readyToExportFile : readyToExportFiles.keySet()) { zos.putNextEntry(new ZipEntry(readyToExportFile)); InputStream inputStream = readyToExportFiles.get(readyToExportFile); byte[] dataBlock = new byte[1024]; int count = inputStream.read(dataBlock, 0, 1024); while (count != -1) { zos.write(dataBlock, 0, count); count = inputStream.read(dataBlock, 0, 1024); } } MongoTemplate mongoTemplate = MongoTemplateManager.get(sModule); int markerCount = markerCursor.count(); List<Individual> individuals = getIndividualsFromSamples(sModule, sampleIDs); ArrayList<String> individualList = new ArrayList<String>(); StringBuffer indFileContents = new StringBuffer(); for (int i = 0; i < sampleIDs.size(); i++) { Individual individual = individuals.get(i); if (!individualList.contains(individual.getId())) { individualList.add(individual.getId()); indFileContents .append(individual.getId() + "\t" + getIndividualGenderCode(sModule, individual.getId()) + "\t" + (individual.getPopulation() == null ? "." : individual.getPopulation()) + LINE_SEPARATOR); } } String exportName = sModule + "_" + markerCount + "variants_" + individualList.size() + "individuals"; zos.putNextEntry(new ZipEntry(exportName + ".ind")); zos.write(indFileContents.toString().getBytes()); zos.putNextEntry(new ZipEntry(exportName + ".eigenstratgeno")); int avgObjSize = (Integer) mongoTemplate .getCollection(mongoTemplate.getCollectionName(VariantRunData.class)).getStats() .get("avgObjSize"); int nChunkSize = nMaxChunkSizeInMb * 1024 * 1024 / avgObjSize; short nProgress = 0, nPreviousProgress = 0; long nLoadedMarkerCount = 0; while (markerCursor.hasNext()) { int nLoadedMarkerCountInLoop = 0; Map<Comparable, String> markerChromosomalPositions = new LinkedHashMap<Comparable, String>(); boolean fStartingNewChunk = true; markerCursor.batchSize(nChunkSize); while (markerCursor.hasNext() && (fStartingNewChunk || nLoadedMarkerCountInLoop % nChunkSize != 0)) { DBObject exportVariant = markerCursor.next(); DBObject refPos = (DBObject) exportVariant.get(VariantData.FIELDNAME_REFERENCE_POSITION); markerChromosomalPositions.put((Comparable) exportVariant.get("_id"), refPos.get(ReferencePosition.FIELDNAME_SEQUENCE) + ":" + refPos.get(ReferencePosition.FIELDNAME_START_SITE)); nLoadedMarkerCountInLoop++; fStartingNewChunk = false; } List<Comparable> currentMarkers = new ArrayList<Comparable>(markerChromosomalPositions.keySet()); LinkedHashMap<VariantData, Collection<VariantRunData>> variantsAndRuns = MgdbDao.getSampleGenotypes( mongoTemplate, sampleIDs, currentMarkers, true, null /*new Sort(VariantData.FIELDNAME_REFERENCE_POSITION + "." + ChromosomalPosition.FIELDNAME_SEQUENCE).and(new Sort(VariantData.FIELDNAME_REFERENCE_POSITION + "." + ChromosomalPosition.FIELDNAME_START_SITE))*/); // query mongo db for matching genotypes for (VariantData variant : variantsAndRuns.keySet()) // read data and write results into temporary files (one per sample) { Comparable variantId = variant.getId(); List<String> chromAndPos = Helper.split(markerChromosomalPositions.get(variantId), ":"); if (chromAndPos.size() == 0) LOG.warn("Chromosomal position not found for marker " + variantId); // LOG.debug(marker + "\t" + (chromAndPos.length == 0 ? "0" : chromAndPos[0]) + "\t" + 0 + "\t" + (chromAndPos.length == 0 ? 0l : Long.parseLong(chromAndPos[1])) + LINE_SEPARATOR); if (markerSynonyms != null) { Comparable syn = markerSynonyms.get(variantId); if (syn != null) variantId = syn; } snpFileWriter.write(variantId + "\t" + (chromAndPos.size() == 0 ? "0" : chromAndPos.get(0)) + "\t" + 0 + "\t" + (chromAndPos.size() == 0 ? 0l : Long.parseLong(chromAndPos.get(1))) + LINE_SEPARATOR); Map<String, List<String>> individualGenotypes = new LinkedHashMap<String, List<String>>(); Collection<VariantRunData> runs = variantsAndRuns.get(variant); if (runs != null) for (VariantRunData run : runs) for (Integer sampleIndex : run.getSampleGenotypes().keySet()) { SampleGenotype sampleGenotype = run.getSampleGenotypes().get(sampleIndex); String individualId = individuals .get(sampleIDs .indexOf(new SampleId(run.getId().getProjectId(), sampleIndex))) .getId(); Integer gq = null; try { gq = (Integer) sampleGenotype.getAdditionalInfo().get(VariantData.GT_FIELD_GQ); } catch (Exception ignored) { } if (gq != null && gq < nMinimumGenotypeQuality) continue; Integer dp = null; try { dp = (Integer) sampleGenotype.getAdditionalInfo().get(VariantData.GT_FIELD_DP); } catch (Exception ignored) { } if (dp != null && dp < nMinimumReadDepth) continue; String gtCode = sampleGenotype.getCode(); List<String> storedIndividualGenotypes = individualGenotypes.get(individualId); if (storedIndividualGenotypes == null) { storedIndividualGenotypes = new ArrayList<String>(); individualGenotypes.put(individualId, storedIndividualGenotypes); } storedIndividualGenotypes.add(gtCode); } for (int j = 0; j < individualList .size(); j++ /* we use this list because it has the proper ordering*/) { String individualId = individualList.get(j); List<String> genotypes = individualGenotypes.get(individualId); HashMap<Object, Integer> genotypeCounts = new HashMap<Object, Integer>(); // will help us to keep track of missing genotypes int highestGenotypeCount = 0; String mostFrequentGenotype = null; if (genotypes != null) for (String genotype : genotypes) { if (genotype.length() == 0) continue; /* skip missing genotypes */ int gtCount = 1 + MgdbDao.getCountForKey(genotypeCounts, genotype); if (gtCount > highestGenotypeCount) { highestGenotypeCount = gtCount; mostFrequentGenotype = genotype; } genotypeCounts.put(genotype, gtCount); } List<String> alleles = mostFrequentGenotype == null ? new ArrayList<String>() : variant.getAllelesFromGenotypeCode(mostFrequentGenotype); int nOutputCode = 0; if (mostFrequentGenotype == null) nOutputCode = 9; else for (String all : Helper.split(mostFrequentGenotype, "/")) if ("0".equals(all)) nOutputCode++; if (j == 0 && variant.getKnownAlleleList().size() > 2) warningFileWriter.write("- Variant " + variant.getId() + " is multi-allelic. Make sure Eigenstrat genotype encoding specifications are suitable for you.\n"); zos.write(("" + nOutputCode).getBytes()); if (genotypeCounts.size() > 1 || alleles.size() > 2) { if (genotypeCounts.size() > 1) warningFileWriter.write("- Dissimilar genotypes found for variant " + (variantId == null ? variant.getId() : variantId) + ", individual " + individualId + ". Exporting most frequent: " + nOutputCode + "\n"); if (alleles.size() > 2) warningFileWriter.write("- More than 2 alleles found for variant " + (variantId == null ? variant.getId() : variantId) + ", individual " + individualId + ". Exporting only the first 2 alleles.\n"); } } zos.write((LINE_SEPARATOR).getBytes()); } if (progress.hasAborted()) return; nLoadedMarkerCount += nLoadedMarkerCountInLoop; nProgress = (short) (nLoadedMarkerCount * 100 / markerCount); if (nProgress > nPreviousProgress) { // if (nProgress%5 == 0) // LOG.info("============= exportData: " + nProgress + "% =============" + (System.currentTimeMillis() - before)/1000 + "s"); progress.setCurrentStepProgress(nProgress); nPreviousProgress = nProgress; } } snpFileWriter.close(); zos.putNextEntry(new ZipEntry(exportName + ".snp")); BufferedReader in = new BufferedReader(new FileReader(snpFile)); String sLine; while ((sLine = in.readLine()) != null) zos.write((sLine + "\n").getBytes()); in.close(); warningFileWriter.close(); if (warningFile.length() > 0) { zos.putNextEntry(new ZipEntry(exportName + "-REMARKS.txt")); int nWarningCount = 0; in = new BufferedReader(new FileReader(warningFile)); while ((sLine = in.readLine()) != null) { zos.write((sLine + "\n").getBytes()); nWarningCount++; } LOG.info("Number of Warnings for export (" + exportName + "): " + nWarningCount); in.close(); } warningFile.delete(); zos.close(); progress.setCurrentStepProgress((short) 100); } finally { if (snpFile != null && snpFile.exists()) snpFile.delete(); } }
From source file:eu.esdihumboldt.hale.io.appschema.writer.AppSchemaMappingUploader.java
private byte[] writeContent(String mappingFileName, ContentType contentType, ProgressIndicator progress, IOReporter reporter) throws IOException { try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { if (contentType.equals(DataStoreFile.ZIP_CONTENT_TYPE)) { try (ZipOutputStream zos = new ZipOutputStream(bos)) { if (includeTargetSchema) { // add target schema to zip addTargetSchemaToZip(zos, null, progress, reporter); }//from w w w. j a va2 s . co m // main mapping configuration file zos.putNextEntry(new ZipEntry(mappingFileName + ".appschema")); generator.writeMappingConf(zos); zos.closeEntry(); if (generator.getGeneratedMapping().requiresMultipleFiles()) { zos.putNextEntry(new ZipEntry(AppSchemaIO.INCLUDED_TYPES_MAPPING_FILE)); generator.writeIncludedTypesMappingConf(zos); zos.closeEntry(); } } } else { generator.writeMappingConf(bos); } return bos.toByteArray(); } }
From source file:edu.lternet.pasta.datapackagemanager.DataPackageArchive.java
/** * Generate an "archive" of the data package by parsing and retrieving * components of the data package resource map * //from w w w . j ava 2 s .c om * @param scope * The scope value of the data package * @param identifier * The identifier value of the data package * @param revision * The revision value of the data package * @param map * The resource map of the data package * @param authToken * The authentication token of the user requesting the archive * @param transaction * The transaction id of the request * @return The file name of the data package archive * @throws Exception */ public String createDataPackageArchive(String scope, Integer identifier, Integer revision, String userId, AuthToken authToken, String transaction) throws Exception { String zipName = transaction + ".zip"; String zipPath = tmpDir + "/"; EmlPackageId emlPackageId = new EmlPackageId(scope, identifier, revision); StringBuffer manifest = new StringBuffer(); Date now = new Date(); manifest.append("Manifest file for " + zipName + " created on " + now.toString() + "\n"); DataPackageManager dpm = null; /* * It is necessary to create a temporary file while building the ZIP archive * to prevent the client from accessing an incomplete product. */ String tmpName = DigestUtils.md5Hex(transaction); File zFile = new File(zipPath + tmpName); if (zFile.exists()) { String gripe = "The resource " + zipName + "already exists!"; throw new ResourceExistsException(gripe); } try { dpm = new DataPackageManager(); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); throw e; } FileOutputStream fOut = null; try { fOut = new FileOutputStream(zFile); } catch (FileNotFoundException e) { logger.error(e.getMessage()); e.printStackTrace(); } if (dpm != null && fOut != null) { String map = null; try { map = dpm.readDataPackage(scope, identifier, revision.toString(), authToken, userId); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); throw e; } Scanner mapScanner = new Scanner(map); ZipOutputStream zOut = new ZipOutputStream(fOut); while (mapScanner.hasNextLine()) { FileInputStream fIn = null; String objectName = null; File file = null; String line = mapScanner.nextLine(); if (line.contains(URI_MIDDLE_METADATA)) { try { file = dpm.getMetadataFile(scope, identifier, revision.toString(), userId, authToken); objectName = emlPackageId.toString() + ".xml"; } catch (ClassNotFoundException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (SQLException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } if (file != null) { try { fIn = new FileInputStream(file); Long size = FileUtils.sizeOf(file); manifest.append(objectName + " (" + size.toString() + " bytes)\n"); } catch (FileNotFoundException e) { logger.error(e.getMessage()); e.printStackTrace(); } } } else if (line.contains(URI_MIDDLE_REPORT)) { try { file = dpm.readDataPackageReport(scope, identifier, revision.toString(), emlPackageId, authToken, userId); objectName = emlPackageId.toString() + ".report.xml"; } catch (ClassNotFoundException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (SQLException e) { logger.error(e.getMessage()); e.printStackTrace(); } if (file != null) { try { fIn = new FileInputStream(file); Long size = FileUtils.sizeOf(file); manifest.append(objectName + " (" + size.toString() + " bytes)\n"); } catch (FileNotFoundException e) { logger.error(e.getMessage()); e.printStackTrace(); } } } else if (line.contains(URI_MIDDLE_DATA)) { String[] lineParts = line.split("/"); String entityId = lineParts[lineParts.length - 1]; String dataPackageResourceId = DataPackageManager.composeResourceId(ResourceType.dataPackage, scope, identifier, revision, null); String entityResourceId = DataPackageManager.composeResourceId(ResourceType.data, scope, identifier, revision, entityId); String entityName = null; String xml = null; try { entityName = dpm.readDataEntityName(dataPackageResourceId, entityResourceId, authToken); xml = dpm.readMetadata(scope, identifier, revision.toString(), userId, authToken); objectName = dpm.findObjectName(xml, entityName); file = dpm.getDataEntityFile(scope, identifier, revision.toString(), entityId, authToken, userId); } catch (UnauthorizedException e) { logger.error(e.getMessage()); e.printStackTrace(); manifest.append(objectName + " (access denied)\n"); } catch (ResourceNotFoundException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (ClassNotFoundException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (SQLException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } if (file != null) { try { fIn = new FileInputStream(file); Long size = FileUtils.sizeOf(file); manifest.append(objectName + " (" + size.toString() + " bytes)\n"); } catch (FileNotFoundException e) { logger.error(e.getMessage()); e.printStackTrace(); } } } if (objectName != null && fIn != null) { ZipEntry zipEntry = new ZipEntry(objectName); try { zOut.putNextEntry(zipEntry); int length; byte[] buffer = new byte[1024]; while ((length = fIn.read(buffer)) > 0) { zOut.write(buffer, 0, length); } zOut.closeEntry(); fIn.close(); } catch (IOException e) { logger.error(e.getMessage()); e.printStackTrace(); } } } // Create ZIP archive manifest File mFile = new File(zipPath + transaction + ".txt"); FileUtils.writeStringToFile(mFile, manifest.toString()); ZipEntry zipEntry = new ZipEntry("manifest.txt"); try { FileInputStream fIn = new FileInputStream(mFile); zOut.putNextEntry(zipEntry); int length; byte[] buffer = new byte[1024]; while ((length = fIn.read(buffer)) > 0) { zOut.write(buffer, 0, length); } zOut.closeEntry(); fIn.close(); } catch (IOException e) { logger.error(e.getMessage()); e.printStackTrace(); } // Close ZIP archive zOut.close(); FileUtils.forceDelete(mFile); } File tmpFile = new File(zipPath + tmpName); File zipFile = new File(zipPath + zipName); // Copy hidden ZIP archive to visible ZIP archive, thus making available if (!tmpFile.renameTo(zipFile)) { String gripe = "Error renaming " + tmpName + " to " + zipName + "!"; throw new IOException(); } return zipName; }
From source file:S3DataManagerTest.java
@Test public void testZipSourceEmptyDir() throws Exception { clearSourceDirectory();//from ww w. j av a 2 s .c o m ZipOutputStream out = new ZipOutputStream(new FileOutputStream("/tmp/source.zip")); S3DataManager dataManager = createDefaultSource(); dataManager.zipSource("/tmp/source/", out, "/tmp/source/"); out.close(); File zip = new File("/tmp/source.zip"); assertTrue(zip.exists()); File unzipFolder = new File("/tmp/folder/"); unzipFolder.mkdir(); ZipFile z = new ZipFile(zip.getPath()); assertFalse(z.isValidZipFile()); //checks that zipfile is empty }
From source file:com.coinblesk.client.backup.BackupDialogFragment.java
private void backup() { final File backupFile = newWalletBackupFile(); final String password = txtPassword.getText().toString(); Preconditions.checkState(password.equals(txtPasswordAgain.getText().toString()) && password.length() > 0); clearPasswordInput();/*from w w w . ja v a 2s . c o m*/ Writer fileOut = null; try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ZipOutputStream zos = new ZipOutputStream(baos); Log.i(TAG, "Backup file: " + backupFile); // assemble all content to backup addFilesToZip(zos); // encrypt zip bytes and write to file zos.close(); byte[] plainBackup = baos.toByteArray(); String encryptedBackup = EncryptionUtils.encrypt(plainBackup, password.toCharArray()); fileOut = new OutputStreamWriter(new FileOutputStream(backupFile), Charsets.UTF_8); fileOut.write(encryptedBackup); fileOut.flush(); Log.i(TAG, "Wallet backup finished. File = [" + backupFile + "], size = " + backupFile.length() + "bytes, exists = " + backupFile.exists()); // ask user whether he wants backup file as mail attachment showBackupCompletedDialog(backupFile); } catch (Exception e) { Log.w(TAG, "Could not write to file", e); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.fragment_backup_failed_title) .setMessage(getString(R.string.fragment_backup_failed_message) + ": " + e.getMessage()) .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }).create().show(); } finally { if (fileOut != null) { try { fileOut.close(); } catch (IOException e) { Log.i(TAG, "Could not close output stream"); } } } }
From source file:ch.randelshofer.cubetwister.HTMLExporter.java
public void exportToZipFile(String documentName, DocumentModel model, File zipFile, ProgressObserver p) throws IOException { this.documentName = documentName; this.model = model; this.dir = null; this.zipFile = zipFile; this.p = p;/*www.j a v a2 s .c o m*/ zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile))); try { } finally { zipOut.close(); } init(); processHTMLTemplates(p); }
From source file:com.aurel.track.exchange.track.exporter.TrackExportBL.java
public static ZipOutputStream exportWorkItemsWithAttachments(List<ReportBean> reportBeanList, Integer personID, OutputStream outputStream) { ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); Document document = exportWorkItems(reportBeanList, personID); ZipEntry dataEntry = new ZipEntry(ExchangeFieldNames.EXCHANGE_ZIP_ENTRY); try {// w ww . java 2 s. co m zipOutputStream.putNextEntry(dataEntry); } catch (IOException e) { LOGGER.error("Adding the XML data to the zip failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } ReportBeansToXML.convertToXml(zipOutputStream, document); //zipOutputStream.closeEntry(); //get the attachments for each workItem (if exist) if (reportBeanList != null) { for (ReportBean reportBean : reportBeanList) { TWorkItemBean workItemBean = reportBean.getWorkItemBean(); Integer workItemID = workItemBean.getObjectID(); String workItemAttachmentsDirectory = AttachBL.getFullDirName(null, workItemID); File file = new File(workItemAttachmentsDirectory); if (file.exists() && file.isDirectory()) { ReportBL.zipFiles(file, zipOutputStream, file.getAbsolutePath()); } } } try { zipOutputStream.close(); } catch (IOException e) { LOGGER.warn("Closing the zip failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } return zipOutputStream; }
From source file:com.genericworkflownodes.knime.nodes.io.outputfile.OutputFileNodeModel.java
/** * {@inheritDoc}//from w w w. j a v a 2 s .c o m */ @Override protected void saveInternals(final File internDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(new File(internDir, "loadeddata"))); ZipEntry entry = new ZipEntry("rawdata.bin"); out.putNextEntry(entry); out.write(data.getBytes()); out.close(); }
From source file:com.microsoft.tfs.client.common.ui.teambuild.commands.CreateUploadZipCommand.java
/** * Zip a file or folder to a zip file/*from ww w .j a v a2s. c om*/ * * * @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(); } }