Example usage for java.util.zip ZipOutputStream setLevel

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

Introduction

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

Prototype

public void setLevel(int level) 

Source Link

Document

Sets the compression level for subsequent entries which are DEFLATED.

Usage

From source file:org.opensha.commons.util.FileUtils.java

/**
 * This creates a zip file with the given name from a list of file names.
 * //from   ww w. j a va2s. c o m
 * @param zipFile
 * @param files
 * @throws IOException
 */
public static void createZipFile(String zipFile, String dir, Collection<String> fileNames) throws IOException {

    byte[] buffer = new byte[18024];

    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));

    if (dir.length() > 0 && !dir.endsWith(File.separator))
        dir += File.separator;

    // Set the compression ratio
    out.setLevel(Deflater.DEFAULT_COMPRESSION);

    // iterate through the array of files, adding each to the zip file
    for (String file : fileNames) {
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(file));

        File f = new File(dir + file);
        if (f.isDirectory())
            continue;

        // Associate a file input stream for the current file
        FileInputStream in = new FileInputStream(f);

        // Transfer bytes from the current file to the ZIP file
        //out.write(buffer, 0, in.read(buffer));

        int len;
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        // Close the current entry
        out.closeEntry();

        // Close the current file input stream
        in.close();

    }
    // Close the ZipOutPutStream
    out.close();
}

From source file:edu.stanford.epad.plugins.qifpwrapper.QIFPHandler.java

public static File generateZipFile(List<File> files, String dirPath) {

    File dir_file = new File(dirPath);
    File zip_file = new File(dirPath + "/temp_" + (fileNum++) + ".zip");
    int dir_l = dir_file.getAbsolutePath().length();

    FileOutputStream fos = null;//from  w ww  .  j  av  a  2s . co m
    try {
        fos = new FileOutputStream(zip_file);
    } catch (FileNotFoundException e1) {
        log.warning("File not found", e1);
        return null;
    }

    ZipOutputStream zipout = new ZipOutputStream(fos);
    zipout.setLevel(1);
    for (int i = 0; i < files.size(); i++) {
        File f = (File) files.get(i);
        if (f.canRead()) {
            log.info("Adding file: " + f.getAbsolutePath());
            try {
                zipout.putNextEntry(new ZipEntry(f.getAbsolutePath().substring(dir_l + 1)));
            } catch (Exception e) {
                log.warning("Error adding to zip file", e);
                return null;
            }
            BufferedInputStream fr;
            try {
                fr = new BufferedInputStream(new FileInputStream(f));

                byte buffer[] = new byte[0xffff];
                int b;
                while ((b = fr.read(buffer)) != -1)
                    zipout.write(buffer, 0, b);

                fr.close();
                zipout.closeEntry();

            } catch (Exception e) {
                log.warning("Error closing zip file", e);
                return null;
            }
        }
    }

    try {
        zipout.finish();
        fos.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    log.info("file zipped and returning as " + zip_file.getAbsolutePath());
    return zip_file;

}

From source file:org.xwoot.xwootUtil.FileUtil.java

/**
 * Zip an array of files. Each file in the array must reflect a file on disk. If a file represents a directory on
 * disk, it will be skipped./*from   w  w  w  .j  ava2  s . com*/
 * 
 * @param files the files to be zipped.
 * @param zippedFileDestinationPath the location of the zip file that will result.
 * @throws IOException if problems occur.
 */
public static void zipFiles(File[] files, String zippedFileDestinationPath) throws IOException {
    if (files == null || zippedFileDestinationPath == null) {
        throw new NullPointerException("Null paramenters. filePaths: " + files + " zippedFileDestinationPath: "
                + zippedFileDestinationPath);
    }

    FileUtil.checkDirectoryPath(new File(zippedFileDestinationPath).getParent());

    if (files.length < 1) {
        return;
    }

    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    ZipOutputStream zos = null;

    try {
        fos = new FileOutputStream(zippedFileDestinationPath);
        bos = new BufferedOutputStream(fos);
        zos = new ZipOutputStream(fos);

        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setLevel(Deflater.BEST_COMPRESSION);

        for (File aFile : files) {
            zipFiletoZipOutputStream(aFile.getParentFile(), aFile.getName(), zos);
        }

    } catch (Exception e) {
        throw new IOException("Failed to zip files: (" + e.getClass() + ") " + e.getMessage());
    } finally {
        try {
            if (zos != null) {
                zos.close();
            }
            if (bos != null) {
                bos.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (Exception e) {
            throw new IOException("Problem closing streams. Reason: " + e.getMessage());
        }
    }
}

From source file:com.yunmel.syncretic.utils.io.IOUtils.java

/**
 * ?/*w w  w  .  ja  va  2  s. c o m*/
 * 
 * @param files 
 * @param out ?
 * @throws java.io.IOException
 * @throws Exception
 */
public static void zipDownLoad(File file, HttpServletResponse response) throws IOException {

    ServletOutputStream out = response.getOutputStream();
    ZipOutputStream zipout = new ZipOutputStream(out);

    zipout.setLevel(1);
    // zipout.setEncoding("GBK");
    for (File f : file.listFiles()) {
        if (f.isFile()) {
            compressFile(f, f.getName(), zipout, "");
        } else {
            compressFolder(f, f.getName(), zipout, "");
        }
    }

    zipout.finish();
    zipout.flush();
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);/*from  w w  w.j  av  a2s .  c  o m*/

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);/*  w w  w  .j a  v a 2 s .  co m*/

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setMethod(ZipEntry.DEFLATED);
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);/*from  w  ww.j a  v a  2  s  .c  o  m*/

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setTime(new Date().getTime());
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:com.yunmel.syncretic.utils.io.IOUtils.java

/**
 * ?/*from  w w  w.j  a  va  2s  .  c  om*/
 * 
 * @param files 
 * @param out ?
 * @throws IOException
 * @throws Exception
 */
public static void zipDownLoad(Map<File, String> downQuene, HttpServletResponse response) throws IOException {

    ServletOutputStream out = response.getOutputStream();
    ZipOutputStream zipout = new ZipOutputStream(out);
    ZipEntry entry = null;
    zipout.setLevel(1);
    // zipout.setEncoding("GBK");
    if (downQuene != null && downQuene.size() > 0) {
        for (Entry<File, String> fileInfo : downQuene.entrySet()) {
            File file = fileInfo.getKey();
            try {
                String filename = new String(fileInfo.getValue().getBytes(), "GBK");
                entry = new ZipEntry(filename);
                entry.setSize(file.length());
                zipout.putNextEntry(entry);
            } catch (IOException e) {
                // Logger.getLogger(FileUtil.class).warn(":", e);
            }
            BufferedInputStream fr = new BufferedInputStream(new FileInputStream(fileInfo.getKey()));
            int len;
            byte[] buffer = new byte[1024];
            while ((len = fr.read(buffer)) != -1)
                zipout.write(buffer, 0, len);
            fr.close();
        }
    }

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

From source file:org.celeria.minecraft.backup.ArchiveWorldTaskFactory.java

private Archive archiveFor(final FileObject file) throws FileSystemException {
    final ZipOutputStream stream = zipStreamFor(file);
    stream.setLevel(compressionLevel.asInteger());
    return new Archive(stream);
}

From source file:org.olat.core.util.ZipUtil.java

public static boolean zip(List<VFSItem> vfsFiles, VFSLeaf target, boolean compress) {
    boolean success = true;

    String zname = target.getName();
    if (target instanceof LocalImpl) {
        zname = ((LocalImpl) target).getBasefile().getAbsolutePath();
    }//from ww  w.ja v  a 2  s .c  o  m

    OutputStream out = target.getOutputStream(false);

    if (out == null) {
        throw new OLATRuntimeException(ZipUtil.class, "Error getting output stream for file: " + zname, null);
    }

    long s = System.currentTimeMillis();

    ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(out, FileUtils.BSIZE));

    if (vfsFiles.size() == 0) {
        try {
            zipOut.close();
        } catch (IOException e) {
            //
        }
        return true;
    }

    zipOut.setLevel(compress ? 9 : 0);
    for (Iterator<VFSItem> iter = vfsFiles.iterator(); success && iter.hasNext();) {
        success = addToZip(iter.next(), "", zipOut);
    }

    try {
        zipOut.flush();
        zipOut.close();
        log.info("zipped (" + (compress ? "compress" : "store") + ") " + zname + " t="
                + Long.toString(System.currentTimeMillis() - s));
    } catch (IOException e) {
        throw new OLATRuntimeException(ZipUtil.class, "I/O error closing file: " + zname, null);
    }

    return success;
}