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:com.mvdb.etl.actions.ActionUtils.java

public static void zipFullDirectory(String sourceDir, String targetZipFile) {
    FileOutputStream fos = null;// www. j  ava2  s.c  o  m
    BufferedOutputStream bos = null;
    ZipOutputStream zos = null;

    try {
        fos = new FileOutputStream(targetZipFile);
        bos = new BufferedOutputStream(fos);
        zos = new ZipOutputStream(bos);
        zipDir(sourceDir, new File(sourceDir), zos);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zos != null) {
            try {
                zos.flush();
                zos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if (bos != null) {
            try {
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if (fos != null) {
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java

/**
 * saves a sound package with all meta information and audio files to a ZIP
 * file and creates the security tokens.
 *
 * @param packageFile the zip file, where the soundpackage should be stored
 * @param soundPackage the sound package info
 * @throws com.dreikraft.infactory.sound.SoundPackageException encapsulates
 * all low level (IO) exceptions/*from  w w  w .  j  av  a2s  .co m*/
 */
public static void exportSoundPackage(final File packageFile, final SoundPackage soundPackage)
        throws SoundPackageException {

    if (packageFile == null) {
        throw new SoundPackageException(new IllegalArgumentException("null package file"));
    }

    if (packageFile.delete()) {
        log.info("successfully deleted file: " + packageFile.getAbsolutePath());
    }

    ZipOutputStream out = null;
    InputStream in = null;
    try {
        out = new ZipOutputStream(new FileOutputStream(packageFile));
        out.setLevel(9);

        // write package info
        writePackageInfoZipEntry(soundPackage, out);

        // create path entries
        ZipEntry soundDir = new ZipEntry(SOUNDS_PATH_PREFIX + SL);
        out.putNextEntry(soundDir);
        out.flush();
        out.closeEntry();

        // write files
        for (Sound sound : soundPackage.getSounds()) {
            File axboFile = new File(sound.getAxboFile().getPath());
            in = new FileInputStream(axboFile);
            writeZipEntry(SOUNDS_PATH_PREFIX + SL + axboFile.getName(), out, in);
            in.close();
        }
    } catch (FileNotFoundException ex) {
        throw new SoundPackageException(ex);
    } catch (IOException ex) {
        throw new SoundPackageException(ex);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                log.error("failed to close ZipOutputStream", ex);
            }
        }
        try {
            if (in != null)
                in.close();
        } catch (IOException ex) {
            log.error("failed to close FileInputStream", ex);
        }
    }
}

From source file:com.mc.printer.model.utils.ZipHelper.java

private static void writeZip(File file, String parentPath, ZipOutputStream zos) {
    if (file.exists()) {
        if (file.isDirectory()) {//?
            parentPath += file.getName() + File.separator;
            File[] files = file.listFiles();
            for (File f : files) {
                writeZip(f, parentPath, zos);
            }//from   w  w w .  j a  v  a2s .  c om
        } else {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                ZipEntry ze = new ZipEntry(parentPath + file.getName());
                zos.putNextEntry(ze);
                byte[] content = new byte[1024];
                int len;
                while ((len = fis.read(content)) != -1) {
                    zos.write(content, 0, len);
                    zos.flush();
                }

            } catch (FileNotFoundException e) {
                log.error("create zip file failed.", e);
            } catch (IOException e) {
                log.error("create zip file failed.", e);
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                } catch (IOException e) {
                    log.error("create zip file failed.", e);
                }
            }
        }
    }
}

From source file:org.jboss.tools.tycho.sitegenerator.FetchSourcesFromManifests.java

public static void zipDirectory(File dir, File zipFile) throws IOException {
    FileOutputStream fout = new FileOutputStream(zipFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zipSubDirectory("", dir, zout);
    zout.flush();
    zout.close();/*  w w  w  . j  a v  a 2  s.  co  m*/
    fout.flush();
    fout.close();
}

From source file:org.solmix.commons.util.Files.java

/**
 * /*from   ww w.ja v a2 s  . c o  m*/
 * :?ZipOutputStreamZipInputStreamzip.
 * :java.util.zip??,zip??,
 * :"Exception  in thread "main " java.lang.IllegalArgumentException 
 * at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)
 * @param srcDir
 *            ?
 * @param destDir
 *            ?
 * @throws Exception
 */
public static void zip(String srcDir, String destDir) throws IOException {
    String tempFileName = null;
    byte[] buf = new byte[1024 * 2];
    int len;
    // ??
    File[] files = new File(srcDir).listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isFile()) {
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
                if (destDir.endsWith(File.separator)) {
                    tempFileName = destDir + file.getName() + ".zip";
                } else {
                    tempFileName = destDir + File.separator + file.getName() + ".zip";
                }
                FileOutputStream fos = new FileOutputStream(tempFileName);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                ZipOutputStream zos = new ZipOutputStream(bos);// 

                ZipEntry ze = new ZipEntry(file.getName());// ??
                zos.putNextEntry(ze);// ZIP?????

                while ((len = bis.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                    zos.flush();
                }
                bis.close();
                zos.close();

            }
        }
    }
}

From source file:de.tor.tribes.util.AttackToTextWriter.java

private static boolean writeBlocksToZip(List<String> pBlocks, Tribe pTribe, File pPath) {
    int fileNo = 1;
    String baseFilename = pTribe.getName().replaceAll("\\W+", "");
    ZipOutputStream zout = null;
    try {// w  w  w.  j a va2s.c om
        zout = new ZipOutputStream(
                new FileOutputStream(FilenameUtils.concat(pPath.getPath(), baseFilename + ".zip")));
        for (String block : pBlocks) {
            String entryName = baseFilename + fileNo + ".txt";
            ZipEntry entry = new ZipEntry(entryName);
            try {
                zout.putNextEntry(entry);
                zout.write(block.getBytes());
                zout.closeEntry();
            } catch (IOException ioe) {
                logger.error("Failed to write attack to zipfile", ioe);
                return false;
            }
            fileNo++;
        }
    } catch (IOException ioe) {
        logger.error("Failed to write content to zip file", ioe);
        return false;
    } finally {
        if (zout != null) {
            try {
                zout.flush();
                zout.close();
            } catch (IOException ignored) {
            }
        }
    }
    return true;
}

From source file:com.nuvolect.securesuite.util.OmniZip.java

public static boolean zipFiles(Context ctx, Iterable<OmniFile> files, OmniFile zipOmni, int destination) {

    ZipOutputStream zos = null;
    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "ZIPPING TO: " + zipOmni.getPath());

    try {// w  w  w . j a v  a  2  s  .co  m
        zos = new ZipOutputStream(zipOmni.getOutputStream());

        for (OmniFile file : files) {

            if (file.isDirectory()) {

                zipSubDirectory(ctx, "", file, zos);
            } else {

                LogUtil.log(LogUtil.LogType.OMNI_ZIP,
                        "zipping up: " + file.getPath() + " (bytes: " + file.length() + ")");

                /**
                 * Might get lucky here, can it associate the name with the copyLarge that follows
                 */
                ZipEntry ze = new ZipEntry(file.getName());
                zos.putNextEntry(ze);

                IOUtils.copyLarge(file.getFileInputStream(), zos);

                zos.flush();
            }
        }

        zos.close();
        return true;
    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
        e.printStackTrace();
    }

    return false;
}

From source file:org.sakaiproject.portal.charon.test.PortalTestFileUtils.java

/**
 * pack a segment into the zip/* w w w .  ja  v a2s.  c  o m*/
 * 
 * @param addsi
 * @return
 * @throws IOException
 */
public static void pack(File source, final String basePath, final String replacePath, OutputStream output)
        throws IOException {
    log.debug("Packing " + source + " repacing " + basePath + " with " + replacePath);
    final ZipOutputStream zout = new ZipOutputStream(output);
    final byte[] buffer = new byte[1024 * 100];
    FileInputStream fin = null;
    try {
        recurse(source, new RecurseAction() {

            public void doFile(File file) throws IOException {
                if (!file.isDirectory()) {
                    log.debug("               Add " + file.getPath());
                    addSingleFile(basePath, replacePath, file, zout, buffer);
                } else {
                    log.debug("              Ignore " + file.getPath());
                }
            }

            public void doBeforeFile(File f) {
            }

            public void doAfterFile(File f) {
            }

        });
    } finally {
        zout.flush();
        try {
            zout.close();
        } catch (Exception e) {
        }
        try {
            fin.close();
        } catch (Exception e) {
        }
    }
}

From source file:org.jgrades.backup.creator.ZipUtils.java

private void zipFolder(String srcFolder, String destZipFile) throws IOException {
    FileOutputStream fileWriter = new FileOutputStream(destZipFile);
    ZipOutputStream zip = new ZipOutputStream(fileWriter);
    addFolderToZip(StringUtils.EMPTY, srcFolder, zip);
    zip.flush();
    zip.close();/*from w  w  w .  j  a  va 2s .  c o  m*/
}

From source file:org.geoserver.wps.ppio.ZipArchivePPIO.java

/**
 * This method zip the provided file to the provided {@link ZipOutputStream}.
 * //from  w w  w  . ja va  2 s .com
 * <p>
 * It throws {@link IllegalArgumentException} in case the provided file does not exists or is not a readable file.
 * 
 * @param file the {@link File} to zip
 * @param zipout the {@link ZipOutputStream} to write to
 * @param buffer the buffer to use for reading/writing
 * @throws IOException in case something bad happen
 */
private static void zipFileInternal(File file, ZipOutputStream zipout, byte[] buffer) throws IOException {
    if (file == null || !file.exists() || !file.canRead()) {
        throw new IllegalArgumentException(
                "Provided File is not valid and/or reqadable! --> File:" + file != null ? file.getAbsolutePath()
                        : "null");
    }

    final ZipEntry entry = new ZipEntry(FilenameUtils.getName(file.getAbsolutePath()));
    zipout.putNextEntry(entry);

    // copy over the file
    InputStream in = null;
    try {
        int c;
        in = new FileInputStream(file);
        while (-1 != (c = in.read(buffer))) {
            zipout.write(buffer, 0, c);
        }
        zipout.closeEntry();
    } finally {
        // close the input stream
        if (in != null) {
            org.apache.commons.io.IOUtils.closeQuietly(in);
        }
    }
    zipout.flush();
}