Example usage for java.util.zip ZipOutputStream DEFLATED

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

Introduction

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

Prototype

int DEFLATED

To view the source code for java.util.zip ZipOutputStream DEFLATED.

Click Source Link

Document

Compression method for compressed (DEFLATED) entries.

Usage

From source file:Main.java

public static String compress(String filename) throws FileNotFoundException, IOException {
    byte[] buffer = new byte[4096];
    int bytesRead;
    String[] entries = { ".mp4" };
    String zipfile = filename.replace(".mp4", ".zip");
    if (!(new File(zipfile)).exists()) {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
        out.setLevel(Deflater.BEST_COMPRESSION);
        out.setMethod(ZipOutputStream.DEFLATED);
        for (int i = 0; i < entries.length; i++) {
            File f = new File(filename.replace(".mp4", entries[i]));
            if (f.exists()) {
                FileInputStream in = new FileInputStream(f);
                ZipEntry entry = new ZipEntry(f.getName());
                out.putNextEntry(entry);
                while ((bytesRead = in.read(buffer)) != -1)
                    out.write(buffer, 0, bytesRead);
                in.close();//from   www  . ja  v  a  2s .co m
            }
        }
        out.close();
    }
    return zipfile;
}

From source file:pl.umk.mat.zawodyweb.www.zip.ZipSolutions.java

public ZipSolutions() {
    out = new ZipOutputStream(baos);
    out.setMethod(ZipOutputStream.DEFLATED);
    out.setLevel(java.util.zip.Deflater.BEST_COMPRESSION);
}

From source file:org.exist.util.Compressor.java

/**
 * The method <code>compress</code>
 *
 * @param whatToCompress a <code>byte[]</code> value
 * @param length an <code>int</code> value
 * @return a <code>byte[]</code> value
 * @exception IOException if an error occurs
 *//*from   ww  w .  ja va2  s. c o m*/
public static byte[] compress(byte[] whatToCompress, int length) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ZipOutputStream gzos = new ZipOutputStream(baos);
    gzos.setMethod(ZipOutputStream.DEFLATED);
    gzos.putNextEntry(new ZipEntry(length + ""));
    gzos.write(whatToCompress, 0, length);
    gzos.closeEntry();
    gzos.finish();
    gzos.close();
    return baos.toByteArray();
}

From source file:JarUtil.java

/**
 * Writes all given files to the specified jar-file.
 * //from   www . ja v a2 s .c  o m
 * @param files
 *            all files that should be added to the JAR file
 * @param sourceDir
 *            The parent directory containing the given files.
 * @param target
 *            The jar file which should be created
 * @param compress
 *            True when the jar file should be compressed
 * @throws FileNotFoundException
 *             when a file could not be found
 * @throws IOException
 *             when a file could not be read or the jar file could not be
 *             written to.
 */
public static void jar(File sourceDir, OutputStream target, boolean compress) throws IOException {
    File[] files = sourceDir.listFiles();
    // creates target-jar-file:
    JarOutputStream out = new JarOutputStream(target);
    if (compress) {
        out.setLevel(ZipOutputStream.DEFLATED);
    } else {
        out.setLevel(ZipOutputStream.STORED);
    }
    // create a CRC32 object:
    CRC32 crc = new CRC32();
    byte[] buffer = new byte[1024 * 1024];
    // add all files:
    int sourceDirLength = sourceDir.getAbsolutePath().length() + 1;
    for (File file : files) {
        addFile(file, out, crc, sourceDirLength, buffer);
    }
    out.close();
}

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.
 * /*  www  .ja  va2  s . c o 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;
}

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

/**
 * Instantiates a new zip archive ppio.//from  www  .  ja  v a 2s . co m
 * 
 * @param resources the resources
 */
public ZipArchivePPIO(int compressionLevel) {
    super(File.class, File.class, "application/zip");
    if (compressionLevel < ZipOutputStream.STORED || compressionLevel > ZipOutputStream.DEFLATED) {
        throw new IllegalArgumentException("Invalid Compression Level: " + compressionLevel);
    }
    this.compressionLevel = compressionLevel;
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, "Using compression level " + compressionLevel);
    }
}

From source file:mpimp.assemblxweb.util.J5FileUtils.java

public static void zipDirectory(String dirPath, String targetPath) throws Exception {
    BufferedInputStream origin = null;
    FileOutputStream dest = new FileOutputStream(targetPath);
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
    out.setMethod(ZipOutputStream.DEFLATED);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];
    zipDirectoryRecursively(dirPath, origin, BUFFER, out, data, new File(dirPath).getName());
    out.close();//from w w w . j  av  a  2  s .  c o m

}

From source file:com.stehno.oxy.ZipBuilder.java

/**
 * Used to specify the the zip file should be compressed. By default no compression is performed.
 *
 * @return a reference to the zip builder
 *//*from  w w w. j a  va2  s .c  om*/
public ZipBuilder useCompression() {
    zos.setMethod(ZipOutputStream.DEFLATED);
    return (this);
}

From source file:com.espringtran.compressor4j.processor.GzipProcessor.java

/**
 * Compress data//  w w  w .jav a2  s  . co m
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GzipCompressorOutputStream cos = new GzipCompressorOutputStream(baos);
    ZipOutputStream zos = new ZipOutputStream(cos);
    try {
        zos.setLevel(fileCompressor.getLevel().getValue());
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setComment(fileCompressor.getComment());
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            zos.putNextEntry(new ZipEntry(binaryFile.getDesPath()));
            zos.write(binaryFile.getData());
            zos.closeEntry();
        }
        zos.flush();
        zos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        zos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.espringtran.compressor4j.processor.Bzip2Processor.java

/**
 * Compress data//from   ww  w. j  a va  2 s .c  om
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BZip2CompressorOutputStream cos = new BZip2CompressorOutputStream(baos);
    ZipOutputStream zos = new ZipOutputStream(cos);
    try {
        zos.setLevel(fileCompressor.getLevel().getValue());
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setComment(fileCompressor.getComment());
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            zos.putNextEntry(new ZipEntry(binaryFile.getDesPath()));
            zos.write(binaryFile.getData());
            zos.closeEntry();
        }
        zos.flush();
        zos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        zos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}