Example usage for java.util.zip ZipOutputStream close

List of usage examples for java.util.zip ZipOutputStream close

Introduction

In this page you can find the example usage for java.util.zip ZipOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP output stream as well as the stream being filtered.

Usage

From source file:csns.web.controller.DownloadController.java

@RequestMapping(value = "/download", params = "sectionId")
public String downloadSectionFiles(@RequestParam Long sectionId, HttpServletResponse response, ModelMap models)
        throws IOException {
    Section section = sectionDao.getSection(sectionId);
    String name = section.getCourse().getCode();
    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment; filename=" + name + ".zip");

    User user = SecurityUtils.getUser();
    ZipOutputStream zip = new ZipOutputStream(response.getOutputStream());
    for (Assignment assignment : section.getAssignments()) {
        if (assignment.isPastDue() && !assignment.isAvailableAfterDueDate())
            continue;

        String dir = assignment.getAlias();
        Submission submission = submissionDao.getSubmission(user, assignment);
        if (submission != null)
            addToZip(zip, dir, submission.getFiles());
    }/*ww  w. j  ava2  s  . c  o m*/
    zip.close();
    return null;
}

From source file:be.fedict.eid.applet.service.signer.ooxml.AbstractOOXMLSignatureService.java

private void outputSignedOfficeOpenXMLDocument(byte[] signatureData)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    LOG.debug("output signed Office OpenXML document");
    OutputStream signedOOXMLOutputStream = getSignedOfficeOpenXMLDocumentOutputStream();
    if (null == signedOOXMLOutputStream) {
        throw new NullPointerException("signedOOXMLOutputStream is null");
    }/* w w w  .  j  av a2 s .  com*/

    String signatureZipEntryName = "_xmlsignatures/sig-" + UUID.randomUUID().toString() + ".xml";
    LOG.debug("signature ZIP entry name: " + signatureZipEntryName);
    /*
     * Copy the original OOXML content to the signed OOXML package. During
     * copying some files need to changed.
     */
    ZipOutputStream zipOutputStream = copyOOXMLContent(signatureZipEntryName, signedOOXMLOutputStream);

    /*
     * Add the OOXML XML signature file to the OOXML package.
     */
    ZipEntry zipEntry = new ZipEntry(signatureZipEntryName);
    zipOutputStream.putNextEntry(zipEntry);
    IOUtils.write(signatureData, zipOutputStream);
    zipOutputStream.close();
}

From source file:fr.ippon.wip.portlet.WIPConfigurationPortlet.java

@Override
public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException {
    String configName = request.getParameter(Attributes.ACTION_EXPORT_CONFIGURATION.name());
    if (StringUtils.isEmpty(configName))
        return;//from   ww w. ja  va2  s . c  o m

    response.setContentType("multipart/x-zip");
    response.setProperty("Content-Disposition", "attachment; filename=" + configName + ".zip");

    WIPConfiguration configuration = configurationDAO.read(configName);
    ZipConfiguration zip = new ZipConfiguration();
    ZipOutputStream out = new ZipOutputStream(response.getPortletOutputStream());
    zip.zip(configuration, out);
    out.flush();
    out.close();
}

From source file:com.jaspersoft.jasperserver.api.engine.scheduling.quartz.ReportExecutionJobMailNotificationImpl.java

protected void attachOutput(ReportExecutionJob job, MimeMessageHelper messageHelper, ReportOutput output,
        boolean useZipFormat) throws MessagingException, JobExecutionException {
    String attachmentName;//from   w ww.  j a  v a 2 s. c  o m
    DataContainer attachmentData;
    if (output.getChildren().isEmpty()) {
        attachmentName = output.getFilename();
        attachmentData = output.getData();
    } else if (useZipFormat) { // use zip format
        attachmentData = job.createDataContainer();
        boolean close = true;
        ZipOutputStream zipOut = new ZipOutputStream(attachmentData.getOutputStream());
        try {
            zipOutput(job, output, zipOut);
            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";
    } else { // NO ZIP FORMAT
        attachmentName = output.getFilename();
        try {
            attachmentName = MimeUtility.encodeWord(attachmentName, job.getCharacterEncoding(), null);
        } catch (UnsupportedEncodingException e) {
            throw new JSExceptionWrapper(e);
        }
        StringBuffer primaryPage = null;
        for (Iterator it = output.getChildren().iterator(); it.hasNext();) {
            ReportOutput child = (ReportOutput) it.next();
            String childName = child.getFilename();

            // NOTE:  add the ".dat" extension to all image resources
            // email client will automatically append ".dat" extension to all the files with no extension
            // should do it in JasperReport side
            if (output.getFileType().equals(ContentResource.TYPE_HTML)) {
                if (primaryPage == null)
                    primaryPage = new StringBuffer(new String(output.getData().getData()));
                int fromIndex = 0;
                while ((fromIndex = primaryPage.indexOf("src=\"" + childName + "\"", fromIndex)) > 0) {
                    primaryPage.insert(fromIndex + 5 + childName.length(), ".dat");
                }
                childName = childName + ".dat";
            }

            try {
                childName = MimeUtility.encodeWord(childName, job.getCharacterEncoding(), null);
            } catch (UnsupportedEncodingException e) {
                throw new JSExceptionWrapper(e);
            }
            messageHelper.addAttachment(childName, new DataContainerResource(child.getData()));
        }
        if (primaryPage == null) {
            messageHelper.addAttachment(attachmentName, new DataContainerResource(output.getData()));
        } else {
            messageHelper.addAttachment(attachmentName,
                    new DataContainerResource(new MemoryDataContainer(primaryPage.toString().getBytes())));
        }
        return;
    }
    try {
        attachmentName = MimeUtility.encodeWord(attachmentName, job.getCharacterEncoding(), null);
    } catch (UnsupportedEncodingException e) {
        throw new JSExceptionWrapper(e);
    }
    messageHelper.addAttachment(attachmentName, new DataContainerResource(attachmentData));
}

From source file:es.urjc.mctwp.bbeans.research.image.DownloadBean.java

public String accDownloadImage() {

    if (imageId != null && !imageId.isEmpty()) {
        Trial trial = getSession().getTrial();

        try {/*from   ww w  . j av  a 2s.  c om*/
            if (trial != null) {
                ZipOutputStream zos = prepareZOS(imageId);

                //Get Image
                LoadImage cmd = (LoadImage) getCommand(LoadImage.class);
                cmd.setCollection(trial.getCollection());
                cmd.setImageId(imageId);
                cmd = (LoadImage) runCommand(cmd);
                Image img = cmd.getResult();

                addImageToZos(zos, img, null);
                zos.close();

                completeResponse();
            }
        } catch (Exception e) {
            setErrorMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    return null;
}

From source file:com.music.scheduled.BackupJob.java

private void zipBackup(String baseFileName) throws IOException {
    FileOutputStream fos = new FileOutputStream(baseFileName + ".zip");
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));

    File entryFile = new File(baseFileName + ".sql");
    FileInputStream fi = new FileInputStream(entryFile);
    InputStream origin = new BufferedInputStream(fi, ZIP_BUFFER);
    ZipEntry entry = new ZipEntry("data.sql");
    zos.putNextEntry(entry);//from   ww w  .  j  a  v a2s . c om
    int count;
    byte[] data = new byte[ZIP_BUFFER];
    while ((count = origin.read(data, 0, ZIP_BUFFER)) != -1) {
        zos.write(data, 0, count);
    }
    origin.close();
    zos.close();

    entryFile.delete();
}

From source file:nl.edia.sakai.tool.skinmanager.impl.SkinFileSystemServiceImpl.java

@Override
public void writeSkinData(String name, OutputStream out) throws SkinException, IOException {
    File mySkinDir = getSkinDir(name);
    ZipOutputStream myOut = new ZipOutputStream(out);
    writeSkinZipEntries(mySkinDir, myOut);
    myOut.flush();//from w  w w. j a  v  a  2  s.c  om
    myOut.close();
}

From source file:com.boundlessgeo.geoserver.bundle.BundleExporter.java

/**
 * Packages up the export as a zip file.
 *
 * @return The path pointing to the zip file.
 *//*  w ww  .j ava2s  . c o m*/
public Path zip() throws IOException {
    Path zip = temp.resolve(options.name() + ".zip");
    try (OutputStream out = new BufferedOutputStream(new FileOutputStream(zip.toFile()));) {
        ZipOutputStream zout = new ZipOutputStream(out);
        try {
            IOUtils.zipDirectory(root().toFile(), zout, null);
        } finally {
            zout.flush();
            zout.close();
            out.flush();
        }
    }
    return zip;
}

From source file:it.eng.spagobi.tools.scheduler.dispatcher.UniqueMailDocumentDispatchChannel.java

public static MimeBodyPart zipAttachment(String zipFileName, Map mailOptions, File tempFolder) {
    logger.debug("IN");
    MimeBodyPart messageBodyPart = null;
    try {//from   ww w . ja  va2 s .  c  o  m

        String nameSuffix = mailOptions.get(NAME_SUFFIX) != null ? (String) mailOptions.get(NAME_SUFFIX) : "";

        byte[] buffer = new byte[4096]; // Create a buffer for copying
        int bytesRead;

        // the zip
        String tempFolderPath = (String) mailOptions.get(TEMP_FOLDER_PATH);

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ZipOutputStream out = new ZipOutputStream(bout);

        logger.debug("File zip to write: " + tempFolderPath + File.separator + "zippedFile.zip");

        //files to zip
        String[] entries = tempFolder.list();

        for (int i = 0; i < entries.length; i++) {
            //File f = new File(tempFolder, entries[i]);
            File f = new File(tempFolder + File.separator + entries[i]);
            if (f.isDirectory())
                continue;//Ignore directory
            logger.debug("insert file: " + f.getName());
            FileInputStream in = new FileInputStream(f); // Stream to read file
            ZipEntry entry = new ZipEntry(f.getName()); // Make a ZipEntry
            out.putNextEntry(entry); // Store entry
            while ((bytesRead = in.read(buffer)) != -1)
                out.write(buffer, 0, bytesRead);
            in.close();
        }
        out.close();

        messageBodyPart = new MimeBodyPart();
        DataSource source = new ByteArrayDataSource(bout.toByteArray(), "application/zip");
        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName(zipFileName + nameSuffix + ".zip");

    } catch (Exception e) {
        logger.error("Error while creating the zip", e);
        return null;
    }

    logger.debug("OUT");

    return messageBodyPart;
}

From source file:edu.mit.lib.bagit.Filler.java

private void deflate(OutputStream out, String format) throws IOException {
    switch (format) {
    case "zip":
        ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(out));
        fillZip(base, base.getName(), zout);
        zout.close();
        break;/*ww  w. j  a v a  2s. c  o m*/
    case "tgz":
        TarArchiveOutputStream tout = new TarArchiveOutputStream(
                new BufferedOutputStream(new GzipCompressorOutputStream(out)));
        fillArchive(base, base.getName(), tout);
        tout.close();
        break;
    default:
        throw new IOException("Unsupported package format: " + format);
    }
}