List of usage examples for java.util.zip ZipOutputStream flush
public void flush() throws IOException
From source file:com.jaspersoft.jasperserver.api.engine.scheduling.quartz.ReportExecutionJobFileSavingImpl.java
private Map<String, byte[]> zipFile(ReportExecutionJob job, ReportOutput output, boolean useFolderHierarchy) throws IOException { String attachmentName;//from www . jav a2 s . co m DataContainer attachmentData; if (output.getChildren().isEmpty()) { attachmentName = output.getFilename(); attachmentData = output.getData(); } else { // use zip format attachmentData = job.createDataContainer(); boolean close = true; ZipOutputStream zipOut = new ZipOutputStream(attachmentData.getOutputStream()); try { zipOut.putNextEntry(new ZipEntry(output.getFilename())); DataContainerStreamUtil.pipeDataAndCloseInput(output.getData().getInputStream(), zipOut); zipOut.closeEntry(); for (Iterator it = output.getChildren().iterator(); it.hasNext();) { ReportOutput child = (ReportOutput) it.next(); String childName = child.getFilename(); if (useFolderHierarchy) childName = getChildrenFolderName(job, output.getFilename()) + '/' + childName; zipOut.putNextEntry(new ZipEntry(childName)); DataContainerStreamUtil.pipeDataAndCloseInput(child.getData().getInputStream(), zipOut); zipOut.closeEntry(); } 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); } } } attachmentName = output.getFilename() + ".zip"; } Map result = new HashMap<String, InputStream>(); result.put(attachmentName, IOUtils.toByteArray(attachmentData.getInputStream())); return result; }
From source file:com.simiacryptus.mindseye.lang.Layer.java
/** * Write zip./*from www . j ava2 s. c om*/ * * @param out the out * @param precision the precision */ default void writeZip(@Nonnull ZipOutputStream out, SerialPrecision precision) { try { @Nonnull HashMap<CharSequence, byte[]> resources = new HashMap<>(); JsonObject json = getJson(resources, precision); out.putNextEntry(new ZipEntry("model.json")); @Nonnull JsonWriter writer = new JsonWriter(new OutputStreamWriter(out)); writer.setIndent(" "); writer.setHtmlSafe(true); writer.setSerializeNulls(false); new GsonBuilder().setPrettyPrinting().create().toJson(json, writer); writer.flush(); out.closeEntry(); resources.forEach((name, data) -> { try { out.putNextEntry(new ZipEntry(String.valueOf(name))); IOUtils.write(data, out); out.flush(); out.closeEntry(); } catch (IOException e) { throw new RuntimeException(e); } }); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.funambol.framework.tools.FileArchiverTest.java
public void testAddDirectory_NotRecursive() throws Throwable { String sourceDirname = MULTILEVELTREE + File.separator + "first"; String destFilename = BASE_TARGET_DIR + "addedDir.zip"; File destFile = new File(destFilename); if (destFile.exists()) { assertTrue("Unable to delete destination file", destFile.delete()); }/* w ww . j av a2s.c o m*/ // recursively = false, includeRoot = true FileArchiver instance = new FileArchiver(sourceDirname, destFilename, false, true); FileOutputStream outputStream = null; ZipOutputStream zipOutputStream = null; try { outputStream = new FileOutputStream(destFile, false); zipOutputStream = new ZipOutputStream(outputStream); PrivateAccessor.invoke(instance, "addDirectory", new Class[] { ZipOutputStream.class, String.class, File.class }, new Object[] { zipOutputStream, "", new File(sourceDirname) }); assertCounters(instance, 1, 1, 0, 0); assertDestFile(destFile, true); } finally { if (zipOutputStream != null) { zipOutputStream.flush(); zipOutputStream.close(); } if (outputStream != null) { outputStream.flush(); outputStream.close(); } } List<String> expContent = new ArrayList<String>(); expContent.add("first/"); expContent.add("first/firstlog.zip"); assertDestFileContent(destFile, expContent); }
From source file:com.funambol.framework.tools.FileArchiverTest.java
public void testAddFile() throws Throwable { String sourceFilename = MULTILEVELTREE + File.separator + "multilog.txt"; String destFilename = BASE_TARGET_DIR + "addedFile.zip"; File destFile = new File(destFilename); if (destFile.exists()) { assertTrue("Unable to delete destination file", destFile.delete()); }//from w w w .j a v a2 s.com // recursively = true, includeRoot = true FileArchiver instance = new FileArchiver(sourceFilename, destFilename, true, true); FileOutputStream outputStream = null; ZipOutputStream zipOutputStream = null; try { outputStream = new FileOutputStream(destFile, false); zipOutputStream = new ZipOutputStream(outputStream); PrivateAccessor.invoke(instance, "addFile", new Class[] { ZipOutputStream.class, String.class, File.class }, new Object[] { zipOutputStream, "", new File(sourceFilename) }); assertCounters(instance, 0, 1, 0, 0); assertDestFile(destFile, true); } finally { if (zipOutputStream != null) { zipOutputStream.flush(); } if (outputStream != null) { outputStream.flush(); } } try { sourceFilename = MULTILEVELTREE + File.separator + "first" + File.separator + "firstlog.zip"; PrivateAccessor.setField(instance, "sourceFile", new File(sourceFilename)); PrivateAccessor.invoke(instance, "addFile", new Class[] { ZipOutputStream.class, String.class, File.class }, new Object[] { zipOutputStream, "", new File(sourceFilename) }); assertCounters(instance, 0, 2, 0, 0); assertDestFile(destFile, true); } finally { if (zipOutputStream != null) { zipOutputStream.flush(); zipOutputStream.close(); } if (outputStream != null) { outputStream.flush(); outputStream.close(); } } List<String> expContent = new ArrayList<String>(); expContent.add("multilog.txt"); expContent.add("firstlog.zip"); assertDestFileContent(destFile, expContent); }
From source file:com.funambol.framework.tools.FileArchiverTest.java
public void testAddFile_WithFilter() throws Throwable { String sourceFilename = MULTILEVELTREE + File.separator + "multilog.txt"; String destFilename = BASE_TARGET_DIR + "addedFile.zip"; File destFile = new File(destFilename); if (destFile.exists()) { assertTrue("Unable to delete destination file", destFile.delete()); }//from w w w . ja v a 2 s. c o m // recursively = true, includeRoot = true FileArchiver instance = new FileArchiver(sourceFilename, destFilename, true, true); // applies a filter based on a suffix SuffixFileFilter fileFilter = new SuffixFileFilter("txt"); instance.setFilter(fileFilter); FileOutputStream outputStream = null; ZipOutputStream zipOutputStream = null; try { outputStream = new FileOutputStream(destFile, false); zipOutputStream = new ZipOutputStream(outputStream); PrivateAccessor.invoke(instance, "addFile", new Class[] { ZipOutputStream.class, String.class, File.class }, new Object[] { zipOutputStream, "", new File(sourceFilename) }); assertCounters(instance, 0, 1, 0, 0); assertDestFile(destFile, true); } finally { if (zipOutputStream != null) { zipOutputStream.flush(); } if (outputStream != null) { outputStream.flush(); } } try { sourceFilename = MULTILEVELTREE + File.separator + "first" + File.separator + "firstlog.zip"; PrivateAccessor.setField(instance, "sourceFile", new File(sourceFilename)); PrivateAccessor.invoke(instance, "addFile", new Class[] { ZipOutputStream.class, String.class, File.class }, new Object[] { zipOutputStream, "", new File(sourceFilename) }); assertCounters(instance, 0, 1, 0, 1); assertDestFile(destFile, true); } finally { if (zipOutputStream != null) { zipOutputStream.flush(); zipOutputStream.close(); } if (outputStream != null) { outputStream.flush(); outputStream.close(); } } List<String> expContent = new ArrayList<String>(); expContent.add("multilog.txt"); assertDestFileContent(destFile, expContent); }
From source file:com.funambol.framework.tools.FileArchiverTest.java
public void testAddDirectory_WithFilter() throws Throwable { String sourceDirname = MULTILEVELTREE + File.separator + "first"; String destFilename = BASE_TARGET_DIR + "addedDir.zip"; File destFile = new File(destFilename); if (destFile.exists()) { assertTrue("Unable to delete destination file", destFile.delete()); }// w w w . j a v a 2 s .co m // recursively = true, includeRoot = true FileArchiver instance = new FileArchiver(sourceDirname, destFilename, true, true); // add only directories instance.setFilter(DirectoryFileFilter.DIRECTORY); FileOutputStream outputStream = null; ZipOutputStream zipOutputStream = null; try { outputStream = new FileOutputStream(destFile, false); zipOutputStream = new ZipOutputStream(outputStream); PrivateAccessor.invoke(instance, "addDirectory", new Class[] { ZipOutputStream.class, String.class, File.class }, new Object[] { zipOutputStream, "", new File(sourceDirname) }); assertCounters(instance, 2, 0, 0, 3); assertDestFile(destFile, true); } finally { if (zipOutputStream != null) { zipOutputStream.flush(); } if (outputStream != null) { outputStream.flush(); } } try { sourceDirname = ONELEVELTREE; PrivateAccessor.setField(instance, "sourceFile", new File(sourceDirname)); PrivateAccessor.invoke(instance, "addDirectory", new Class[] { ZipOutputStream.class, String.class, File.class }, new Object[] { zipOutputStream, "", new File(sourceDirname) }); assertCounters(instance, 3, 0, 0, 5); assertDestFile(destFile, true); } finally { if (zipOutputStream != null) { zipOutputStream.flush(); zipOutputStream.close(); } if (outputStream != null) { outputStream.flush(); outputStream.close(); } } List<String> expContent = new ArrayList<String>(); expContent.add("first/"); expContent.add("first/second/"); expContent.add("oneleveltree/"); assertDestFileContent(destFile, expContent); }
From source file:com.funambol.framework.tools.FileArchiverTest.java
public void testAddDirectory() throws Throwable { String sourceDirname = MULTILEVELTREE + File.separator + "first"; String destFilename = BASE_TARGET_DIR + "addedDir.zip"; File destFile = new File(destFilename); if (destFile.exists()) { assertTrue("Unable to delete destination file", destFile.delete()); }//w w w.ja va2s.c o m // recursively = true, includeRoot = true FileArchiver instance = new FileArchiver(sourceDirname, destFilename, true, true); FileOutputStream outputStream = null; ZipOutputStream zipOutputStream = null; try { outputStream = new FileOutputStream(destFile, false); zipOutputStream = new ZipOutputStream(outputStream); PrivateAccessor.invoke(instance, "addDirectory", new Class[] { ZipOutputStream.class, String.class, File.class }, new Object[] { zipOutputStream, "", new File(sourceDirname) }); assertCounters(instance, 2, 3, 0, 0); assertDestFile(destFile, true); } finally { if (zipOutputStream != null) { zipOutputStream.flush(); } if (outputStream != null) { outputStream.flush(); } } try { sourceDirname = ONELEVELTREE; PrivateAccessor.setField(instance, "sourceFile", new File(sourceDirname)); PrivateAccessor.invoke(instance, "addDirectory", new Class[] { ZipOutputStream.class, String.class, File.class }, new Object[] { zipOutputStream, "", new File(sourceDirname) }); assertCounters(instance, 3, 5, 0, 0); assertDestFile(destFile, true); } finally { if (zipOutputStream != null) { zipOutputStream.flush(); zipOutputStream.close(); } if (outputStream != null) { outputStream.flush(); outputStream.close(); } } List<String> expContent = new ArrayList<String>(); expContent.add("first/"); expContent.add("first/firstlog.zip"); expContent.add("first/second/"); expContent.add("first/second/secondlog.txt"); expContent.add("first/second/secondlog.zip"); expContent.add("oneleveltree/"); expContent.add("oneleveltree/synclog.txt"); expContent.add("oneleveltree/synclog.zip"); assertDestFileContent(destFile, expContent); }
From source file:org.opennms.upgrade.api.AbstractOnmsUpgrade.java
/** * ZIP a list of files.//w w w.ja v a 2 s .c o m * * @param zipFile the output ZIP file * @param sourceFolder the source folder * @param filesToCompress the list of files to compress * @throws OnmsUpgradeException the OpenNMS upgrade exception */ private File zipFiles(File zipFile, File sourceFolder, List<File> filesToCompress) throws OnmsUpgradeException { try { FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); for (File file : filesToCompress) { String filePath = file.getAbsolutePath(); log(" Zipping %s\n", filePath); ZipEntry ze = new ZipEntry( filePath.substring(sourceFolder.getAbsolutePath().length() + 1, filePath.length())); zos.putNextEntry(ze); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); fis.close(); } zos.flush(); zos.close(); fos.close(); return zipFile; } catch (Exception e) { throw new OnmsUpgradeException("Cannot ZIP files because " + e.getMessage(), e); } }
From source file:org.eclipse.mylyn.internal.context.core.InteractionContextExternalizer.java
private void addAdditionalInformation(final IInteractionContext context, final ZipOutputStream outputStream) throws IOException { for (final IContextContributor contributor : getContextContributor()) { SafeRunner.run(new ISafeRunnable() { public void handleException(Throwable e) { StatusHandler.log(//w w w. j av a 2 s.co m new Status(IStatus.WARNING, ContextCorePlugin.ID_PLUGIN, "Context contribution failed: " //$NON-NLS-1$ + contributor.getClass(), e)); } public void run() throws Exception { InputStream additionalContextInformation = contributor.getDataAsStream(context); if (additionalContextInformation != null) { try { String encoded = URLEncoder.encode(contributor.getIdentifier(), InteractionContextManager.CONTEXT_FILENAME_ENCODING); ZipEntry zipEntry = new ZipEntry(encoded); outputStream.putNextEntry(zipEntry); IOUtils.copy(additionalContextInformation, outputStream); outputStream.flush(); outputStream.closeEntry(); } finally { additionalContextInformation.close(); } } } }); } }
From source file:com.comcast.video.dawg.show.video.VideoSnap.java
/** * Retrieve the images with input device ids from cache and stores it in zip * output stream./*from ww w . j ava2 s .c o m*/ * * @param capturedImageIds * Ids of captures images * @param deviceMacs * Mac address of selected devices * @param response * http servelet response * @param session * http session. */ public void addImagesToZipFile(String[] capturedImageIds, String[] deviceMacs, HttpServletResponse response, HttpSession session) { UniqueIndexedCache<BufferedImage> imgCache = getClientCache(session).getImgCache(); response.setHeader("Content-Disposition", "attachment; filename=\"" + "Images_" + getCurrentDateAndTime() + ".zip\""); response.setContentType("application/zip"); ServletOutputStream serveletOutputStream = null; ZipOutputStream zipOutputStream = null; try { serveletOutputStream = response.getOutputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); zipOutputStream = new ZipOutputStream(byteArrayOutputStream); BufferedImage image; ZipEntry zipEntry; ByteArrayOutputStream imgByteArrayOutputStream = null; for (int i = 0; i < deviceMacs.length; i++) { // Check whether id of captured image is null or not if (!StringUtils.isEmpty(capturedImageIds[i])) { image = imgCache.getItem(capturedImageIds[i]); zipEntry = new ZipEntry(deviceMacs[i].toUpperCase() + "." + IMG_FORMAT); zipOutputStream.putNextEntry(zipEntry); imgByteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(image, IMG_FORMAT, imgByteArrayOutputStream); imgByteArrayOutputStream.flush(); zipOutputStream.write(imgByteArrayOutputStream.toByteArray()); zipOutputStream.closeEntry(); } } zipOutputStream.flush(); zipOutputStream.close(); serveletOutputStream.write(byteArrayOutputStream.toByteArray()); } catch (IOException ioe) { LOGGER.error("Image zipping failed !!!", ioe); } finally { IOUtils.closeQuietly(zipOutputStream); } }