List of usage examples for java.util.zip ZipOutputStream flush
public void flush() throws IOException
From source file:com.flexive.core.storage.GenericDivisionExporter.java
/** * Signal the end of a zip entry and flush the stream * * @param out zip output stream// w ww. jav a2 s . c om * @throws IOException on errors */ private void endEntry(ZipOutputStream out) throws IOException { out.closeEntry(); out.flush(); }
From source file:net.solarnetwork.node.backup.DefaultBackupManager.java
@Override public void exportBackupArchive(String backupKey, OutputStream out) throws IOException { final BackupService service = activeBackupService(); if (service == null) { return;/* ww w . ja va2 s . c om*/ } final Backup backup = service.backupForKey(backupKey); if (backup == null) { return; } // create the zip archive for the backup files ZipOutputStream zos = new ZipOutputStream(out); try { BackupResourceIterable resources = service.getBackupResources(backup); for (BackupResource r : resources) { zos.putNextEntry(new ZipEntry(r.getBackupPath())); FileCopyUtils.copy(r.getInputStream(), new FilterOutputStream(zos) { @Override public void close() throws IOException { // FileCopyUtils closed the stream, which we don't want here } }); } resources.close(); } finally { zos.flush(); zos.finish(); zos.close(); } }
From source file:cascading.tap.hadoop.ZipInputFormatTest.java
public void testSplits() throws Exception { JobConf job = new JobConf(); FileSystem currentFs = FileSystem.get(job); Path file = new Path(workDir, "test.zip"); Reporter reporter = Reporter.NULL;/*from w w w .j a va 2s .c om*/ int seed = new Random().nextInt(); LOG.info("seed = " + seed); Random random = new Random(seed); FileInputFormat.setInputPaths(job, file); for (int entries = 1; entries < MAX_ENTRIES; entries += random.nextInt(MAX_ENTRIES / 10) + 1) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream); long length = 0; LOG.debug("creating; zip file with entries = " + entries); // for each entry in the zip file for (int entryCounter = 0; entryCounter < entries; entryCounter++) { // construct zip entries splitting MAX_LENGTH between entries long entryLength = MAX_LENGTH / entries; ZipEntry zipEntry = new ZipEntry("/entry" + entryCounter + ".txt"); zipEntry.setMethod(ZipEntry.DEFLATED); zos.putNextEntry(zipEntry); for (length = entryCounter * entryLength; length < (entryCounter + 1) * entryLength; length++) { zos.write(Long.toString(length).getBytes()); zos.write("\n".getBytes()); } zos.flush(); zos.closeEntry(); } zos.flush(); zos.close(); currentFs.delete(file, true); OutputStream outputStream = currentFs.create(file); byteArrayOutputStream.writeTo(outputStream); outputStream.close(); ZipInputFormat format = new ZipInputFormat(); format.configure(job); LongWritable key = new LongWritable(); Text value = new Text(); InputSplit[] splits = format.getSplits(job, 100); BitSet bits = new BitSet((int) length); for (int j = 0; j < splits.length; j++) { LOG.debug("split[" + j + "]= " + splits[j]); RecordReader<LongWritable, Text> reader = format.getRecordReader(splits[j], job, reporter); try { int count = 0; while (reader.next(key, value)) { int v = Integer.parseInt(value.toString()); LOG.debug("read " + v); if (bits.get(v)) LOG.warn("conflict with " + v + " in split " + j + " at position " + reader.getPos()); assertFalse("key in multiple partitions.", bits.get(v)); bits.set(v); count++; } LOG.debug("splits[" + j + "]=" + splits[j] + " count=" + count); } finally { reader.close(); } } assertEquals("some keys in no partition.", length, bits.cardinality()); } }
From source file:com.webpagebytes.cms.controllers.FlatStorageImporterExporter.java
public void exportToZip(OutputStream os) throws WPBIOException { ZipOutputStream zos = new ZipOutputStream(os); exportUris(zos, PATH_URIS);/* w ww . j a va 2 s. c om*/ exportSitePages(zos, PATH_SITE_PAGES); exportPageModules(zos, PATH_SITE_PAGES_MODULES); exportMessages(zos, PATH_MESSAGES); exportFiles(zos, PATH_FILES); exportArticles(zos, PATH_ARTICLES); exportGlobals(zos, PATH_GLOBALS); exportLocales(zos, PATH_LOCALES); try { zos.flush(); zos.close(); } catch (IOException e) { log.log(Level.SEVERE, e.getMessage(), e); throw new WPBIOException("Cannot export project, error flushing/closing stream", e); } }
From source file:com.yunmel.syncretic.utils.io.IOUtils.java
/** * ?//from ww w . ja va2 s . co m * * @param files * @param out ? * @throws IOException * @throws Exception */ public static void zipDownLoad(Map<File, String> downQuene, HttpServletResponse response) throws IOException { ServletOutputStream out = response.getOutputStream(); ZipOutputStream zipout = new ZipOutputStream(out); ZipEntry entry = null; zipout.setLevel(1); // zipout.setEncoding("GBK"); if (downQuene != null && downQuene.size() > 0) { for (Entry<File, String> fileInfo : downQuene.entrySet()) { File file = fileInfo.getKey(); try { String filename = new String(fileInfo.getValue().getBytes(), "GBK"); entry = new ZipEntry(filename); entry.setSize(file.length()); zipout.putNextEntry(entry); } catch (IOException e) { // Logger.getLogger(FileUtil.class).warn(":", e); } BufferedInputStream fr = new BufferedInputStream(new FileInputStream(fileInfo.getKey())); int len; byte[] buffer = new byte[1024]; while ((len = fr.read(buffer)) != -1) zipout.write(buffer, 0, len); fr.close(); } } zipout.finish(); zipout.flush(); // out.flush(); }
From source file:org.codice.alliance.nsili.endpoint.requests.OrderRequestImpl.java
private void getZip(ZipOutputStream zipOut, List<ResourceContainer> files) throws IOException { List<String> addedFiles = new ArrayList<>(); for (ResourceContainer file : files) { if (!addedFiles.contains(file.getName())) { ZipEntry zipEntry = new ZipEntry(file.getName()); zipOut.putNextEntry(zipEntry); IOUtils.copy(file.getInputStream(), zipOut); addedFiles.add(file.getName()); }/*from ww w .j ava 2 s . c o m*/ } zipOut.flush(); }
From source file:gov.nih.nci.cabig.caaers.web.ae.AdditionalInformationDocumentZipDownloadController.java
@Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException { Integer additionalInformationId = ServletRequestUtils.getRequiredIntParameter(request, "additionalInformationId"); List<AdditionalInformationDocument> additionalInformationDocuments = additionalInformationDocumentService .findByAdditionalInformationId(additionalInformationId); File tempFile = null;//from w w w .ja v a2s . c o m ZipOutputStream zos = null; FileOutputStream fos = null; List<String> zipEntriesName = new ArrayList<String>(); try { tempFile = File.createTempFile( "additionalInformationFile" + System.currentTimeMillis() + RandomUtils.nextInt(1000), ".zip"); fos = new FileOutputStream(tempFile); zos = new ZipOutputStream(fos); for (AdditionalInformationDocument additionalInformationDocument : additionalInformationDocuments) { String name = getUniqueZipEntryName(additionalInformationDocument, zipEntriesName); zipEntriesName.add(name); ZipEntry zipEntry = new ZipEntry(name); zos.putNextEntry(zipEntry); IOUtils.copy(new FileInputStream(additionalInformationDocument.getFile()), zos); zos.closeEntry(); } zos.flush(); } catch (Exception e) { log.error("Unable to create temp file", e); return null; } finally { if (zos != null) IOUtils.closeQuietly(zos); if (fos != null) IOUtils.closeQuietly(fos); } if (tempFile != null) { FileInputStream fis = null; OutputStream out = null; try { fis = new FileInputStream(tempFile); out = response.getOutputStream(); response.setContentType("application/x-download"); response.setHeader("Content-Disposition", "attachment; filename=" + additionalInformationId + ".zip"); response.setHeader("Content-length", String.valueOf(tempFile.length())); response.setHeader("Pragma", "private"); response.setHeader("Cache-control", "private, must-revalidate"); IOUtils.copy(fis, out); out.flush(); } catch (Exception e) { log.error("Error while reading zip file ", e); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(out); } FileUtils.deleteQuietly(tempFile); } return null; }
From source file:edu.umd.cs.submitServer.servlets.UploadSubmission.java
@Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { long now = System.currentTimeMillis(); Timestamp submissionTimestamp = new Timestamp(now); // these are set by filters or previous servlets Project project = (Project) request.getAttribute(PROJECT); StudentRegistration studentRegistration = (StudentRegistration) request.getAttribute(STUDENT_REGISTRATION); MultipartRequest multipartRequest = (MultipartRequest) request.getAttribute(MULTIPART_REQUEST); boolean webBasedUpload = ((Boolean) request.getAttribute("webBasedUpload")).booleanValue(); String clientTool = multipartRequest.getCheckedParameter("submitClientTool"); String clientVersion = multipartRequest.getOptionalCheckedParameter("submitClientVersion"); String cvsTimestamp = multipartRequest.getOptionalCheckedParameter("cvstagTimestamp"); Collection<FileItem> files = multipartRequest.getFileItems(); Kind kind;// www.ja v a 2 s . c o m byte[] zipOutput = null; // zipped version of bytesForUpload boolean fixedZip = false; try { if (files.size() > 1) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(bos); for (FileItem item : files) { String name = item.getName(); if (name == null || name.length() == 0) continue; byte[] bytes = item.get(); ZipEntry zentry = new ZipEntry(name); zentry.setSize(bytes.length); zentry.setTime(now); zos.putNextEntry(zentry); zos.write(bytes); zos.closeEntry(); } zos.flush(); zos.close(); zipOutput = bos.toByteArray(); kind = Kind.MULTIFILE_UPLOAD; } else { FileItem fileItem = multipartRequest.getFileItem(); if (fileItem == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "There was a problem processing your submission. " + "No files were found in your submission"); return; } // get size in bytes long sizeInBytes = fileItem.getSize(); if (sizeInBytes == 0 || sizeInBytes > Integer.MAX_VALUE) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Trying upload file of size " + sizeInBytes); return; } // copy the fileItem into a byte array byte[] bytesForUpload = fileItem.get(); String fileName = fileItem.getName(); boolean isSpecialSingleFile = OfficeFileName.matcher(fileName).matches(); FormatDescription desc = FormatIdentification.identify(bytesForUpload); if (!isSpecialSingleFile && desc != null && desc.getMimeType().equals("application/zip")) { fixedZip = FixZip.hasProblem(bytesForUpload); kind = Kind.ZIP_UPLOAD; if (fixedZip) { bytesForUpload = FixZip.fixProblem(bytesForUpload, studentRegistration.getStudentRegistrationPK()); kind = Kind.FIXED_ZIP_UPLOAD; } zipOutput = bytesForUpload; } else { // ========================================================================================== // [NAT] [Buffer to ZIP Part] // Check the type of the upload and convert to zip format if // possible // NOTE: I use both MagicMatch and FormatDescription (above) // because MagicMatch was having // some trouble identifying all zips String mime = URLConnection.getFileNameMap().getContentTypeFor(fileName); if (!isSpecialSingleFile && mime == null) try { MagicMatch match = Magic.getMagicMatch(bytesForUpload, true); if (match != null) mime = match.getMimeType(); } catch (Exception e) { // leave mime as null } if (!isSpecialSingleFile && "application/zip".equalsIgnoreCase(mime)) { zipOutput = bytesForUpload; kind = Kind.ZIP_UPLOAD2; } else { InputStream ins = new ByteArrayInputStream(bytesForUpload); if ("application/x-gzip".equalsIgnoreCase(mime)) { ins = new GZIPInputStream(ins); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(bos); if (!isSpecialSingleFile && ("application/x-gzip".equalsIgnoreCase(mime) || "application/x-tar".equalsIgnoreCase(mime))) { kind = Kind.TAR_UPLOAD; TarInputStream tins = new TarInputStream(ins); TarEntry tarEntry = null; while ((tarEntry = tins.getNextEntry()) != null) { zos.putNextEntry(new ZipEntry(tarEntry.getName())); tins.copyEntryContents(zos); zos.closeEntry(); } tins.close(); } else { // Non-archive file type if (isSpecialSingleFile) kind = Kind.SPECIAL_ZIP_FILE; else kind = Kind.SINGLE_FILE; // Write bytes to a zip file ZipEntry zentry = new ZipEntry(fileName); zos.putNextEntry(zentry); zos.write(bytesForUpload); zos.closeEntry(); } zos.flush(); zos.close(); zipOutput = bos.toByteArray(); } // [END Buffer to ZIP Part] // ========================================================================================== } } } catch (NullPointerException e) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "There was a problem processing your submission. " + "You should submit files that are either zipped or jarred"); return; } finally { for (FileItem fItem : files) fItem.delete(); } if (webBasedUpload) { clientTool = "web"; clientVersion = kind.toString(); } Submission submission = uploadSubmission(project, studentRegistration, zipOutput, request, submissionTimestamp, clientTool, clientVersion, cvsTimestamp, getDatabaseProps(), getSubmitServerServletLog()); request.setAttribute("submission", submission); if (!webBasedUpload) { response.setContentType("text/plain"); PrintWriter out = response.getWriter(); out.println("Successful submission #" + submission.getSubmissionNumber() + " received for project " + project.getProjectNumber()); out.flush(); out.close(); return; } boolean instructorUpload = ((Boolean) request.getAttribute("instructorViewOfStudent")).booleanValue(); // boolean // isCanonicalSubmission="true".equals(request.getParameter("isCanonicalSubmission")); // set the successful submission as a request attribute String redirectUrl; if (fixedZip) { redirectUrl = request.getContextPath() + "/view/fixedSubmissionUpload.jsp?submissionPK=" + submission.getSubmissionPK(); } if (project.getCanonicalStudentRegistrationPK() == studentRegistration.getStudentRegistrationPK()) { redirectUrl = request.getContextPath() + "/view/instructor/projectUtilities.jsp?projectPK=" + project.getProjectPK(); } else if (instructorUpload) { redirectUrl = request.getContextPath() + "/view/instructor/project.jsp?projectPK=" + project.getProjectPK(); } else { redirectUrl = request.getContextPath() + "/view/project.jsp?projectPK=" + project.getProjectPK(); } response.sendRedirect(redirectUrl); }
From source file:edu.dfci.cccb.mev.dataset.rest.controllers.WorkspaceController.java
@RequestMapping(value = "/export/zip", method = POST, consumes = "multipart/form-data") @ResponseStatus(OK)//from w ww . ja va2s .c o m public byte[] export(@RequestParam("name") String name, @RequestParam("rows") List<String> rows, @RequestParam("columns") List<String> columns, @RequestParam("rowSelections") String jsonRowSelections, @RequestParam("columnSelections") String jsonColumnSelections, @RequestParam("analyses") String[] analyses, HttpServletResponse response) throws DatasetException, IOException { log.info(String.format("Offline %s, %s, %s, %s", name, rows, columns, analyses)); //creating byteArray stream, make it bufforable and passing this buffor to ZipOutputStream ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream); ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream); //nw zip entry for dataset Dataset dataset = workspace.get(name); zipOutputStream.putNextEntry(new ZipEntry("dataset.json")); IOUtils.copy(new ByteArrayInputStream(mapper.writeValueAsBytes(dataset)), zipOutputStream); zipOutputStream.closeEntry(); zipAnnotations(name, "row", zipOutputStream); zipAnnotations(name, "column", zipOutputStream); if (dataset.values() instanceof IFlatFileValues) { IFlatFileValues values = (IFlatFileValues) dataset.values(); zipOutputStream.putNextEntry(new ZipEntry("values.bin")); InputStream valuesIs = values.asInputStream(); IOUtils.copy(valuesIs, zipOutputStream); zipOutputStream.closeEntry(); zipOutputStream.flush(); valuesIs.close(); } //new zip entry and copying inputstream with file to zipOutputStream, after all closing streams int i = 0; for (String analysis : analyses) { InputStream analysisOs = new ByteArrayInputStream(analysis.getBytes(StandardCharsets.UTF_8)); zipOutputStream.putNextEntry(new ZipEntry(String.format("analysis_%d.json", i))); JsonNode analysisJson = mapper.readTree(analysis); IOUtils.copy(analysisOs, zipOutputStream); zipOutputStream.closeEntry(); zipOutputStream.flush(); analysisOs.close(); i++; } zipOutputStream.flush(); zipOutputStream.close(); byte[] ret = byteArrayOutputStream.toByteArray(); IOUtils.closeQuietly(bufferedOutputStream); IOUtils.closeQuietly(byteArrayOutputStream); response.setContentLength(ret.length); response.setContentType("application/zip"); //or something more generic... response.setHeader("Accept-Ranges", "bytes"); response.setStatus(HttpServletResponse.SC_OK); response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s.zip\"", name)); return ret; }
From source file:au.com.gaiaresources.bdrs.controller.theme.ThemeControllerTest.java
private byte[] createZip(Map<String, byte[]> files) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zipfile = new ZipOutputStream(bos); ZipEntry zipentry;/*ww w .ja v a 2 s.co m*/ for (Map.Entry<String, byte[]> entry : files.entrySet()) { zipentry = new ZipEntry(entry.getKey()); zipfile.putNextEntry(zipentry); zipfile.write(entry.getValue()); } zipfile.flush(); zipfile.close(); byte[] data = bos.toByteArray(); bos.close(); return data; }