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:de.uzk.hki.da.pkg.ZipArchiveBuilder.java

public void archiveFolder(File srcFolder, File destFile, boolean includeFolder) throws Exception {

    FileOutputStream fileWriter = new FileOutputStream(destFile);
    ZipOutputStream zip = new ZipOutputStream(fileWriter);

    addFolderToArchive("", srcFolder, zip, includeFolder);

    zip.close();
    fileWriter.close();/*from w  w  w .ja va 2s .  com*/
}

From source file:net.sourceforge.dita4publishers.word2dita.Word2DitaValidationHelper.java

/**
 * @param documentDom/*from  w  ww .  ja va  2 s  . c  o m*/
 * @param commentsDom
 * @param docxZip
 * @param zipFile
 * @throws FileNotFoundException
 * @throws IOException
 * @throws Exception
 */
public static void saveZipComponents(ZipComponents zipComponents, File zipFile)
        throws FileNotFoundException, IOException, Exception {
    ZipOutputStream zipOutStream = new ZipOutputStream(new FileOutputStream(zipFile));
    for (ZipComponent comp : zipComponents.getComponents()) {
        ZipEntry newEntry = new ZipEntry(comp.getName());
        zipOutStream.putNextEntry(newEntry);
        if (comp.isDirectory()) {
            // Nothing to do.
        } else {
            // System.out.println(" + [DEBUG] saving component \"" + comp.getName() + "\"");
            if (comp.getName().endsWith("document.xml") || comp.getName().endsWith("document.xml.rels")) {
                // System.out.println("Handling a file of interest.");
            }
            InputStream inputStream = comp.getInputStream();
            IOUtils.copy(inputStream, zipOutStream);
            inputStream.close();
        }
    }
    zipOutStream.close();
}

From source file:com.mosso.client.cloudfiles.sample.FilesCopy.java

/**
 *
 * @param folder/*from w  w  w . j  a v a  2  s.co m*/
 * @return null if the input is not a folder otherwise a zip file containing all the files in the folder with nested folders skipped.
 * @throws IOException
 */
public static File zipFolder(File folder) throws IOException {
    byte[] buf = new byte[1024];
    int len;

    // Create the ZIP file
    String filenameWithZipExt = folder.getName() + ZIPEXTENSION;
    File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt));

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile));

    if (folder.isDirectory()) {
        File[] files = folder.listFiles();

        for (File f : files) {
            if (!f.isDirectory()) {
                FileInputStream in = new FileInputStream(f);

                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(f.getName()));

                // Transfer bytes from the file to the ZIP file
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Complete the entry
                out.closeEntry();
                in.close();
            } else
                logger.warn("Skipping nested folder: " + f.getAbsoluteFile());
        }

        out.flush();
        out.close();
    } else {
        logger.warn("The folder name supplied is not a folder!");
        System.err.println("The folder name supplied is not a folder!");
        return null;
    }

    return zippedFile;
}

From source file:com.aurel.track.admin.server.dbbackup.DatabaseBackupBL.java

private static void zipPart2(Boolean includeAttachments, String backupName, File tmpDir)
        throws DatabaseBackupBLException {
    if (includeAttachments) {
        String attachDir = AttachBL.getAttachDirBase();
        String distDir = tmpDir.getAbsolutePath() + File.separator + ATTACHMENTS_NAME;
        copyAttachments(attachDir, distDir);
    }/*  w w w  . j a va  2 s  .  com*/
    String backupFileName = backupName;
    if (!backupFileName.endsWith(".zip")) {
        backupFileName = backupFileName + ".zip";
    }
    String backupRootDirPath = ApplicationBean.getInstance().getSiteBean().getBackupDir();
    if (backupRootDirPath == null || "".equals(backupRootDirPath)) {
        LOGGER.error("No backup directory defined");
        DatabaseBackupBLException ex = new DatabaseBackupBLException("No backup directory defined");
        ex.setLocalizedKey("admin.server.databaseBackup.err.noBackupDirectory");
        throw ex;
    }
    File fbackupRootDir = new File(ApplicationBean.getInstance().getSiteBean().getBackupDir());
    if (!fbackupRootDir.exists()) {
        fbackupRootDir.mkdirs();
    }
    File fbackup = new File(fbackupRootDir, backupFileName);
    ZipOutputStream zipOut = null;
    try {
        zipOut = new ZipOutputStream(new FileOutputStream(fbackup), Charset.forName("UTF-8"));
    } catch (FileNotFoundException e) {
        LOGGER.error("Can't find backup file:\"" + fbackup.getAbsolutePath() + "\"");
        DatabaseBackupBLException ex = new DatabaseBackupBLException(
                "Can't find backup file:\"" + fbackup.getAbsolutePath() + "\"", e);
        ex.setLocalizedKey("admin.server.databaseBackup.err.cantFindBackupFile");
        ex.setLocalizedParams(new Object[] { fbackup.getAbsolutePath() });
        throw ex;
    }

    FileUtil.zipFiles(tmpDir, zipOut);

    try {
        zipOut.close();
        LOGGER.info("Backup created successfully at " + new Date());
    } catch (IOException e) {
        LOGGER.error("Can't close zip file: ");
        DatabaseBackupBLException ex = new DatabaseBackupBLException("Can't close zip file", e);
        ex.setLocalizedKey("admin.server.databaseBackup.err.cantCloseZipFile");
        ex.setLocalizedParams(new Object[] { fbackup.getAbsolutePath() });
        throw ex;
    }
    FileUtil.deltree(tmpDir);
}

From source file:com.thejustdo.util.Utils.java

/**
 * Creates a ZIP file containing all the files inside a directory.
 * @param location Directory to read./*from  w w w  .j  ava2 s . com*/
 * @param pathname Name and path where to store the file.
 * @throws FileNotFoundException If can't find the initial directory.
 * @throws IOException If can't read/write.
 */
public static void zipDirectory(File location, File pathname) throws FileNotFoundException, IOException {

    BufferedInputStream origin;
    FileOutputStream dest = new FileOutputStream(pathname);
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
    byte data[] = new byte[2048];

    // 1. Get a list of files from current directory
    String files[] = location.list();
    File f;

    // 2. Adding each file to the zip-set.
    for (String s : files) {
        log.info(String.format("Adding: %s", s));
        f = new File(location, s);
        FileInputStream fi = new FileInputStream(f);
        origin = new BufferedInputStream(fi, 2048);
        ZipEntry entry = new ZipEntry(location.getName() + File.separator + s);
        out.putNextEntry(entry);
        int count;
        while ((count = origin.read(data, 0, 2048)) != -1) {
            out.write(data, 0, count);
        }
        origin.close();
    }
    out.close();
}

From source file:com.sqli.liferay.imex.util.xml.ZipUtils.java

public void zipFiles(File archive, File inputDir) throws IOException {

    FileOutputStream dest = new FileOutputStream(archive);
    CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checksum));

    zipEntries(out, inputDir, inputDir);

    out.close();
    log.debug("checksum:" + checksum.getChecksum().getValue());

}

From source file:com.ibm.liberty.starter.ProjectZipConstructor.java

public void buildZip(OutputStream os)
        throws IOException, SAXException, ParserConfigurationException, TransformerException {
    initializeMap();/*from  w w  w  . j a v  a 2  s .  co  m*/
    addHtmlToMap();
    addTechSamplesToMap();
    addPomFileToMap();
    ZipOutputStream zos = new ZipOutputStream(os);
    createZipFromMap(zos);
    zos.close();
}

From source file:it.isislab.sof.core.engine.hadoop.sshclient.connection.SofManager.java

private static void zipDir(String zipFileName, String dir) throws Exception {
    File dirObj = new File(dir);
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    log.info("Creating : " + zipFileName);
    addDir(dirObj, out, dir);//from   w  w w. j a v a2s  . com
    out.close();
}

From source file:io.apigee.buildTools.enterprise4g.utils.ZipUtils.java

public void zipDir(File zipFileName, File dirObj, String prefix) throws IOException {

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    log.debug("Creating : " + zipFileName);
    addDir(dirObj, out, dirObj, prefix);
    out.close();
}

From source file:com.plotsquared.iserver.util.FileUtils.java

/**
 * Add files to a zip file/*w w w. j  av  a 2s.c  om*/
 *
 * @param zipFile Zip File
 * @param files   Files to add to the zip
 * @param delete  If the original files should be deleted
 * @throws Exception If anything goes wrong
 */
public static void addToZip(final File zipFile, final File[] files, final boolean delete) throws Exception {
    Assert.notNull(zipFile, files);

    if (!zipFile.exists()) {
        if (!zipFile.createNewFile()) {
            throw new RuntimeException("Couldn't create " + zipFile);
        }
    }

    final File temporary = File.createTempFile(zipFile.getName(), "");
    //noinspection ResultOfMethodCallIgnored
    temporary.delete();

    if (!zipFile.renameTo(temporary)) {
        throw new RuntimeException("Couldn't rename " + zipFile + " to " + temporary);
    }

    final byte[] buffer = new byte[1024 * 16]; // 16mb

    ZipInputStream zis = new ZipInputStream(new FileInputStream(temporary));
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry e = zis.getNextEntry();
    while (e != null) {
        String n = e.getName();

        boolean no = true;
        for (File f : files) {
            if (f.getName().equals(n)) {
                no = false;
                break;
            }
        }

        if (no) {
            zos.putNextEntry(new ZipEntry(n));
            int len;
            while ((len = zis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
        }
        e = zis.getNextEntry();
    }
    zis.close();
    for (File file : files) {
        InputStream in = new FileInputStream(file);
        zos.putNextEntry(new ZipEntry(file.getName()));

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

        zos.closeEntry();
        in.close();
    }
    zos.close();
    temporary.delete();

    if (delete) {
        for (File f : files) {
            f.delete();
        }
    }
}