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.datasalt.pangool.solr.TupleSolrOutputFormat.java

private static void createZip(File dir, File out) throws IOException {
    HashSet<File> files = new HashSet<File>();
    // take only conf/ and lib/
    for (String allowedDirectory : SolrRecordWriter.getAllowedConfigDirectories()) {
        File configDir = new File(dir, allowedDirectory);
        boolean configDirExists;
        /** If the directory does not exist, and is required, bail out */
        if (!(configDirExists = configDir.exists())
                && SolrRecordWriter.isRequiredConfigDirectory(allowedDirectory)) {
            throw new IOException(String.format("required configuration directory %s is not present in %s",
                    allowedDirectory, dir));
        }/*from w  ww. j av  a 2s . c om*/
        if (!configDirExists) {
            continue;
        }
        listFiles(configDir, files); // Store the files in the existing, allowed
                                     // directory configDir, in the list of files
                                     // to store in the zip file
    }

    out.delete();
    int subst = dir.toString().length();
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out));
    byte[] buf = new byte[1024];
    for (File f : files) {
        ZipEntry ze = new ZipEntry(f.toString().substring(subst));
        zos.putNextEntry(ze);
        InputStream is = new FileInputStream(f);
        int cnt;
        while ((cnt = is.read(buf)) >= 0) {
            zos.write(buf, 0, cnt);
        }
        is.close();
        zos.flush();
        zos.closeEntry();
    }
    zos.close();
}

From source file:pl.psnc.synat.wrdz.common.utility.ZipUtility.java

/**
 * Packs specified files into destination file.
 * /*from  www. j a v  a2 s .  c  om*/
 * @param srcs
 *            files to pack
 * @param dest
 *            destination zip file
 * @throws IOException
 *             in case when something goes wrong in method
 */
public static void zip(List<File> srcs, File dest) throws IOException {
    ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(dest));
    try {
        for (File file : srcs) {
            FileInputStream inputStream = new FileInputStream(file);
            try {
                outputStream.putNextEntry(new ZipEntry(file.getName()));
                IOUtils.copy(inputStream, outputStream);
            } finally {
                inputStream.close();
                outputStream.closeEntry();
            }

        }
    } finally {
        outputStream.flush();
        outputStream.close();
    }
}

From source file:es.sm2.openppm.core.plugin.action.GenericAction.java

/**
 *
 * @param files//  w w w .ja  va  2s . co m
 * @return
 * @throws IOException
 */
public static byte[] zipFiles(List<File> files) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);

    for (File f : files) {

        zos.putNextEntry(new ZipEntry(f.getName()));

        zos.write(getBytesFromFile(f.getAbsoluteFile()));

        zos.closeEntry();
    }

    zos.flush();
    baos.flush();
    zos.close();
    baos.close();

    return baos.toByteArray();
}

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

public static File zipFile(File f) throws IOException {
    byte[] buf = new byte[1024];
    int len;/*from  w  w  w  .  j  ava 2 s.c  o  m*/

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

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile));
    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();
    out.flush();

    in.close();
    out.close();

    return zippedFile;
}

From source file:net.grinder.util.LogCompressUtil.java

/**
 * Compress the given file.// w  ww . jav  a2  s  . c  o m
 * 
 * @param logFile
 *            file to be compressed
 * @return compressed file byte array
 */
public static byte[] compressFile(File logFile) {
    FileInputStream fio = null;
    ByteArrayOutputStream out = null;
    ZipOutputStream zos = null;
    try {
        fio = new FileInputStream(logFile);
        out = new ByteArrayOutputStream();
        zos = new ZipOutputStream(out);
        ZipEntry zipEntry = new ZipEntry("log.txt");
        zipEntry.setTime(new Date().getTime());
        zos.putNextEntry(zipEntry);
        byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
        int count = 0;
        while ((count = fio.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
            zos.write(buffer, 0, count);
        }
        zos.closeEntry();
        zos.finish();
        zos.flush();
        return out.toByteArray();
    } catch (IOException e) {
        LOGGER.error("Error occurs while compress {}", logFile.getAbsolutePath());
        LOGGER.error("Details", e);
        return null;
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(fio);
        IOUtils.closeQuietly(out);
    }
}

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

private static void writeZipEntry(final String entryName, final ZipOutputStream out, final InputStream in)
        throws IOException {
    try {//  w  w w.  ja  v  a  2  s  . c  om
        ZipEntry fileEntry = new ZipEntry(entryName);
        out.putNextEntry(fileEntry);
        final byte[] buf = new byte[BUF_SIZE];
        int avail;
        while ((avail = in.read(buf)) != -1) {
            out.write(buf, 0, avail);
        }
    } finally {
        out.flush();
        out.closeEntry();
    }
}

From source file:Main.java

public static void zip(String zipFilePath, List<String> sourceFiles, String baseFolderPath, Boolean OverWrite)
        throws Exception {
    File zipFile = new File(zipFilePath);
    if (zipFile.exists() && !OverWrite)
        return;/* ww w  .  ja  v  a 2 s . c o  m*/
    else if (zipFile.exists() && OverWrite)
        zipFile.delete();

    ZipOutputStream zos = null;
    FileOutputStream outStream = null;
    outStream = new FileOutputStream(zipFile);
    zos = new ZipOutputStream(outStream);

    if (baseFolderPath == null)
        baseFolderPath = "";

    for (String item : sourceFiles) {
        String itemName = (item.startsWith(File.separator) || baseFolderPath.endsWith(File.separator)) ? item
                : (File.separator + item);

        if (baseFolderPath.equals("")) //absolute path
            addFileToZip(baseFolderPath + itemName, zos);
        else
            //relative path
            addFileToZip(baseFolderPath + itemName, baseFolderPath, zos);

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

}

From source file:Main.java

private static void zipDir(String dir, ZipOutputStream out) throws IOException {
    File directory = new File(dir);

    URI base = directory.toURI();

    ArrayList<File> filesToZip = new ArrayList<File>();

    GetFiles(directory, filesToZip);//from  ww  w . ja va2s.com

    for (int i = 0; i < filesToZip.size(); ++i) {
        FileInputStream in = new FileInputStream(filesToZip.get(i));

        String name = base.relativize(filesToZip.get(i).toURI()).getPath();

        out.putNextEntry(new ZipEntry(name));

        byte[] buf = new byte[4096];
        int bytes = 0;

        while ((bytes = in.read(buf)) != -1) {
            out.write(buf, 0, bytes);
        }

        out.closeEntry();

        in.close();
    }

    out.finish();
    out.flush();
}

From source file:b2s.idea.mavenize.JarCombiner.java

private static void copyContentsOf(File input, ZipOutputStream output, JarContext context) throws IOException {
    ZipFile zip = null;/*from   w  w w.ja va  2  s.  com*/
    try {
        zip = new ZipFile(input);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (fileCanBeIgnored(entry, context)) {
                continue;
            }

            if (isServiceEntry(entry)) {
                context.addService(entry.getName(), contentsOf(entry, zip));
                continue;
            }

            output.putNextEntry(new ZipEntry(entry.getName()));
            output.write(contentsOf(entry, zip));
            output.flush();
            output.closeEntry();

            context.addVisitedEntry(entry);
        }
    } finally {
        close(zip);
    }
}

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

/**
 * This method will bundle all the files into a zip file. 
 * If there are 2 files with the same name, only the first file is part of the zip.
 * /*from  ww w.ja  v a2s.  co m*/
 * @param zipFileName
 * @param files
 * 
 * @return zipFile absolute path
 * 
 */
public static String bundleZipFile(String zipFileName, List<File> files) {
    File zipFile = null;
    Map<String, File> filesMap = new HashMap<String, File>();

    if (StringUtils.isEmpty(zipFileName))
        return null;

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

        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
        zipOut.setLevel(ZipOutputStream.DEFLATED);
        byte[] buffer = new byte[BUFFER_SIZE];

        for (File file : files) {
            if (filesMap.containsKey(file.getName())) {
                continue;
            } else if (file.exists() && file.canRead()) {
                filesMap.put(file.getName(), file);
                zipOut.putNextEntry(new ZipEntry(file.getName()));
                FileInputStream fis = new FileInputStream(file);
                int bytesRead;
                while ((bytesRead = fis.read(buffer)) != -1) {
                    zipOut.write(buffer, 0, bytesRead);
                }
                zipOut.flush();
                zipOut.closeEntry();
            }
        }
        zipOut.finish();
        zipOut.close();
    } catch (IOException e) {
        //log.error("Error while creating Zip file");
    }

    return (null != zipFile) ? zipFile.getAbsolutePath() : null;
}