Example usage for java.util.zip ZipOutputStream finish

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

Introduction

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

Prototype

public void finish() throws IOException 

Source Link

Document

Finishes writing the contents of the ZIP output stream without closing the underlying stream.

Usage

From source file:org.exist.util.Compressor.java

/**
 * The method <code>compress</code>
 *
 * @param whatToCompress a <code>byte[]</code> value
 * @param length an <code>int</code> value
 * @return a <code>byte[]</code> value
 * @exception IOException if an error occurs
 *///from  w  w  w. ja va 2s .  com
public static byte[] compress(byte[] whatToCompress, int length) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ZipOutputStream gzos = new ZipOutputStream(baos);
    gzos.setMethod(ZipOutputStream.DEFLATED);
    gzos.putNextEntry(new ZipEntry(length + ""));
    gzos.write(whatToCompress, 0, length);
    gzos.closeEntry();
    gzos.finish();
    gzos.close();
    return baos.toByteArray();
}

From source file:org.clickframes.util.ZipUtils.java

@SuppressWarnings("unchecked")
/*/*w w  w.  j a v  a2s. c om*/
 * * zip the directory to the target file
 */
public static void zipDir(File directory, File targetFile) throws IOException {
    targetFile.getParentFile().mkdirs();

    ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(targetFile));

    for (File child : (Collection<File>) FileUtils.listFiles(directory, null, true)) {
        zipStream.putNextEntry(new ZipEntry(getRelativePath(child, directory)));
        IOUtils.copy(new FileInputStream(child), zipStream);
    }

    zipStream.finish();
}

From source file:org.craftercms.commons.zip.ZipUtils.java

/**
 * Zips a collection of files to a destination zip output stream.
 *
 * @param files        A collection of files and directories
 * @param outputStream The output stream of the destination zip file
 * @throws FileNotFoundException/*  w  ww .  j ava 2  s .  c  om*/
 * @throws IOException
 */
public static void zipFiles(List<File> files, OutputStream outputStream) throws IOException {
    ZipOutputStream zos = new ZipOutputStream(outputStream);

    for (File file : files) {
        if (file.isDirectory()) { //if it's a folder
            addFolderToZip("", file, zos);
        } else {
            addFileToZip("", file, zos);
        }
    }

    zos.finish();
}

From source file:edu.kit.dama.util.ZipUtils.java

/**
 * Write all files located in pDirectory into a zip file specified by
 * pZipOut. The provided base path is used to keep the structure defined by
 * pDirectory within the zip file./*from ww  w  . j  a  v a 2s . c o m*/
 *
 * Due to the fact, that we have only one single base path, pDirectory must
 * start with pBasePath to avoid unexpected zip file entries.
 *
 * @param pFiles The directory containing the input files
 * @param pBasePath The base path the will be removed from all file paths
 * before creating a new zip entry
 * @param pZipOut The zip output file
 * @throws IOException If something goes wrong, in most cases if there are
 * problems with reading the input files or writing into the output file
 */
public static void zip(File[] pFiles, String pBasePath, File pZipOut) throws IOException {
    ZipOutputStream zipOut = null;
    try {
        zipOut = new ZipOutputStream(new FileOutputStream(pZipOut));
        zip(pFiles, pBasePath, zipOut);
        zipOut.finish();
        zipOut.flush();
    } finally {
        try {
            if (zipOut != null) {
                zipOut.close();
            }
        } catch (IOException ignored) {
        }
    }
}

From source file:com.formkiq.core.util.Zips.java

/**
 * Create Zip file.//from w w w . ja  v a2s  .  co  m
 * @param map {@link Map}
 * @return byte[] zip file
 * @throws IOException IOException
 */
public static byte[] zipFile(final Map<String, byte[]> map) throws IOException {

    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(bs);

    for (Map.Entry<String, byte[]> e : map.entrySet()) {

        String name = e.getKey();
        byte[] data = e.getValue();

        ZipEntry ze = new ZipEntry(name);
        zip.putNextEntry(ze);

        zip.write(data, 0, data.length);
        zip.closeEntry();
    }

    zip.finish();

    byte[] bytes = bs.toByteArray();

    IOUtils.closeQuietly(bs);
    IOUtils.closeQuietly(zip);

    return bytes;
}

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

/**
 * Close an open ZipOutputStream without errormessages
 * //w  w  w  . j  a v  a 2 s.c o  m
 * @param zipOutputStream
 * @throws IOException
 */
public static void closeZipOutputStreamQuietly(ZipOutputStream zipOutputStream) {
    try {
        zipOutputStream.finish();
        zipOutputStream.flush();
        zipOutputStream.close();
        zipOutputStream = null;
    } catch (IOException e) {
    } finally {
        if (zipOutputStream != null) {
            try {
                zipOutputStream.close();
            } catch (Exception e) {
            }
            zipOutputStream = null;
        }
    }
}

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

/**
 * Close an open ZipOutputStream/*from ww  w. j a  v a  2 s . c  o  m*/
 * 
 * @param zipOutputStream
 * @throws IOException
 */
public static void closeZipOutputStream(ZipOutputStream zipOutputStream) throws IOException {
    try {
        zipOutputStream.finish();
        zipOutputStream.flush();
        zipOutputStream.close();
        zipOutputStream = null;
    } catch (IOException e) {
        throw e;
    } finally {
        if (zipOutputStream != null) {
            try {
                zipOutputStream.close();
            } catch (Exception e) {
            }
            zipOutputStream = null;
        }
    }
}

From source file:org.apache.jetspeed.portlets.site.PortalSiteManagerUtil.java

private static boolean zipObject(String sourcePath, String target) {
    FileOutputStream fos = null;/*w ww .  j  a v a2 s  .co  m*/
    ZipOutputStream cpZipOutputStream = null;

    try {
        File cpFile = new File(sourcePath);
        if (!cpFile.isDirectory()) {
            return false;
        }
        fos = new FileOutputStream(target);
        cpZipOutputStream = new ZipOutputStream(fos);
        cpZipOutputStream.setLevel(9);
        zipFiles(cpFile, sourcePath, cpZipOutputStream);
        cpZipOutputStream.finish();
    } catch (Exception e) {
        logger.error("Unexpected exception during writing to zip output stream.", e);
        return false;
    } finally {
        if (cpZipOutputStream != null) {
            try {
                cpZipOutputStream.close();
            } catch (Exception ce) {
            }
        }

        if (fos != null) {
            try {
                fos.close();
            } catch (Exception ce) {
            }
        }
    }
    return true;
}

From source file:org.wso2.carbon.apimgt.hybrid.gateway.usage.publisher.util.zip.ZIPUtils.java

/**
 * Compresses the given file into ZIP format
 *
 * @param sourcePath source file path//  ww w .j  a v  a  2s  .  com
 * @param destinationPath destination file path
 * @throws ZIPException if there is an error while compressing
 */
public static void compressFile(String sourcePath, String destinationPath) throws ZIPException {
    if (log.isDebugEnabled()) {
        log.debug("Compressing file : " + sourcePath + " to : " + destinationPath);
    }
    byte[] buffer = new byte[BUFFER_SIZE];
    ZipOutputStream zipOutputStream = null;
    FileOutputStream fileOutputStream = null;
    FileInputStream fileInputStream = null;

    try {
        fileOutputStream = new FileOutputStream(destinationPath);
        zipOutputStream = new ZipOutputStream(fileOutputStream);
        ZipEntry zipEntry = new ZipEntry(sourcePath);
        zipOutputStream.putNextEntry(zipEntry);
        fileInputStream = new FileInputStream(sourcePath);

        int len;
        while ((len = fileInputStream.read(buffer)) > 0) {
            zipOutputStream.write(buffer, 0, len);
        }
        zipOutputStream.finish();
    } catch (IOException ex) {
        throw new ZIPException("Error occurred while compressing the file : " + sourcePath, ex);
    } finally {
        IOUtils.closeQuietly(fileInputStream);
        IOUtils.closeQuietly(fileOutputStream);
        IOUtils.closeQuietly(zipOutputStream);
    }
}

From source file:de.fu_berlin.inf.dpp.netbeans.feedback.ErrorLogManager.java

/**
 * Convenience wrapper method to upload an error log file to the server. To
 * save time and storage space, the log is compressed to a zip archive with
 * the given zipName.//  ww w.  j a v a2  s. c o m
 * 
 * @param zipName
 *            a name for the zip archive, e.g. with added user ID to make it
 *            unique, zipName must be at least 3 characters long!
 * @throws IOException
 *             if an I/O error occurs
 */
private static void uploadErrorLog(String zipName, File file, IProgressMonitor monitor) throws IOException {

    if (ERROR_LOG_UPLOAD_URL == null) {
        log.warn("error log upload url is not configured, cannot upload error log file");
        return;
    }

    File archive = new File(System.getProperty("java.io.tmpdir"), zipName + ".zip");

    ZipOutputStream out = null;
    FileInputStream in = null;

    byte[] buffer = new byte[8192];

    try {

        in = new FileInputStream(file);

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

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

        int read;

        while ((read = in.read(buffer)) > 0)
            out.write(buffer, 0, read);

        out.finish();
        out.close();

        FileSubmitter.uploadFile(archive, ERROR_LOG_UPLOAD_URL, monitor);
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
        archive.delete();
    }
}