Example usage for java.util.zip ZipOutputStream flush

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

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the compressed output stream.

Usage

From source file:Main.java

public static void compressDir(File file) throws IOException {
    FileOutputStream f = new FileOutputStream(file.getParent() + file.getName() + ".zip");
    CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));

    compressDir(file, out, file.getAbsolutePath());

    out.flush();
    out.close();/*from   w  w w  .  j  a v  a  2  s  .  c o m*/
}

From source file:FolderZiper.java

static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

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

    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();//from  w w  w  . j  a v a  2s.com
}

From source file:de.tudarmstadt.ukp.clarin.webanno.api.dao.ZipUtils.java

/**
 * While exporting annotation documents, some of the writers generate multiple outputs, e.g. a
 * type system file in addition to the annotation data. This method generates a zip file if the
 * exported file do contain multiple file output
 * /*from  ww  w .  j  a  v  a  2 s  .co  m*/
 * @param srcFolder source folder.
 * @param destZipFile target folder.
 * @throws IOException if an I/O error occurs.
 */
public static void zipFolder(File srcFolder, File destZipFile) throws IOException {
    ZipOutputStream zip = null;
    try {
        zip = new ZipOutputStream(new FileOutputStream(destZipFile));

        for (File file : srcFolder.getAbsoluteFile().listFiles()) {
            addToZip(zip, srcFolder.getAbsoluteFile(), file);
        }
        zip.flush();
    } finally {
        closeQuietly(zip);
    }
}

From source file:com.ecofactor.qa.automation.platform.util.FileUtil.java

/**
 * Add a folder to Zip folder./*from w w w  .j a  v a2 s  .  co m*/
 * @param srcFolder the src folder
 * @param destZipFile the dest zip file
 */
public static void zipFolder(final String srcFolder, final String destZipFile) {

    try {
        final FileOutputStream fileWriter = new FileOutputStream(destZipFile);
        final ZipOutputStream zip = new ZipOutputStream(fileWriter);
        addFolderToZip("", srcFolder, zip);
        zip.flush();
        zip.close();
    } catch (Exception e) {
        LOGGER.error("Error in creating zip file", e);
    }
}

From source file:Main.java

/**
 * Compress a String to a zip file that has only one entry like zipName
 * The name shouldn't have .zip//from  w  w w . j a  v a2  s. c om
 * 
 * @param content
 * @param fName
 * @throws IOException 
 */
public static void zipAContentAsFileName(String fName, String content, String charset) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes(charset));
    BufferedInputStream bis = new BufferedInputStream(bais);

    File f = new File(fName);
    File parentFile = f.getParentFile();
    if (!parentFile.exists()) {
        parentFile.mkdirs();
    }
    ZipOutputStream zf = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f + ".zip")));

    ZipEntry entry = new ZipEntry(f.getName());
    zf.putNextEntry(entry);
    byte[] barr = new byte[8192];
    int len = 0;
    while ((len = bis.read(barr)) != -1) {
        zf.write(barr, 0, len);
    }
    zf.flush();
    zf.close();
    bis.close();
    bais.close();
}

From source file:io.lavagna.service.LavagnaExporter.java

private static void writeEntry(String entryName, Object toSerialize, ZipOutputStream zf,
        OutputStreamWriter osw) {
    try {//www . jav a2s  .c o  m
        zf.putNextEntry(new ZipEntry(entryName));
        Json.GSON.toJson(toSerialize, osw);
        osw.flush();
        zf.flush();
        zf.closeEntry();
    } catch (IOException ioe) {
        throw new IllegalStateException("error while serializing entry " + entryName, ioe);
    }
}

From source file:edu.isi.wings.portal.classes.StorageHandler.java

private static void streamDirectory(File directory, OutputStream os) {
    try {/*from w  w w. ja  v  a2s .  c  o  m*/
        // Start the ZipStream reader. Whatever is read is streamed to response
        PipedInputStream pis = new PipedInputStream(2048);
        ZipStreamer pipestreamer = new ZipStreamer(pis, os);
        pipestreamer.start();

        // Start Zipping folder and piping to the ZipStream reader
        PipedOutputStream pos = new PipedOutputStream(pis);
        ZipOutputStream zos = new ZipOutputStream(pos);
        StorageHandler.zipAndStream(directory, zos, directory.getName() + "/");
        zos.flush();
        zos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.wisemapping.util.ZipUtils.java

public static byte[] bytesToZip(@NotNull final byte[] content) throws IOException {
    ZipOutputStream zip = null;
    final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    try {/*from   ww  w. j a  v  a 2 s  .  c o m*/
        zip = new ZipOutputStream(byteArray);
        ZipEntry zEntry = new ZipEntry("content");
        zip.putNextEntry(zEntry);
        IOUtils.write(content, zip);
        zip.closeEntry();
    } finally {
        if (zip != null) {
            zip.flush();
            zip.close();
        }
    }

    return byteArray.toByteArray();
}

From source file:Main.java

/**
 * Compress a folder and its contents.//  ww  w. java2 s.c om
 * 
 * @param srcFolder path to the folder to be compressed.
 * @param destZipFile path to the final output zip file.
 * @param addBaseFolder flag to decide whether to add also the provided base folder or not.
 * @throws IOException 
 */
static public void zipFolder(String srcFolder, String destZipFile, boolean addBaseFolder) throws IOException {
    if (new File(srcFolder).isDirectory()) {

        ZipOutputStream zip = null;
        FileOutputStream fileWriter = null;
        try {
            fileWriter = new FileOutputStream(destZipFile);
            zip = new ZipOutputStream(fileWriter);
            addFolderToZip("", srcFolder, zip, addBaseFolder); //$NON-NLS-1$
        } finally {
            if (zip != null) {
                zip.flush();
                zip.close();
            }
            if (fileWriter != null)
                fileWriter.close();
        }
    } else {
        throw new IOException(srcFolder + " is not a folder.");
    }
}

From source file:com.recomdata.transmart.data.export.util.ZipUtil.java

/**
 * This method will zip a given folder.//from  w  w  w  .  j  a  v  a2  s  .  co  m
 * 
 * @param srcFolder
 * @param destZipFile
 * @throws Exception
 */
static public String zipFolder(String srcFolder, String destZipFile) throws Exception {
    File zipFile = null;
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    zipFile = new File(destZipFile);
    if (zipFile.exists() && zipFile.isFile() && zipFile.delete()) {
        zipFile = new File(destZipFile);
    }

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

    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();

    return zipFile.getName();
}