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:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.ArchiveCompressorZipImpl.java

/**
 * Compress the given files into an archive with the given name, plus the file extension.  Put the compressed
 * archive into the given directory./*from  w  w  w  .ja  v a2  s  . com*/
 *
 * @param files                the files to include in the archive
 * @param archiveName          the name of the archive, minus extension
 * @param destinationDirectory the location to put the new compressed archive
 * @param compress              flag to compress the archive
 * @return the File representing the created compressed archive
 * @throws IOException if it needs to
 */
public File createArchive(final List<File> files, final String archiveName, final File destinationDirectory,
        final Boolean compress) throws IOException {
    final File archiveFile = new File(destinationDirectory, archiveName + ZIP_EXTENSION);

    ZipOutputStream out = null;
    FileInputStream in = null;

    try {
        //noinspection IOResourceOpenedButNotSafelyClosed
        out = new ZipOutputStream(new FileOutputStream(archiveFile));
        final byte[] buf = new byte[1024];
        for (final File file : files) {
            try {
                //noinspection IOResourceOpenedButNotSafelyClosed
                in = new FileInputStream(file);
                out.putNextEntry(new ZipEntry(archiveName + File.separator + file.getName()));
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            } finally {
                IOUtils.closeQuietly(in);
            }
        }
    } finally {
        IOUtils.closeQuietly(out);
    }
    return archiveFile;
}

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

private MimeBodyPart zipAttachment(byte[] attach, String containedFileName, String zipFileName,
        String nameSuffix, String fileExtension) {
    MimeBodyPart messageBodyPart = null;
    try {/*from   www  .j a  va  2 s .c  om*/

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ZipOutputStream zipOut = new ZipOutputStream(bout);
        String entryName = containedFileName + nameSuffix + fileExtension;
        zipOut.putNextEntry(new ZipEntry(entryName));
        zipOut.write(attach);
        zipOut.closeEntry();

        zipOut.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) {
        // TODO: handle exception            
    }
    return messageBodyPart;
}

From source file:org.apache.chemistry.opencmis.tools.specexamples.Main.java

private static void addDirectory(ZipOutputStream zout, String prefix, File sourceDir) throws IOException {

    File[] files = sourceDir.listFiles();
    LOG.debug("Create Zip, adding directory " + sourceDir.getName());

    if (null != files) {
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                addDirectory(zout, prefix + File.separator + files[i].getName(), files[i]);
            } else {
                LOG.debug("Create Zip, adding file " + files[i].getName());
                byte[] buffer = new byte[65536];
                FileInputStream fin = new FileInputStream(files[i]);
                String zipEntryName = prefix + File.separator + files[i].getName();
                LOG.debug("   adding entry " + zipEntryName);
                zout.putNextEntry(new ZipEntry(zipEntryName));

                int length;
                while ((length = fin.read(buffer)) > 0) {
                    zout.write(buffer, 0, length);
                }//ww w  .jav a  2  s. c o  m

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

From source file:com.qwazr.tools.ArchiverTool.java

public void addToZipFile(String entryName, String filePath, ZipOutputStream zos) throws IOException {

    File srcFile = new File(filePath);
    if (!srcFile.exists())
        throw new FileNotFoundException("The file does not exists: " + srcFile.getPath());
    FileInputStream fis = new FileInputStream(srcFile);
    try {//w ww . j  a v  a  2 s .c om
        ZipEntry zipEntry = new ZipEntry(entryName);
        zos.putNextEntry(zipEntry);
        IOUtils.copy(fis, zos);
        zos.closeEntry();
    } finally {
        IOUtils.closeQuietly(fis);
    }
}

From source file:org.tonguetied.datatransfer.DataServiceImpl.java

public void createArchive(File directory) throws ExportException, IllegalArgumentException {
    if (!directory.isDirectory())
        throw new IllegalArgumentException("expecting a directory");

    ZipOutputStream zos = null;
    try {/*ww w .  j  a  va  2 s . c  o  m*/
        File[] files = directory.listFiles();
        if (files.length > 0) {
            final File archive = new File(directory, directory.getName() + ".zip");
            zos = new ZipOutputStream(new FileOutputStream(archive));
            for (File file : files) {
                zos.putNextEntry(new ZipEntry(file.getName()));
                IOUtils.write(FileUtils.readFileToByteArray(file), zos);
                zos.closeEntry();
            }

            if (logger.isDebugEnabled())
                logger.debug("archived " + files.length + " files to " + archive.getPath());
        }
    } catch (IOException ioe) {
        throw new ExportException(ioe);
    } finally {
        IOUtils.closeQuietly(zos);
    }
}

From source file:io.neba.core.logviewer.LogfileViewerConsolePlugin.java

/**
 * Streams the contents of the log directory as a zip file.
 *//*from   w ww  . j  a v a 2 s  . c o  m*/
private void download(HttpServletResponse res, HttpServletRequest req) throws IOException {
    final String selectedLogfile = req.getParameter("file");
    final String filenameSuffix = isEmpty(selectedLogfile) ? ""
            : "-" + substringAfterLast(selectedLogfile, File.separator);

    res.setContentType("application/zip");
    res.setHeader("Content-Disposition",
            "attachment;filename=logfiles-" + req.getServerName() + filenameSuffix + ".zip");
    ZipOutputStream zos = new ZipOutputStream(res.getOutputStream());
    try {
        for (File file : this.logFiles.resolveLogFiles()) {
            if (selectedLogfile != null && !file.getAbsolutePath().equals(selectedLogfile)) {
                continue;
            }

            ZipEntry ze = new ZipEntry(toZipFileEntryName(file));
            zos.putNextEntry(ze);
            FileInputStream in = new FileInputStream(file);
            try {
                copy(in, zos);
                zos.closeEntry();
            } finally {
                closeQuietly(in);
            }
        }
        zos.finish();
    } finally {
        closeQuietly(zos);
    }
}

From source file:com.Candy.center.AboutCandy.java

private boolean zip() {
    String[] source = { systemfile, logfile, last_kmsgfile, kmsgfile };
    try {/* w w  w .j  a  v a  2 s.  com*/
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
        for (int i = 0; i < source.length; i++) {
            String file = source[i].substring(source[i].lastIndexOf("/"), source[i].length());
            FileInputStream in = new FileInputStream(source[i]);
            out.putNextEntry(new ZipEntry(file));
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        }
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:nl.imvertor.common.file.ZipFile.java

/**
 * Zip it/*from  w  ww  .  j  a  v  a 2 s .  c om*/
 * @throws IOException 
 */
// adapted from http://www.mkyong.com/java/how-to-compress-files-in-zip-format/
private void zipIt() throws IOException {
    byte[] buffer = new byte[1024];
    FileOutputStream fos = new FileOutputStream(this);
    ZipOutputStream zos = new ZipOutputStream(fos);
    for (String file : this.fileList) {
        ZipEntry ze = new ZipEntry(file);
        zos.putNextEntry(ze);
        FileInputStream in = new FileInputStream(sourceFolder + File.separator + file);
        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }
        in.close();
    }
    zos.closeEntry();
    zos.close();
}

From source file:br.univali.celine.lms.utils.zip.Zip.java

private void addDir(File dirObj, ZipOutputStream out, int rootLen) throws IOException {
    File[] files = dirObj.listFiles();
    byte[] tmpBuf = new byte[1024];

    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            addDir(files[i], out, rootLen);
            continue;
        }/*from   w w  w  .  j ava  2s.c  o m*/

        FileInputStream in = new FileInputStream(files[i].getAbsolutePath());

        out.putNextEntry(new ZipEntry(files[i].getAbsolutePath().substring(rootLen)));

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

        out.closeEntry();
        in.close();
    }
}

From source file:io.fabric8.maven.generator.springboot.SpringBootGenerator.java

private void copyFilesToFatJar(List<File> libs, List<File> classes, File target) throws IOException {
    File tmpZip = File.createTempFile(target.getName(), null);
    tmpZip.delete();/* w w  w.j ava  2  s.  c o  m*/

    // Using Apache commons rename, because renameTo has issues across file systems
    FileUtils.moveFile(target, tmpZip);

    byte[] buffer = new byte[8192];
    ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
    for (ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()) {
        if (matchesFatJarEntry(libs, ze.getName(), true) || matchesFatJarEntry(classes, ze.getName(), false)) {
            continue;
        }
        out.putNextEntry(ze);
        for (int read = zin.read(buffer); read > -1; read = zin.read(buffer)) {
            out.write(buffer, 0, read);
        }
        out.closeEntry();
    }

    for (File lib : libs) {
        try (InputStream in = new FileInputStream(lib)) {
            out.putNextEntry(createZipEntry(lib, getFatJarFullPath(lib, true)));
            for (int read = in.read(buffer); read > -1; read = in.read(buffer)) {
                out.write(buffer, 0, read);
            }
            out.closeEntry();
        }
    }

    for (File cls : classes) {
        try (InputStream in = new FileInputStream(cls)) {
            out.putNextEntry(createZipEntry(cls, getFatJarFullPath(cls, false)));
            for (int read = in.read(buffer); read > -1; read = in.read(buffer)) {
                out.write(buffer, 0, read);
            }
            out.closeEntry();
        }
    }

    out.close();
    tmpZip.delete();
}