Example usage for java.util.zip ZipOutputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

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

private long addToZip(ZipOutputStream zip, String dir, Collection<File> files) throws IOException {
    files = removeDuplicates(files);//ww  w. ja  va 2 s  .  co m

    long totalSize = 0;
    for (File file : files) {
        ZipEntry entry = new ZipEntry(dir + "/" + file.getName());
        zip.putNextEntry(entry);
        fileIO.copy(file, zip);
        zip.closeEntry();
        totalSize += entry.getCompressedSize();
    }
    return totalSize;
}

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

public static File deflate(final File outputDir, final File zipFile, final File[] files, boolean overwrite) {

    if (zipFile.exists() && overwrite) {
        if (LOGGER.isInfoEnabled())
            LOGGER.info("The output file already exists: " + zipFile + " overvriting");
        return zipFile;
    }/* ww  w .  j a  va  2s . co  m*/

    // Create a buffer for reading the files
    byte[] buf = new byte[Conf.getBufferSize()];

    ZipOutputStream out = null;
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(zipFile);
        bos = new BufferedOutputStream(fos);
        out = new ZipOutputStream(bos);

        // Compress the files
        for (File file : files) {
            FileInputStream in = null;
            try {
                in = new FileInputStream(file);
                if (file.isDirectory()) {
                    continue;
                } else {
                    // Add ZIP entry to output stream.
                    out.putNextEntry(new ZipEntry(file.getName()));
                    // Transfer bytes from the file to the ZIP file
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    out.flush();
                }

            } finally {
                try {
                    // Complete the entry
                    out.closeEntry();
                } catch (Exception e) {
                }
                IOUtils.closeQuietly(in);
            }

        }

    } catch (IOException e) {
        LOGGER.error(e.getLocalizedMessage(), e);
        return null;
    } finally {

        // Complete the ZIP file
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(bos);
        IOUtils.closeQuietly(fos);
    }

    return zipFile;
}

From source file:com.commonsware.android.backup.BackupService.java

private void zipDir(String basePath, File dir, ZipOutputStream zos) throws IOException {
    byte[] buf = new byte[16384];

    if (dir.listFiles() != null) {
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                String path = basePath + file.getName() + "/";

                zos.putNextEntry(new ZipEntry(path));
                zipDir(path, file, zos);
                zos.closeEntry();
            } else if (!file.getName().equals(BACKUP_PREFS_FILENAME)) {
                FileInputStream fin = new FileInputStream(file);
                int length;

                zos.putNextEntry(new ZipEntry(basePath + file.getName()));

                while ((length = fin.read(buf)) > 0) {
                    zos.write(buf, 0, length);
                }/*from w  w w.  j a  v  a 2 s .  co m*/

                zos.closeEntry();
                fin.close();
            }
        }
    }
}

From source file:com.funambol.framework.tools.FileArchiver.java

private void closeEntry(ZipOutputStream zipOutputStream, String entryName) throws FileArchiverException {
    try {/*from   ww w .  jav a2  s. c o  m*/
        zipOutputStream.closeEntry();
    } catch (IOException e) {
        throw new FileArchiverException("An error occurred closing the entry '" + entryName + "'.", e);
    }
}

From source file:com.googlecode.dex2jar.v3.Dex2jar.java

private void check(String dir, ZipOutputStream zos) throws IOException {
    if (dirs.contains(dir)) {
        return;//from w w  w  .j ava 2s. c o m
    }
    dirs.add(dir);
    int i = dir.lastIndexOf('/');
    if (i > 0) {
        check(dir.substring(0, i), zos);
    }
    zos.putNextEntry(new ZipEntry(dir + "/"));
    zos.closeEntry();
}

From source file:de.unisaarland.swan.export.ExportUtil.java

private void createZipEntry(File file, ZipOutputStream zos) throws IOException {
    zos.putNextEntry(new ZipEntry(file.getName()));
    zos.write(FileUtils.readFileToByteArray(file));
    zos.closeEntry();
}

From source file:com.eviware.soapui.impl.wsdl.support.FileAttachment.java

public void cacheFileLocally(File file) throws FileNotFoundException, IOException {
    // write attachment-data to tempfile
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    ZipOutputStream out = new ZipOutputStream(data);
    out.putNextEntry(new ZipEntry(config.getName()));

    InputStream in = new FileInputStream(file);
    long sz = file.length();
    config.setSize(sz);//from  w ww.j  av a  2 s  .c o m

    Tools.writeAll(out, in);

    in.close();
    out.closeEntry();
    out.finish();
    out.close();
    data.close();

    config.setData(data.toByteArray());
}

From source file:com.matze5800.paupdater.Functions.java

public static void addFilesToExistingZip(File zipFile, File[] files) throws IOException {
    File tempFile = new File(Environment.getExternalStorageDirectory() + "/pa_updater", "temp_kernel.zip");
    tempFile.delete();/*from w ww.j  a  v  a2s  .c o  m*/

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[1024];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (File f : files) {
            if (f.getName().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {

            out.putNextEntry(new ZipEntry(name));

            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    zin.close();

    for (int i = 0; i < files.length; i++) {
        InputStream in = new FileInputStream(files[i]);
        out.putNextEntry(new ZipEntry(files[i].getName()));

        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
    out.close();
    tempFile.delete();
}

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

/**
 * This function zip the input directory.
 * /* www.  ja  va  2  s. com*/
 * @param directory
 *            The directory to be zipped.
 * @param base
 *            The base directory.
 * @param out
 *            The zip output stream.
 * @throws IOException
 */
public static void zipDirectory(final File directory, final File base, final ZipOutputStream out)
        throws IOException {

    if (directory != null && base != null && out != null) {
        File[] files = directory.listFiles();
        byte[] buffer = new byte[4096];
        int read = 0;

        FileInputStream in = null;
        ZipEntry entry = null;

        try {
            for (int i = 0, n = files.length; i < n; i++) {
                if (files[i].isDirectory()) {
                    zipDirectory(files[i], base, out);
                } else {
                    in = new FileInputStream(files[i]);
                    entry = new ZipEntry(base.getName().concat("\\")
                            .concat(files[i].getPath().substring(base.getPath().length() + 1)));
                    out.putNextEntry(entry);

                    while (-1 != (read = in.read(buffer))) {
                        out.write(buffer, 0, read);
                    }

                    // //////////////////////
                    // Complete the entry
                    // //////////////////////

                    out.closeEntry();

                    in.close();
                    in = null;
                }
            }

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

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

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

/**
 * This function zip the input file./*from w  w w .  j  a v  a 2s  .  com*/
 * 
 * @param file
 *            The input file to be zipped.
 * @param out
 *            The zip output stream.
 * @throws IOException
 */
public static void zipFile(final File file, final ZipOutputStream out) throws IOException {

    if (file != null && out != null) {

        // ///////////////////////////////////////////
        // Create a buffer for reading the files
        // ///////////////////////////////////////////

        byte[] buf = new byte[4096];

        FileInputStream in = null;

        try {
            in = new FileInputStream(file);

            // //////////////////////////////////
            // Add ZIP entry to output stream.
            // //////////////////////////////////

            out.putNextEntry(new ZipEntry(FilenameUtils.getName(file.getAbsolutePath())));

            // //////////////////////////////////////////////
            // Transfer bytes from the file to the ZIP file
            // //////////////////////////////////////////////

            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

            // //////////////////////
            // Complete the entry
            // //////////////////////

            out.closeEntry();

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

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