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:net.firejack.platform.core.utils.ArchiveUtils.java

public static void zip(OutputStream outputStream, Map<String, InputStream> filePaths) throws IOException {
    ZipOutputStream out = new ZipOutputStream(outputStream);

    for (Map.Entry<String, InputStream> file : filePaths.entrySet()) {
        String filename = file.getKey();
        InputStream stream = file.getValue();
        if (stream != null) {
            System.out.println("Adding: " + filename);
            ZipEntry entry = new ZipEntry(filename);
            out.putNextEntry(entry);/*from  ww  w  .  j a  va  2 s  . c  o m*/
            IOUtils.copy(stream, out);
            stream.close();
        }
    }

    out.close();
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java

public static byte[] compressXMLFiles(XMLFileVO[] xmlFiles) throws TransformerException, IOException {
    byte[] compressedFiles = null;

    ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
    ZipOutputStream zipOS = new ZipOutputStream(byteOS);
    for (XMLFileVO xmlFile : xmlFiles) {
        ZipEntry entry = new ZipEntry(xmlFile.getName());
        zipOS.putNextEntry(entry);/*from w  ww .ja  v a2 s  . com*/
        byte[] xml = documentToByte(xmlFile.getXml());
        ByteArrayInputStream byteIS = new ByteArrayInputStream(xml);
        IOUtils.copy(byteIS, zipOS);
        byteIS.close();
        zipOS.closeEntry();
    }
    zipOS.close();

    compressedFiles = byteOS.toByteArray();

    return compressedFiles;
}

From source file:Main.java

public static String zip(String filename) throws IOException {
    BufferedInputStream origin = null;
    Integer BUFFER_SIZE = 20480;//from w ww  .  j a  va2s  .c  o  m

    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        String uncompressedFile = flockedFilesFolder.toString() + "/" + filename;
        String compressedFile = flockedFilesFolder.toString() + "/" + "flockZip.zip";
        ZipOutputStream out = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(compressedFile)));
        out.setLevel(9);
        try {
            byte data[] = new byte[BUFFER_SIZE];
            FileInputStream fi = new FileInputStream(uncompressedFile);
            System.out.println("Filename: " + uncompressedFile);
            System.out.println("Zipfile: " + compressedFile);
            origin = new BufferedInputStream(fi, BUFFER_SIZE);
            try {
                ZipEntry entry = new ZipEntry(
                        uncompressedFile.substring(uncompressedFile.lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
                    out.write(data, 0, count);
                }
            } finally {
                origin.close();
            }
        } finally {
            out.close();
        }
        return "flockZip.zip";
    }
    return null;
}

From source file:com.aurel.track.report.datasource.faqs.FaqsDatasource.java

/**
 * @param os//  w ww .j a  va2  s  . c  o  m
 * write the zipped FAQ directory into the download output stream
 */
public static void streamZippedFAQs(OutputStream os) {
    try {
        File tmpDir = FileUtils.getTempDirectory();
        File zipFile = new File(tmpDir.getAbsolutePath() + File.separator + "FAQs.zip");

        try {
            //FileUtils.forceDelete(zipFile);
            zipFile.delete();
        } catch (/*FileNotFound*/Exception fne) {
            // ignore, that is okay
        }

        FileOutputStream fos = new FileOutputStream(zipFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
        FileUtil.zipFiles(new File(FAQ_DIR), zos);
        zos.close();
        fos.close();

        InputStream is = new FileInputStream(zipFile);
        IOUtils.copy(is, os);
        //FileUtils.forceDelete(zipFile);
        zipFile.delete();
        is.close();
    } catch (FileNotFoundException e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    } catch (Exception e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));

    }
}

From source file:it.geosolutions.tools.compress.file.Compressor.java

/**
 * This function zip the input files.//from  w ww. ja v a 2s. c om
 * 
 * @param outputDir
 *            The temporary directory where the zip files.
 * @param zipFileBaseName
 *            The name of the zip file.
 * @param files
 *            The array files to zip.
 * @return The zip file or null.
 * @throws IOException
 */
public static File zip(final File outputDir, final String zipFileBaseName, final File[] files)
        throws IOException {

    if (outputDir != null && files != null && zipFileBaseName != null) {

        // //////////////////////////////////////////
        // Create a buffer for reading the files
        // //////////////////////////////////////////
        final File outZipFile = new File(outputDir, zipFileBaseName + ".zip");
        ZipOutputStream out = null;

        try {

            // /////////////////////////////////
            // Create the ZIP output stream
            // /////////////////////////////////

            out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outZipFile)));

            // /////////////////////
            // Compress the files
            // /////////////////////

            for (File file : files) {
                if (file.isDirectory()) {
                    zipDirectory(file, file, out);
                } else {
                    zipFile(file, out);
                }
            }

            out.close();
            out = null;

            return outZipFile;

        } catch (IOException e) {
            if (LOGGER.isErrorEnabled())
                LOGGER.error(e.getLocalizedMessage(), e);
            return null;
        } finally {
            if (out != null)
                out.close();
        }

    } else
        throw new IOException("One or more input parameters are null!");
}

From source file:org.envirocar.app.util.Util.java

/**
 * Zips a list of files into the target archive.
 * //from  w  w  w .j  a v  a  2s .  c  om
 * @param files
 *            the list of files of the target archive
 * @param target
 *            the target filename
 * @throws IOException 
 */
public static void zipNative(List<File> files, String target) throws IOException {
    ZipOutputStream zos = null;
    try {
        File targetFile = new File(target);
        FileOutputStream dest = new FileOutputStream(targetFile);

        zos = new ZipOutputStream(new BufferedOutputStream(dest));

        for (File f : files) {
            byte[] bytes = readFileContents(f).toByteArray();
            ZipEntry entry = new ZipEntry(f.getName());
            zos.putNextEntry(entry);
            zos.write(bytes);
            zos.closeEntry();
        }

    } catch (IOException e) {
        throw e;
    } finally {
        try {
            if (zos != null)
                zos.close();
        } catch (IOException e) {
            logger.warn(e.getMessage(), e);
        }
    }

}

From source file:de.tor.tribes.util.AttackToTextWriter.java

private static boolean writeBlocksToZip(List<String> pBlocks, Tribe pTribe, File pPath) {
    int fileNo = 1;
    String baseFilename = pTribe.getName().replaceAll("\\W+", "");
    ZipOutputStream zout = null;
    try {// ww w.j  av  a 2s. c  o m
        zout = new ZipOutputStream(
                new FileOutputStream(FilenameUtils.concat(pPath.getPath(), baseFilename + ".zip")));
        for (String block : pBlocks) {
            String entryName = baseFilename + fileNo + ".txt";
            ZipEntry entry = new ZipEntry(entryName);
            try {
                zout.putNextEntry(entry);
                zout.write(block.getBytes());
                zout.closeEntry();
            } catch (IOException ioe) {
                logger.error("Failed to write attack to zipfile", ioe);
                return false;
            }
            fileNo++;
        }
    } catch (IOException ioe) {
        logger.error("Failed to write content to zip file", ioe);
        return false;
    } finally {
        if (zout != null) {
            try {
                zout.flush();
                zout.close();
            } catch (IOException ignored) {
            }
        }
    }
    return true;
}

From source file:com.epam.wilma.message.search.web.support.FileZipper.java

private void closeZipStream(final ZipOutputStream zipStream) {
    try {/*from w w w .  jav a2  s.co m*/
        zipStream.close();
    } catch (IOException e) {
        logger.warn("Zipping search result failed", e);
    }
}

From source file:com.openkm.misc.ZipTest.java

public void testJava() throws IOException {
    log.debug("testJava()");
    File zip = File.createTempFile("java_", ".zip");

    // Create zip
    FileOutputStream fos = new FileOutputStream(zip);
    ZipOutputStream zos = new ZipOutputStream(fos);
    zos.putNextEntry(new ZipEntry("coeta"));
    zos.closeEntry();// w ww  .  jav  a 2 s  .c  o  m
    zos.close();

    // Read zip
    FileInputStream fis = new FileInputStream(zip);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze = zis.getNextEntry();
    System.out.println(ze.getName());
    assertEquals(ze.getName(), "coeta");
    zis.close();
}

From source file:com.nuvolect.securesuite.util.OmniZip.java

public static boolean zipFiles(Context ctx, Iterable<OmniFile> files, OmniFile zipOmni, int destination) {

    ZipOutputStream zos = null;
    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "ZIPPING TO: " + zipOmni.getPath());

    try {//from   www .  j a va 2s  . com
        zos = new ZipOutputStream(zipOmni.getOutputStream());

        for (OmniFile file : files) {

            if (file.isDirectory()) {

                zipSubDirectory(ctx, "", file, zos);
            } else {

                LogUtil.log(LogUtil.LogType.OMNI_ZIP,
                        "zipping up: " + file.getPath() + " (bytes: " + file.length() + ")");

                /**
                 * Might get lucky here, can it associate the name with the copyLarge that follows
                 */
                ZipEntry ze = new ZipEntry(file.getName());
                zos.putNextEntry(ze);

                IOUtils.copyLarge(file.getFileInputStream(), zos);

                zos.flush();
            }
        }

        zos.close();
        return true;
    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
        e.printStackTrace();
    }

    return false;
}