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:org.pentaho.osgi.platform.plugin.deployer.impl.PluginZipFileProcessor.java

public void processManifest(ZipOutputStream zipOutputStream) throws IOException {
    Manifest manifest = null;//from www.jav a  2  s.  c om

    String manifestFolder = JarFile.MANIFEST_NAME.split("/")[0] + "/";
    ZipEntry manifestFolderEntry = new ZipEntry(manifestFolder);
    zipOutputStream.putNextEntry(manifestFolderEntry);
    zipOutputStream.closeEntry();

    ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
    zipOutputStream.putNextEntry(manifestEntry);
    new ManifestUpdaterImpl().write(manifest, zipOutputStream, name, symbolicName, version);
    zipOutputStream.closeEntry();

    zipOutputStream.flush();

    zipOutputStream.close();
}

From source file:org.theospi.portfolio.guidance.impl.GuidanceManagerImpl.java

public void packageGuidanceForExport(List guidanceIds, OutputStream os) throws IOException {
    CheckedOutputStream checksum = new CheckedOutputStream(os, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));
    List exportedRefs = new ArrayList();
    for (Iterator i = guidanceIds.iterator(); i.hasNext();) {
        String id = (String) i.next();
        processGuidance(id, zos, exportedRefs);
    }//from www .j a va  2s .  c o  m

    zos.finish();
    zos.flush();
}

From source file:es.urjc.mctwp.bbeans.research.image.DownloadBean.java

private void addFileToZos(ZipOutputStream zos, File file, String path) throws IOException {

    //Prepare entry path
    String entry = null;//from   www .j  a  va 2 s .  c  o m
    if (path != null && !path.isEmpty())
        entry = path + "/" + file.getName();
    else
        entry = file.getName();

    //Create zip entry
    ZipEntry ze = new ZipEntry(entry);
    zos.putNextEntry(ze);

    //Write zip entry
    FileInputStream fileIS = new FileInputStream(file);
    while (fileIS.available() > 0) {
        byte bytes[] = new byte[fileIS.available()];
        fileIS.read(bytes);
        zos.write(bytes);
    }

    zos.flush();
    fileIS.close();
}

From source file:com.cisco.ca.cstg.pdi.services.ConfigurationServiceImpl.java

private void addToZipOutputStream(String filePath, ZipOutputStream zip) throws IOException {
    byte[] buf = new byte[1024];
    int len;/*from   w  w  w  . j  a  v a  2 s .c  o  m*/
    File input = new File(filePath);
    try (FileInputStream in = new FileInputStream(input)) {
        zip.putNextEntry(new ZipEntry(input.getName()));
        while ((len = in.read(buf)) > 0) {
            zip.write(buf, 0, len);
        }
        zip.flush();
    }
}

From source file:org.structr.core.graph.SyncCommand.java

/**
 * Exports the given part of the structr database to the given output stream.
 *
 * @param outputStream//from   w ww .  j a  v  a 2  s . c  o  m
 * @param nodes
 * @param relationships
 * @param filePaths
 * @param includeFiles
 * @throws FrameworkException
 */
public static void exportToStream(final OutputStream outputStream,
        final Iterable<? extends NodeInterface> nodes,
        final Iterable<? extends RelationshipInterface> relationships, final Iterable<String> filePaths,
        final boolean includeFiles) throws FrameworkException {

    try {

        Set<String> filesToInclude = new LinkedHashSet<>();
        ZipOutputStream zos = new ZipOutputStream(outputStream);

        // collect files to include in export
        if (filePaths != null) {

            for (String file : filePaths) {

                filesToInclude.add(file);
            }
        }

        // set compression
        zos.setLevel(6);

        if (includeFiles) {

            logger.log(Level.INFO, "Exporting files..");

            // export files first
            exportDirectory(zos, new File("files"), "", filesToInclude.isEmpty() ? null : filesToInclude);
        }

        // export database
        exportDatabase(zos, new BufferedOutputStream(zos), nodes, relationships);

        // finish ZIP file
        zos.finish();

        // close stream
        zos.flush();
        zos.close();

    } catch (Throwable t) {

        t.printStackTrace();

        throw new FrameworkException(500, t.getMessage());
    }
}

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

/**
 * Compress a file or recursively compress all files of a folder.
 * /* ww w.  j a  va  2 s  .  c o m*/
 * @param sourceFile
 * @param destinationZipFileSream
 * @throws IOException
 */
public static void addFileToOpenZipFileStream(File sourceFile, String relativeDirPath,
        ZipOutputStream destinationZipFileSream) throws IOException {
    BufferedInputStream bufferedFileInputStream = null;

    if (!sourceFile.exists())
        throw new IOException("SourceFile does not exist");

    if (destinationZipFileSream == null)
        throw new IOException("DestinationStream is not ready");

    if (relativeDirPath == null || (!relativeDirPath.endsWith("/") && !relativeDirPath.endsWith("\\")))
        throw new IOException("RelativeDirPath is invalid");

    try {
        if (!sourceFile.isDirectory()) {
            ZipEntry entry = new ZipEntry(relativeDirPath + sourceFile.getName());
            entry.setTime(sourceFile.lastModified());
            destinationZipFileSream.putNextEntry(entry);

            bufferedFileInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
            byte[] bufferArray = new byte[1024];
            int byteBufferFillLength = bufferedFileInputStream.read(bufferArray);
            while (byteBufferFillLength > -1) {
                destinationZipFileSream.write(bufferArray, 0, byteBufferFillLength);
                byteBufferFillLength = bufferedFileInputStream.read(bufferArray);
            }
            bufferedFileInputStream.close();
            bufferedFileInputStream = null;

            destinationZipFileSream.flush();
            destinationZipFileSream.closeEntry();
        } else {
            for (File sourceSubFile : sourceFile.listFiles()) {
                addFileToOpenZipFileStream(sourceSubFile,
                        relativeDirPath + sourceFile.getName() + File.separator, destinationZipFileSream);
            }
        }
    } catch (IOException e) {
        throw e;
    } finally {
        if (bufferedFileInputStream != null) {
            try {
                bufferedFileInputStream.close();
            } catch (Exception e) {
            }
            bufferedFileInputStream = null;
        }
    }
}

From source file:com.mgmtp.jfunk.core.scripting.ModuleArchiver.java

private void zip(final String prefix, final File file, final ZipOutputStream zipOut) throws IOException {
    if (file.isDirectory()) {
        String recursePrefix = prefix + file.getName() + '/';
        for (File child : file.listFiles()) {
            zip(recursePrefix, child, zipOut);
        }//from ww  w .j av  a2s. c o m
    } else {
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            zipOut.putNextEntry(new ZipEntry(prefix + file.getName()));
            copy(in, zipOut);
        } finally {
            closeQuietly(in);
            zipOut.flush();
            zipOut.closeEntry();
        }
    }
}

From source file:org.google.pen.api.util.ZipUtil.java

private static boolean createZipArchive(String srcFolder) throws IOException {
    BufferedInputStream origin = null;
    ZipOutputStream out = null;

    try {/*from www . j av a 2s  .  co  m*/
        final int BUFFER = 2048;
        FileOutputStream dest = new FileOutputStream(new File(srcFolder + ".zip"));
        out = new ZipOutputStream(new BufferedOutputStream(dest));
        byte data[] = new byte[BUFFER];
        File subDir = new File(srcFolder);
        String subdirList[] = subDir.list();
        if (subdirList == null) {
            log.warn("The sub directory " + subDir.getAbsolutePath() + " is empty");
            return false;
        }
        for (String sd : subdirList) {
            // get a list of files from current directory
            File f = new File(srcFolder + "/" + sd);
            if (f.isDirectory()) {
                String files[] = f.list();

                if (files == null) {
                    log.warn("The current directory " + f.getAbsolutePath() + " is empty. Has no files");
                    return false;
                }

                for (int i = 0; i < files.length; i++) {
                    FileInputStream fi = new FileInputStream(srcFolder + "/" + sd + "/" + files[i]);
                    origin = new BufferedInputStream(fi, BUFFER);
                    ZipEntry entry = new ZipEntry(sd + "/" + files[i]);
                    out.putNextEntry(entry);
                    int count;
                    while ((count = origin.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, count);
                        out.flush();
                    }

                }
            } else //it is just a file
            {
                FileInputStream fi = new FileInputStream(f);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(sd);
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                    out.flush();
                }
            }
        }
        out.flush();
    } finally {
        if (origin != null) {
            origin.close();
        }
        if (out != null) {
            out.close();
        }
    }
    return true;
}

From source file:org.wso2.carbon.device.mgt.etc.util.cdmdevice.util.IotDeviceManagementUtil.java

private static boolean createZipArchive(String srcFolder) throws IOException {
    BufferedInputStream origin = null;
    ZipOutputStream out = null;

    try {/*from  ww  w.  j a va2 s. c o  m*/
        final int BUFFER = 2048;

        FileOutputStream dest = new FileOutputStream(new File(srcFolder + ".zip"));

        out = new ZipOutputStream(new BufferedOutputStream(dest));
        byte data[] = new byte[BUFFER];

        File subDir = new File(srcFolder);
        String subdirList[] = subDir.list();

        if (subdirList == null) {
            log.warn("The sub directory " + subDir.getAbsolutePath() + " is empty");
            return false;
        }

        for (String sd : subdirList) {
            // get a list of files from current directory
            File f = new File(srcFolder + "/" + sd);
            if (f.isDirectory()) {
                String files[] = f.list();

                if (files == null) {
                    log.warn("The current directory " + f.getAbsolutePath() + " is empty. Has no files");
                    return false;
                }

                for (int i = 0; i < files.length; i++) {
                    FileInputStream fi = new FileInputStream(srcFolder + "/" + sd + "/" + files[i]);
                    origin = new BufferedInputStream(fi, BUFFER);
                    ZipEntry entry = new ZipEntry(sd + "/" + files[i]);
                    out.putNextEntry(entry);
                    int count;
                    while ((count = origin.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, count);
                        out.flush();
                    }

                }
            } else //it is just a file
            {
                FileInputStream fi = new FileInputStream(f);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(sd);
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                    out.flush();
                }

            }
        }

        out.flush();
    } finally {
        silentClose(origin);
        silentClose(out);
    }
    return true;
}

From source file:org.orbisgis.commons.utils.FileUtils.java

/**
 * Zips the specified file or folder//www.j ava2 s  .  com
 *
 * @param toZip
 * @param outFile
 * @throws IOException
 */
public static void zip(File toZip, File outFile) throws IOException {
    ZipOutputStream out = null;
    try {
        out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));

        byte[] data = new byte[BUF_SIZE];
        ArrayList<File> listToZip = new ArrayList<File>();
        listToZip.add(toZip);

        while (listToZip.size() > 0) {
            File file = listToZip.remove(0);
            if (file.isDirectory()) {
                File[] children = file.listFiles();
                listToZip.addAll(Arrays.asList(children));
            } else {
                BufferedInputStream in = null;
                try {
                    in = new BufferedInputStream(new FileInputStream(file), BUF_SIZE);

                    out.putNextEntry(new ZipEntry(getRelativePath(toZip, file)));
                    int count = in.read(data, 0, BUF_SIZE);
                    while (count != -1) {
                        out.write(data, 0, count);
                        count = in.read(data, 0, BUF_SIZE);
                    }
                    out.closeEntry(); // close each entry
                } finally {
                    if (in != null) {
                        in.close();
                    }
                }
            }
        }
        out.flush();
    } finally {
        if (out != null) {
            out.close();
        }
    }
}