Example usage for java.util.zip ZipOutputStream flush

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

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the compressed output stream.

Usage

From source file:com.jbrisbin.vpc.jobsched.batch.BatchMessageConverter.java

public Message toMessage(Object object, MessageProperties props) throws MessageConversionException {
    if (object instanceof BatchMessage) {
        BatchMessage batch = (BatchMessage) object;
        props.setCorrelationId(batch.getId().getBytes());
        props.setContentType("application/zip");

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ZipOutputStream zout = new ZipOutputStream(bout);
        for (Map.Entry<String, String> msg : batch.getMessages().entrySet()) {
            ZipEntry zentry = new ZipEntry(msg.getKey());
            try {
                zout.putNextEntry(zentry);
                zout.write(msg.getValue().getBytes());
                zout.closeEntry();//from   ww  w  .  j a v a  2s .co  m
            } catch (IOException e) {
                throw new MessageConversionException(e.getMessage(), e);
            }
        }

        try {
            zout.flush();
            zout.close();
        } catch (IOException e) {
            throw new MessageConversionException(e.getMessage(), e);
        }

        return new Message(bout.toByteArray(), props);
    } else {
        throw new MessageConversionException(
                "Cannot convert object " + String.valueOf(object) + " using " + getClass().toString());
    }
}

From source file:gov.nih.nci.caarray.application.fileaccess.FileAccessUtils.java

/**
 * Adds the given file to the given zip output stream using the given name as the zip entry name. This method will
 * NOT call finish on the zip output stream at the end.
 * //  w ww .  j  a  va2 s  .  com
 * @param zos the zip output stream to add the file to. This stream must already be open.
 * @param file the file to put in the zip.
 * @param name the name to use for this zip entry.
 * @param addAsStored if true, then the file will be added to the zip as a STORED entry (e.g. without applying
 *            compression to it); if false, then the file will be added to the zip as a DEFLATED entry.
 * @throws IOException if there is an error writing to the stream
 */
public void writeZipEntry(ZipOutputStream zos, File file, String name, boolean addAsStored) throws IOException {
    final ZipEntry ze = new ZipEntry(name);
    ze.setMethod(addAsStored ? ZipEntry.STORED : ZipEntry.DEFLATED);
    if (addAsStored) {
        ze.setSize(file.length());
        ze.setCrc(FileUtils.checksumCRC32(file));
    }
    zos.putNextEntry(ze);
    final InputStream is = FileUtils.openInputStream(file);
    IOUtils.copy(is, zos);
    zos.closeEntry();
    zos.flush();
    IOUtils.closeQuietly(is);
}

From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java

public String toBase64() {
    try {// w  ww. j a  v  a2s .  co  m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ZipOutputStream zip = new ZipOutputStream(bos);
            try {
                zip.setLevel(9);
                zip.putNextEntry(new ZipEntry("entry"));
                try {
                    ObjectOutput out = new ObjectOutputStream(zip);
                    try {
                        out.writeObject(this);
                    } finally {
                        out.flush();
                    }
                } finally {
                    zip.closeEntry();
                }
                zip.flush();
                return Base64.encodeBase64URLSafeString(bos.toByteArray());
            } finally {
                zip.close();
            }
        } finally {
            bos.close();
        }
    } catch (IOException e) {
        log.log((Level.SEVERE), "IO Error while serializing member", e);
        return null;
    }
}

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 w w  w . java  2  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.boundlessgeo.geoserver.bundle.BundleExporter.java

/**
 * Packages up the export as a zip file.
 *
 * @return The path pointing to the zip file.
 *///from ww w.  j a v  a 2s.co 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: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();
    myOut.close();/*from  w ww.  j  a  v  a  2s . c om*/
}

From source file:org.geoserver.util.IOUtils.java

/**
 * See {@link #zipDirectory(File, ZipOutputStream, FilenameFilter)}, this version handles the prefix needed
 * to recursively zip data preserving the relative path of each
 *///from w  w w . j  av  a 2s.  c om
private static void zipDirectory(File directory, String prefix, ZipOutputStream zipout,
        final FilenameFilter filter) throws IOException, FileNotFoundException {
    File[] files = directory.listFiles(filter);
    // copy file by reading 4k at a time (faster than buffered reading)
    byte[] buffer = new byte[4 * 1024];
    for (File file : files) {
        if (file.exists()) {
            if (file.isDirectory()) {
                // recurse and append
                String newPrefix = prefix + file.getName() + "/";
                zipout.putNextEntry(new ZipEntry(newPrefix));
                zipDirectory(file, newPrefix, zipout, filter);
            } else {
                ZipEntry entry = new ZipEntry(prefix + file.getName());
                zipout.putNextEntry(entry);

                InputStream in = new FileInputStream(file);
                int c;
                try {
                    while (-1 != (c = in.read(buffer))) {
                        zipout.write(buffer, 0, c);
                    }
                    zipout.closeEntry();
                } finally {
                    in.close();
                }
            }
        }
    }
    zipout.flush();
}

From source file:org.theospi.portfolio.help.HelpManagerImpl.java

public void packageGlossaryForExport(Id worksiteId, OutputStream os) throws IOException {
    getAuthzManager().checkPermission(HelpFunctionConstants.EXPORT_TERMS, getToolId());

    CheckedOutputStream checksum = new CheckedOutputStream(os, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));

    putWorksiteTermsIntoZip(worksiteId, zos);

    zos.finish();//w ww .jav a 2 s. com
    zos.flush();
}

From source file:org.agnitas.util.ZipUtilities.java

/**
 * Open an existing Zip file for adding new entries.
 * All existing entries in the zipped file will be copied in the new one.
 * //w w  w . ja  v a 2s .c om
 * @param zipFile
 * @return
 * @throws IOException 
 * @throws ZipException 
 */
public static ZipOutputStream openExistingZipFileForExtension(File zipFile) throws IOException {
    // Rename source Zip file (Attention: the String path and name of the zipFile are preserved
    File originalFileTemp = new File(
            zipFile.getParentFile().getAbsolutePath() + "/" + String.valueOf(System.currentTimeMillis()));
    zipFile.renameTo(originalFileTemp);

    ZipFile sourceZipFile = new ZipFile(originalFileTemp);
    ZipOutputStream zipOutputStream = new ZipOutputStream(
            new BufferedOutputStream(new FileOutputStream(zipFile)));

    BufferedInputStream bufferedInputStream = null;

    try {
        // copy entries
        Enumeration<? extends ZipEntry> srcEntries = sourceZipFile.entries();
        while (srcEntries.hasMoreElements()) {
            ZipEntry sourceZipFileEntry = srcEntries.nextElement();
            zipOutputStream.putNextEntry(sourceZipFileEntry);

            bufferedInputStream = new BufferedInputStream(sourceZipFile.getInputStream(sourceZipFileEntry));

            byte[] bufferArray = new byte[1024];
            int byteBufferFillLength = bufferedInputStream.read(bufferArray);
            while (byteBufferFillLength > -1) {
                zipOutputStream.write(bufferArray, 0, byteBufferFillLength);
                byteBufferFillLength = bufferedInputStream.read(bufferArray);
            }

            zipOutputStream.closeEntry();

            bufferedInputStream.close();
            bufferedInputStream = null;
        }

        zipOutputStream.flush();
        sourceZipFile.close();
        originalFileTemp.delete();

        return zipOutputStream;
    } catch (IOException e) {
        // delete existing Zip file
        if (zipFile.exists()) {
            if (zipOutputStream != null) {
                try {
                    zipOutputStream.close();
                } catch (Exception ex) {
                }
                zipOutputStream = null;
            }
            zipFile.delete();
        }

        // revert renaming of source Zip file
        originalFileTemp.renameTo(zipFile);
        throw e;
    } finally {
        if (bufferedInputStream != null) {
            try {
                bufferedInputStream.close();
            } catch (Exception e) {
            }
            bufferedInputStream = null;
        }
    }
}

From source file:org.fao.geonet.monitor.service.LogConfig.java

/**
 * Download the log file in a ZIP.//from   www  .  ja  v  a  2  s .co  m
 */
@RequestMapping(value = "/{lang}/log/file", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
@ResponseBody
public void getLog(HttpServletResponse response) throws IOException {
    if (isAppenderLogFileLoaded()) {
        File file = new File(fileAppender.getFile());

        // create ZIP FILE

        String fname = String.valueOf(Calendar.getInstance().getTimeInMillis());

        // set headers for the response
        response.setContentType("application/zip");
        response.setContentLength((int) file.length());
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", "export-log-" + fname + ".zip");
        response.setHeader(headerKey, headerValue);

        int read = 0;
        byte[] bytes = new byte[1024];
        ZipOutputStream zos = null;
        ZipEntry ze;
        InputStream in = null;
        try {
            zos = new ZipOutputStream(response.getOutputStream());
            ze = new ZipEntry(file.getName());
            zos.putNextEntry(ze);
            in = new FileInputStream(file);
            while ((read = in.read(bytes)) != -1) {
                zos.write(bytes, 0, read);
            }
        } finally {
            IOUtils.closeQuietly(in);
            if (zos != null)
                zos.flush();
            IOUtils.closeQuietly(zos);
        }
    } else {
        throw new RuntimeException("No log file found for download. Check logger configuration.");
    }
}