Example usage for java.util.zip Deflater BEST_COMPRESSION

List of usage examples for java.util.zip Deflater BEST_COMPRESSION

Introduction

In this page you can find the example usage for java.util.zip Deflater BEST_COMPRESSION.

Prototype

int BEST_COMPRESSION

To view the source code for java.util.zip Deflater BEST_COMPRESSION.

Click Source Link

Document

Compression level for best compression.

Usage

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    byte[] input = "asdf".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);/*from  w w  w.j  a  va  2 s.  c om*/
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();
    byte[] compressedData = bos.toByteArray();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] input = "www.java2s.com".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);//from ww w  .ja v a  2s  .  com
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();
    byte[] compressedData = bos.toByteArray();
    System.out.println(Arrays.toString(compressedData));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] input = "this is a test".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);//w  ww  .j  a v  a 2  s. co m
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();

    byte[] compressedData = bos.toByteArray();
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);

    bos = new ByteArrayOutputStream(compressedData.length);

    buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();

    byte[] decompressedData = bos.toByteArray();
    System.out.println(new String(decompressedData));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String input = "Hello world!";
    byte[] uncompressedData = input.getBytes("UTF-8");

    byte[] compressedData = compress(uncompressedData, Deflater.BEST_COMPRESSION, false);

    byte[] decompressedData = decompress(compressedData, false);

    String output = new String(decompressedData, "UTF-8");

    System.out.println("Uncompressed data length: " + uncompressedData.length);
    System.out.println("Compressed data length:  " + compressedData.length);
    System.out.println("Decompressed data length:  " + decompressedData.length);
}

From source file:Main.java

public static byte[] compress(byte[] input) {
    if (input == null || input.length == 0)
        return input;
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);/*from  w w w . j  ava  2s . c  om*/
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

    // Get the compressed data
    return bos.toByteArray();
}

From source file:Main.java

public static String compressString(String str, String encoding) {
    String compressedString = "";

    try {/*from  w ww.  j  a v  a 2s.  co  m*/
        byte[] input = str.getBytes(encoding);
        Deflater d = new Deflater();
        d.setLevel(Deflater.BEST_COMPRESSION);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DeflaterOutputStream dout = new DeflaterOutputStream(out, d);
        dout.write(input);
        dout.close();
        compressedString = new String(Hex.encodeHex(out.toByteArray()));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return compressedString;
}

From source file:Main.java

public static String compressAndB64EncodeUTF8Bytes(byte[] bytes) throws Exception {

    byte[] input = bytes;

    // Compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);/*from  w  w w.j a  va  2 s. c om*/
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Compress the data
    byte[] buf = new byte[32];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }

    // Get the compressed data
    byte[] compressedData = bos.toByteArray();

    return new String(Base64.encode(compressedData), "UTF-8");
}

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  w  w  w.ja  va2s. c  o  m
            }
        }
        out.close();
    }
    return zipfile;
}

From source file:Main.java

public static void zip(String zipFileName, String[] zipEntries) {

    try (ZipOutputStream zos = new ZipOutputStream(
            new BufferedOutputStream(new FileOutputStream(zipFileName)))) {

        // Set the compression level to best compression
        zos.setLevel(Deflater.BEST_COMPRESSION);

        for (int i = 0; i < zipEntries.length; i++) {
            File entryFile = new File(zipEntries[i]);
            if (!entryFile.exists()) {
                System.out.println("The entry file  " + entryFile.getAbsolutePath() + "  does  not  exist");
                System.out.println("Aborted   processing.");
                return;
            }//from   www .ja v a  2s . c  o m
            ZipEntry ze = new ZipEntry(zipEntries[i]);
            zos.putNextEntry(ze);
            addEntryContent(zos, zipEntries[i]);
            zos.closeEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] compress(byte[] data) throws IOException {
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION);
    compresser.setInput(data);/*from w ww .  j  a  v  a  2 s .  c  o m*/
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    compresser.finish();
    byte[] buffer = new byte[1024];
    while (!compresser.finished()) {
        int count = compresser.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    compresser.end();
    outputStream.close();
    return outputStream.toByteArray();
}